Debian Codegraph @

t/find-identifiers.t

raw
1 # vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et :
2 use strictures 2;
3 use 5.020;
4 use stable 0.031 'postderef';
5 use experimental 'signatures';
6 no autovivification warn => qw(fetch store exists delete);
7 use if "$]" >= 5.022, experimental => 're_strict';
8 no if "$]" >= 5.031009, feature => 'indirect';
9 no if "$]" >= 5.033001, feature => 'multidimensional';
10 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
11 no if "$]" >= 5.041009, feature => 'smartmatch';
12 no feature 'switch';
13 use utf8;
14 use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8
15
16 use List::Util 'unpairs';
17 use builtin::compat 'refaddr';
18 use Data::Dumper ();
19 use lib 't/lib';
20 use Helper;
21 use JSON::Schema::Modern::Utilities 'canonicalize_uri';
22
23 # spec version -> vocab classes
24 my %vocabularies = unpairs(JSON::Schema::Modern->new->__all_metaschema_vocabulary_classes);
25
26 my %dialect = (
27 specification_version => 'draft2020-12',
28 vocabularies => $vocabularies{'draft2020-12'},
29 );
30
31 subtest '$id sets canonical uri' => sub {
32 my $js = JSON::Schema::Modern->new;
33 is_equal(
34 $js->evaluate(
35 1,
36 my $schema = {
37 '$defs' => {
38 foo => my $foo_definition = {
39 '$id' => 'http://localhost:4242/my_foo',
40 const => 'foo value',
41 },
42 },
43 '$ref' => 'http://localhost:4242/my_foo',
44 },
45 )->TO_JSON,
46 {
47 valid => false,
48 errors => [
49 {
50 instanceLocation => '',
51 keywordLocation => '/$ref/const',
52 absoluteKeywordLocation => 'http://localhost:4242/my_foo#/const',
53 error => 'value does not match (wrong type: integer vs string)',
54 },
55 ],
56 },
57 '$id was recognized - $ref was successfully traversed',
58 );
59
60 cmp_result(
61 { $js->_resource_index },
62 {
63 '' => {
64 path => '', canonical_uri => str(''), document => ignore,
65 %dialect,
66 },
67 'http://localhost:4242/my_foo' => {
68 path => '/$defs/foo',
69 canonical_uri => str('http://localhost:4242/my_foo'),
70 document => methods(canonical_uri => str('')),
71 %dialect,
72 },
73 },
74 'resources indexed; document canonical_uri is still unset',
75 );
76
77 my $doc1 = $js->{_resource_index}{''}{document};
78 my $doc2 = $js->{_resource_index}{'http://localhost:4242/my_foo'}{document};
79 is(refaddr($doc1), refaddr($doc2), 'the same document object is indexed under both URIs');
80
81 sub _find_all_values ($data) {
82 if (ref $data eq 'ARRAY') {
83 return map __SUB__->($_), @$data;
84 }
85 elsif (ref $data eq 'HASH') {
86 return map __SUB__->($_), values %$data;
87 }
88 return $data;
89 }
90
91 my @blessed_values = grep ref($_), _find_all_values($doc1->schema);
92 ok(!@blessed_values, 'the schema contains no blessed leaf nodes')
93 or diag 'found blessed values: ',
94 Data::Dumper->new([ map ref, @blessed_values ])->Indent(2)->Terse(1)->Sortkeys(1)->Dump;
95
96 is(
97 canonicalize_uri($js, '#/$defs/foo'),
98 'http://localhost:4242/my_foo',
99 'canonicalize a non-canonical json pointer URI',
100 );
101 };
102
103 subtest 'anchors' => sub {
104 my $js = JSON::Schema::Modern->new;
105 is_equal(
106 $js->evaluate(
107 1,
108 my $schema = {
109 '$defs' => {
110 foo => my $foo_definition = {
111 '$anchor' => 'my_foo',
112 const => 'foo value',
113 },
114 bar => my $bar_definition = {
115 '$anchor' => 'my_bar',
116 not => true,
117 },
118 },
119 '$id' => 'http://localhost:4242',
120 allOf => [
121 { '$ref' => '#my_foo' },
122 { '$ref' => '#my_bar' },
123 { not => my $not_definition = {
124 '$anchor' => 'my_not',
125 not => false,
126 },
127 },
128 ],
129 },
130 )->TO_JSON,
131 {
132 valid => false,
133 errors => [
134 {
135 instanceLocation => '',
136 keywordLocation => '/allOf/0/$ref/const',
137 absoluteKeywordLocation => 'http://localhost:4242#/$defs/foo/const',
138 error => 'value does not match (wrong type: integer vs string)',
139 },
140 {
141 instanceLocation => '',
142 keywordLocation => '/allOf/1/$ref/not',
143 absoluteKeywordLocation => 'http://localhost:4242#/$defs/bar/not',
144 error => 'subschema is true',
145 },
146 {
147 instanceLocation => '',
148 keywordLocation => '/allOf/2/not',
149 absoluteKeywordLocation => 'http://localhost:4242#/allOf/2/not',
150 error => 'subschema is valid',
151 },
152 {
153 instanceLocation => '',
154 keywordLocation => '/allOf',
155 absoluteKeywordLocation => 'http://localhost:4242#/allOf',
156 error => 'subschemas 0, 1, 2 are not valid',
157 },
158 ],
159 },
160 '$id was recognized - absolute locations use json paths, not anchors',
161 );
162
163 cmp_result(
164 { $js->_resource_index },
165 {
166 'http://localhost:4242' => {
167 path => '',
168 canonical_uri => str('http://localhost:4242'),
169 document => methods(canonical_uri => str('http://localhost:4242')),
170 %dialect,
171 anchors => {
172 my_foo => {
173 path => '/$defs/foo',
174 canonical_uri => str('http://localhost:4242#/$defs/foo'),
175 },
176 my_bar => {
177 path => '/$defs/bar',
178 canonical_uri => str('http://localhost:4242#/$defs/bar'),
179 },
180 my_not => {
181 path => '/allOf/2/not',
182 canonical_uri => str('http://localhost:4242#/allOf/2/not'),
183 },
184 },
185 },
186 },
187 'internal resource index is correct',
188 );
189
190 is(
191 canonicalize_uri($js, 'http://localhost:4242#my_foo'),
192 'http://localhost:4242#/$defs/foo',
193 'canonicalize an anchor URI',
194 );
195 };
196
197 subtest '$anchor at root without $id' => sub {
198 my $js = JSON::Schema::Modern->new;
199 is_equal(
200 $js->evaluate(
201 1,
202 {
203 '$anchor' => 'root',
204 '$defs' => {
205 foo => {
206 '$anchor' => 'my_foo',
207 const => 'foo value',
208 },
209 },
210 '$ref' => '#my_foo',
211 },
212 )->TO_JSON,
213 {
214 valid => false,
215 errors => [
216 {
217 instanceLocation => '',
218 keywordLocation => '/$ref/const',
219 absoluteKeywordLocation => '#/$defs/foo/const',
220 error => 'value does not match (wrong type: integer vs string)',
221 },
222 ],
223 },
224 '$id without anchor was recognized - absolute locations use json paths, not anchors',
225 );
226
227 cmp_result(
228 { $js->_resource_index },
229 {
230 '' => {
231 path => '', canonical_uri => str(''), document => ignore,
232 %dialect,
233 anchors => {
234 root => {
235 path => '',
236 canonical_uri => str(''),
237 },
238 my_foo => {
239 path => '/$defs/foo',
240 canonical_uri => str('#/$defs/foo'),
241 },
242 },
243 },
244 },
245 'internal resource index is correct',
246 );
247 };
248
249 subtest '$ids and $anchors in subschemas after $id changes' => sub {
250 my $js = JSON::Schema::Modern->new;
251 is_equal(
252 $js->evaluate(
253 1,
254 {
255 '$id' => 'https://foo.com/a/alpha',
256 properties => {
257 b => {
258 '$id' => 'beta',
259 properties => {
260 d => {
261 '$anchor' => 'my_d',
262 },
263 },
264 },
265 f => {
266 '$id' => 'zeta',
267 properties => {
268 h => {
269 '$anchor' => 'my_h',
270 },
271 },
272 },
273 },
274 },
275 )->TO_JSON,
276 {
277 valid => true,
278 },
279 '$anchor is legal in a subschema',
280 );
281
282 cmp_result(
283 { $js->_resource_index },
284 {
285 'https://foo.com/a/alpha' => {
286 path => '', canonical_uri => str('https://foo.com/a/alpha'), document => ignore,
287 %dialect,
288 },
289 'https://foo.com/a/beta' => {
290 path => '/properties/b', canonical_uri => str('https://foo.com/a/beta'), document => ignore,
291 %dialect,
292 anchors => {
293 my_d => {
294 path => '/properties/b/properties/d',
295 canonical_uri => str('https://foo.com/a/beta#/properties/d'),
296 },
297 },
298 },
299 'https://foo.com/a/zeta' => {
300 path => '/properties/f', canonical_uri => str('https://foo.com/a/zeta'), document => ignore,
301 %dialect,
302 anchors => {
303 my_h => {
304 path => '/properties/f/properties/h',
305 canonical_uri => str('https://foo.com/a/zeta#/properties/h'),
306 },
307 },
308 },
309 },
310 'internal resource index is correct',
311 );
312 };
313
314 subtest 'invalid $id and $anchor' => sub {
315 my $js = JSON::Schema::Modern->new;
316
317 is_equal(
318 $js->evaluate(
319 1,
320 {
321 '$id' => 'foo.json',
322 '$defs' => {
323 a_bad_id => {
324 '$id' => 'foo.json#/foo/bar',
325 },
326 anchor_a => {
327 '$anchor' => 'a_my$foo',
328 },
329 anchor_b => {
330 '$anchor' => 'b_hello123৪২',
331 },
332 anchor_c => {
333 '$anchor' => 'c_helloðworld',
334 },
335 },
336 },
337 )->TO_JSON,
338 {
339 valid => false,
340 errors => [
341 {
342 keywordLocation => '/$defs/a_bad_id/$id',
343 absoluteKeywordLocation => 'foo.json#/$defs/a_bad_id/$id',
344 error => '$id value "foo.json#/foo/bar" cannot have a non-empty fragment',
345 },
346 map +{
347 keywordLocation => '/$defs/anchor_'.substr($_,0,1).'/$anchor',
348 absoluteKeywordLocation => 'foo.json#/$defs/anchor_'.substr($_,0,1).'/$anchor',
349 error => '$anchor value "'.$_.'" does not match required syntax',
350 }, qw(a_my$foo b_hello123৪২ c_helloðworld),
351 ],
352 },
353 'bad $id and $anchor are detected, even if bad definitions are not traversed',
354 );
355
356 is_equal(
357 $js->evaluate(
358 1,
359 {
360 '$id' => 'foo.json',
361 '$defs' => {
362 const_not_id => {
363 const => {
364 '$id' => 'not_a_real_id',
365 },
366 },
367 const_not_anchor => {
368 enum => [
369 '$anchor' => 'not_a_real_anchor',
370 ],
371 },
372 },
373 anyOf => [
374 { '$ref' => '#/$defs/const_not_id' },
375 { '$ref' => '#/$defs/const_not_anchor' },
376 true,
377 ],
378 },
379 )->TO_JSON,
380 {
381 valid => true,
382 },
383 '"bad" $ids and $anchors that are not actually keywords are not reported as errors',
384 );
385 };
386
387 subtest 'nested $ids' => sub {
388 my $js = JSON::Schema::Modern->new(short_circuit => 0);
389 my $schema = {
390 '$id' => '/foo/bar/baz.json',
391 '$ref' => '/foo/bar/baz.json#/properties/alpha', # not the canonical URI for that location
392 properties => {
393 alpha => my $alpha = {
394 '$id' => 'alpha.json',
395 additionalProperties => false,
396 properties => {
397 beta => my $beta = {
398 '$id' => '/beta/hello.json',
399 properties => {
400 gamma => my $gamma = {
401 '$id' => 'gamma.json',
402 const => 'hello',
403 },
404 },
405 },
406 },
407 },
408 },
409 };
410
411 is_equal(
412 $js->evaluate(
413 {
414 alpha => {
415 beta => {
416 gamma => 'not hello',
417 },
418 },
419 },
420 $schema,
421 )->TO_JSON,
422 {
423 valid => false,
424 errors => [
425 {
426 instanceLocation => '/alpha',
427 keywordLocation => '/$ref/additionalProperties',
428 absoluteKeywordLocation => '/foo/bar/alpha.json#/additionalProperties',
429 error => 'additional property not permitted',
430 },
431 {
432 instanceLocation => '',
433 keywordLocation => '/$ref/additionalProperties',
434 absoluteKeywordLocation => '/foo/bar/alpha.json#/additionalProperties',
435 error => 'not all additional properties are valid',
436 },
437 {
438 instanceLocation => '/alpha/beta/gamma',
439 keywordLocation => '/properties/alpha/properties/beta/properties/gamma/const',
440 absoluteKeywordLocation => '/beta/gamma.json#/const',
441 error => 'value does not match (strings not equal)',
442 },
443 {
444 instanceLocation => '/alpha/beta',
445 keywordLocation => '/properties/alpha/properties/beta/properties',
446 absoluteKeywordLocation => '/beta/hello.json#/properties',
447 error => 'not all properties are valid',
448 },
449 {
450 instanceLocation => '/alpha',
451 keywordLocation => '/properties/alpha/properties',
452 absoluteKeywordLocation => '/foo/bar/alpha.json#/properties',
453 error => 'not all properties are valid',
454 },
455 {
456 instanceLocation => '',
457 keywordLocation => '/properties',
458 absoluteKeywordLocation => '/foo/bar/baz.json#/properties',
459 error => 'not all properties are valid',
460 },
461 ],
462 },
463 'errors have the correct location',
464 );
465
466 cmp_result(
467 { $js->_resource_index },
468 {
469 '/foo/bar/baz.json' => {
470 path => '',
471 canonical_uri => str('/foo/bar/baz.json'),
472 document => methods(canonical_uri => str('/foo/bar/baz.json')),
473 %dialect,
474 },
475 '/foo/bar/alpha.json' => {
476 path => '/properties/alpha',
477 canonical_uri => str('/foo/bar/alpha.json'),
478 document => shallow($js->_get_resource('/foo/bar/baz.json')->{document}),
479 %dialect,
480 },
481 '/beta/hello.json' => {
482 path => '/properties/alpha/properties/beta',
483 canonical_uri => str('/beta/hello.json'),
484 document => shallow($js->_get_resource('/foo/bar/baz.json')->{document}),
485 %dialect,
486 },
487 '/beta/gamma.json' => {
488 path => '/properties/alpha/properties/beta/properties/gamma',
489 canonical_uri => str('/beta/gamma.json'),
490 document => shallow($js->_get_resource('/foo/bar/baz.json')->{document}),
491 %dialect,
492 },
493 },
494 'properly resolved all the nested $ids',
495 );
496 };
497
498 subtest 'multiple documents, each using canonical_uri = ""' => sub {
499 my $js = JSON::Schema::Modern->new;
500 my $schema1 = {
501 allOf => [
502 { '$id' => 'subschema1.json', type => 'string' },
503 { '$id' => 'subschema2.json', type => 'number' },
504 ],
505 };
506 my $schema2 = {
507 anyOf => [
508 { '$id' => 'subschema3.json', type => 'string' },
509 { '$id' => 'subschema4.json', type => 'number' },
510 ],
511 };
512
513 is_equal(
514 $js->evaluate(1, $schema1)->TO_JSON,
515 {
516 valid => false,
517 errors => [
518 {
519 instanceLocation => '',
520 keywordLocation => '/allOf/0/type',
521 absoluteKeywordLocation => 'subschema1.json#/type',
522 error => 'got integer, not string',
523 },
524 {
525 instanceLocation => '',
526 keywordLocation => '/allOf',
527 error => 'subschema 0 is not valid',
528 },
529 ],
530 },
531 'evaluation of schema1',
532 );
533
534 my $resource_index1 = +{ $js->_resource_index };
535 my $document1 = $resource_index1->{''}{document};
536
537 cmp_result(
538 $resource_index1,
539 {
540 '' => {
541 path => '',
542 canonical_uri => str(''),
543 document => shallow($document1),
544 %dialect,
545 },
546 'subschema1.json' => {
547 path => '/allOf/0',
548 canonical_uri => str('subschema1.json'),
549 document => shallow($document1),
550 %dialect,
551 },
552 'subschema2.json' => {
553 path => '/allOf/1',
554 canonical_uri => str('subschema2.json'),
555 document => shallow($document1),
556 %dialect,
557 },
558 },
559 'resources in initial schema are indexed',
560 );
561
562 is_equal(
563 $js->evaluate(1, $schema2)->TO_JSON,
564 {
565 valid => true,
566 },
567 'successful evaluation of schema2',
568 );
569
570 my $resource_index2 = +{ $js->_resource_index };
571 my $document2 = $resource_index2->{'subschema3.json'}{document};
572
573 cmp_result(
574 $resource_index2,
575 {
576 '' => {
577 path => '',
578 canonical_uri => str(''),
579 document => shallow($document2), # same uri as earlier, but now points to document2
580 %dialect,
581 },
582 'subschema1.json' => {
583 path => '/allOf/0',
584 canonical_uri => str('subschema1.json'),
585 document => shallow($document1), # still here! there is no reason to forget about it
586 %dialect,
587 },
588 'subschema2.json' => {
589 path => '/allOf/1',
590 canonical_uri => str('subschema2.json'),
591 document => shallow($document1), # still here! there is no reason to forget about it
592 %dialect,
593 },
594 'subschema3.json' => {
595 path => '/anyOf/0',
596 canonical_uri => str('subschema3.json'),
597 document => shallow($document2),
598 %dialect,
599 },
600 'subschema4.json' => {
601 path => '/anyOf/1',
602 canonical_uri => str('subschema4.json'),
603 document => shallow($document2),
604 %dialect,
605 },
606 },
607 'resources in second schema are indexed; all resources from first schema are preserved except uri=""',
608 );
609 };
610
611 subtest 'multiple documents, each using canonical_uri = "", collisions in other resources' => sub {
612 my $js = JSON::Schema::Modern->new;
613 my $schema1 = {
614 allOf => [
615 { '$id' => 'subschema1.json', type => 'string' },
616 { '$id' => 'subschema2.json', type => 'number' },
617 ],
618 };
619 my $schema2 = {
620 anyOf => [
621 { '$id' => 'subschema1.json', type => 'string' },
622 { '$id' => 'subschema3.json', type => 'number' },
623 ],
624 };
625
626 is_equal(
627 $js->evaluate(1, $schema1)->TO_JSON,
628 {
629 valid => false,
630 errors => [
631 {
632 instanceLocation => '',
633 keywordLocation => '/allOf/0/type',
634 absoluteKeywordLocation => 'subschema1.json#/type',
635 error => 'got integer, not string',
636 },
637 {
638 instanceLocation => '',
639 keywordLocation => '/allOf',
640 error => 'subschema 0 is not valid',
641 },
642 ],
643 },
644 'evaluation of schema1',
645 );
646
647 my $resource_index1 = +{ $js->_resource_index };
648 my $document1 = $resource_index1->{''}{document};
649
650 cmp_result(
651 $resource_index1,
652 {
653 '' => {
654 path => '',
655 canonical_uri => str(''),
656 document => shallow($document1),
657 %dialect,
658 },
659 'subschema1.json' => {
660 path => '/allOf/0',
661 canonical_uri => str('subschema1.json'),
662 document => shallow($document1),
663 %dialect,
664 },
665 'subschema2.json' => {
666 path => '/allOf/1',
667 canonical_uri => str('subschema2.json'),
668 document => shallow($document1),
669 %dialect,
670 },
671 },
672 'resources in initial schema are indexed',
673 );
674
675 cmp_result(
676 $js->evaluate(1, $schema2)->TO_JSON,
677 {
678 valid => false,
679 errors => [
680 {
681 error => re(qr/^EXCEPTION: uri "subschema1.json" conflicts with an existing schema resource/),
682 instanceLocation => '',
683 keywordLocation => '',
684 },
685 ],
686 },
687 'schema2 cannot be evaluated - an internal $id collides with an existing resource',
688 );
689 };
690
691 subtest 'resource collisions in canonical uris' => sub {
692 my $js = JSON::Schema::Modern->new;
693 $js->add_schema({ '$id' => 'https://foo.com/x/y/z' });
694
695 cmp_result(
696 $js->evaluate(1, { '$id' => 'https://foo.com', anyOf => [ { '$id' => '/x/y/z' } ] })->TO_JSON,
697 {
698 valid => false,
699 errors => [
700 {
701 instanceLocation => '',
702 keywordLocation => '',
703 error => re(qr{^EXCEPTION: uri "https://foo.com/x/y/z" conflicts with an existing schema resource}),
704 }
705 ],
706 },
707 'detected collision between a document\'s initial uri and a document\'s subschema\'s uri',
708 );
709
710 $js = JSON::Schema::Modern->new;
711 $js->add_schema({
712 '$id' => 'https://foo.com',
713 anyOf => [ { '$id' => '/x/y/z' } ],
714 });
715
716 cmp_result(
717 $js->evaluate(1, { allOf => [ { '$id' => 'https://foo.com/x/y/z' } ] })->TO_JSON,
718 {
719 valid => false,
720 errors => [
721 {
722 instanceLocation => '',
723 keywordLocation => '',
724 error => re(qr{^EXCEPTION: uri "https://foo.com/x/y/z" conflicts with an existing schema resource}),
725 }
726 ],
727 },
728 'detected collision between two document subschema uris',
729 );
730 };
731
732 subtest 'relative uri in $id' => sub {
733 is_equal(
734 JSON::Schema::Modern->new->evaluate(
735 1,
736 {
737 '$id' => 'foo/bar/baz.json',
738 type => 'object',
739 },
740 )->TO_JSON,
741 {
742 valid => false,
743 errors => [
744 {
745 instanceLocation => '',
746 keywordLocation => '/type',
747 absoluteKeywordLocation => 'foo/bar/baz.json#/type',
748 error => 'got integer, not object',
749 },
750 ],
751 },
752 'root schema location is correctly identified',
753 );
754
755 is_equal(
756 JSON::Schema::Modern->new->evaluate(
757 [ 1, [ 2, 3 ] ],
758 {
759 '$id' => 'foo/bar/baz.json',
760 type => [ 'integer', 'array' ],
761 items => { '$ref' => '#' },
762 },
763 )->TO_JSON,
764 {
765 valid => true,
766 },
767 'properly able to traverse a recursive schema using a relative $id',
768 );
769 };
770
771 done_testing;