Debian Codegraph @

t/serialization.t

raw
1 # vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et :
2 use strictures 2;
3 use stable 0.031 'postderef';
4 use experimental 'signatures';
5 no autovivification warn => qw(fetch store exists delete);
6 use if "$]" >= 5.022, experimental => 're_strict';
7 no if "$]" >= 5.031009, feature => 'indirect';
8 no if "$]" >= 5.033001, feature => 'multidimensional';
9 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
10 no if "$]" >= 5.041009, feature => 'smartmatch';
11 no feature 'switch';
12 use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8
13
14 use lib 't/lib';
15 use Helper;
16 use Test::Needs qw(Sereal::Encoder Sereal::Decoder);
17 use Test2::Warnings qw(:no_end_test had_no_warnings allow_warnings);
18
19 my $js = JSON::Schema::Modern->new(
20 collect_annotations => 1,
21 scalarref_booleans => 1,
22 stringy_numbers => 1,
23 strict => 0,
24 max_depth => 42,
25 specification_version => 'draft2019-09',
26 );
27
28 my $metaschema = {
29 '$id' => 'https://my_metaschema',
30 '$schema' => 'https://json-schema.org/draft/2020-12/schema',
31 '$vocabulary' => {
32 'https://json-schema.org/draft/2020-12/vocab/core' => true,
33 'https://json-schema.org/draft/2020-12/vocab/format-annotation' => true,
34 },
35 };
36 my $schema = {
37 '$id' => 'https://my_schema',
38 '$schema' => 'https://my_metaschema',
39 type => 'number',
40 format => 'ipv4',
41 unknown => 1,
42 properties => { hello => false },
43 contentMediaType => 'application/json',
44 contentSchema => {},
45 };
46
47 $js->add_schema($metaschema);
48 $js->add_schema($schema);
49 is_equal(
50 $js->evaluate($schema, {})->TO_JSON,
51 { valid => true },
52 'evaluated against an empty schema',
53 );
54
55 is_equal(
56 $js->evaluate(1, 'https://my_schema')->TO_JSON,
57 my $pass_result = {
58 valid => true,
59 annotations => [
60 map +{
61 instanceLocation => '',
62 keywordLocation => '/'.$_,
63 absoluteKeywordLocation => 'https://my_schema#/'.$_,
64 annotation => $schema->{$_},
65 }, 'format', sort qw(type unknown properties contentMediaType contentSchema),
66 ],
67 },
68 'evaluate data against schema with custom dialect; format and unknown keywords are collected as annotations',
69 );
70
71 is_equal(
72 $js->evaluate('foo', 'https://my_schema')->TO_JSON,
73 $pass_result,
74 'evaluate data against schema with custom dialect; format-annotation is used',
75 );
76
77 my @serialized_attributes = sort qw(
78 specification_version
79 output_format
80 short_circuit
81 max_depth
82 validate_formats
83 validate_content_schemas
84 collect_annotations
85 scalarref_booleans
86 stringy_numbers
87 strict
88 _resource_index
89 _vocabulary_classes
90 _metaschema_vocabulary_classes
91 );
92
93 my $frozen = $js->FREEZE(undef);
94 is_equal(
95 [ sort keys %$frozen ],
96 [ sort @serialized_attributes ],
97 'frozen object contains all the right keys',
98 );
99
100 my $thawed = JSON::Schema::Modern->THAW(undef, $frozen);
101 is_equal(
102 [ sort keys %$thawed ],
103 [ sort @serialized_attributes ],
104 'thawed object contains all the right keys',
105 );
106
107 $frozen = Sereal::Encoder->new({ freeze_callbacks => 1 })->encode($js);
108 Sereal::Decoder->new->decode($frozen, $thawed);
109
110 is_equal(
111 $thawed->evaluate($schema, {})->TO_JSON,
112 { valid => true },
113 'evaluate again against an empty schema',
114 );
115
116 is_equal(
117 $thawed->evaluate('hi', 'https://my_schema')->TO_JSON,
118 {
119 valid => true,
120 annotations => [
121 map +{
122 instanceLocation => '',
123 keywordLocation => '/'.$_,
124 absoluteKeywordLocation => 'https://my_schema#/'.$_,
125 annotation => $schema->{$_},
126 }, 'format', sort qw(type unknown properties contentMediaType contentSchema),
127 ],
128 },
129 'in thawed object, evaluate data against schema with custom dialect; format and unknown keywords are collected as annotations',
130 );
131
132 ok(
133 lives {
134 my $js = JSON::Schema::Modern->new;
135 my $document = $js->_fetch_from_uri('https://json-schema.org/draft/2019-09/schema')->{document};
136 my $frozen = Sereal::Encoder->new({ freeze_callbacks => 1 })->encode($document);
137 my $thawed = Sereal::Decoder->new->decode($frozen);
138 },
139 'can successfully freeze and thaw a document that resides in the global cache',
140 );
141
142 $frozen = Sereal::Encoder->new({ freeze_callbacks => 1 })->encode($js);
143 $thawed = Sereal::Decoder->new->decode($frozen);
144 is_equal(
145 $thawed->evaluate($schema, {})->TO_JSON,
146 { valid => true },
147 'evaluate again against an empty schema',
148 );
149
150 ok($thawed->_get_vocabulary_class('https://json-schema.org/draft/2020-12/vocab/core'), 'core vocabulary_class for a different spec version works in a thawed object');
151 ok($thawed->_get_vocabulary_class('https://json-schema.org/draft/2020-12/vocab/format-assertion'), 'format-assertion vocabulary_class works in a thawed object');
152 ok($thawed->_get_metaschema_vocabulary_classes('https://json-schema.org/draft/2020-12/schema'), 'metaschema_vocabulary_classes works in a thawed object');
153 allow_warnings(1);
154 ok($thawed->get_media_type('application/json'), 'media_type works in a thawed object');
155 allow_warnings(0);
156 ok($thawed->get_encoding('base64'), 'encoding works in a thawed object');
157
158 # now try to thaw the file in a new process and run some more tests
159 if ("$]" >= '5.022' or $^O ne 'MSWin32') {
160 open my $child_in, '|-:raw', $^X, (-d 'blib' ? '-Mblib' : '-Ilib'), 't/read_serialized_file';
161 print $child_in $frozen;
162 close $child_in;
163
164 my $hub = Test2::API::test2_stack->top;
165 $hub->set_count($hub->count + 1 + ($ENV{AUTHOR_TESTING} ? 1 : 0));
166
167 is($? >> 8, 0, 'child process finished successfully');
168 }
169
170 # skip the END block which would normally try to print a plan
171 Test2::API::test2_stack->top->set_no_ending(1);
172
173 had_no_warnings() if $ENV{AUTHOR_TESTING};
174 done_testing;