Debian Codegraph @

t/ref.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 Clone 'clone';
15 use lib 't/lib';
16 use Helper;
17
18 my $js = JSON::Schema::Modern->new;
19
20 subtest 'local JSON pointer' => sub {
21 is_equal(
22 $js->evaluate(true, { '$defs' => { true => true }, '$ref' => '#/$defs/true' })->TO_JSON,
23 { valid => true },
24 'can follow local $ref to a true schema',
25 );
26
27 cmp_result(
28 $js->evaluate(true, { '$defs' => { false => false }, '$ref' => '#/$defs/false' })->TO_JSON,
29 superhashof({ valid => false }),
30 'can follow local $ref to a false schema',
31 );
32
33 ok(
34 lives {
35 my $result = $js->evaluate(true, { '$ref' => 'http://example.com#/$defs/nowhere' });
36 like(
37 (($result->errors)[0])->error,
38 qr{^EXCEPTION: unable to find resource "http://example.com\#/\$defs/nowhere"},
39 'got error for unresolvable ref',
40 );
41 },
42 'no exception',
43 );
44 };
45
46 subtest 'fragment with URI-escaped and JSON Pointer-escaped characters' => sub {
47 is_equal(
48 $js->evaluate(
49 1,
50 {
51 '$defs' => { 'foo-bar-tilde~-slash/-braces{}-def' => true },
52 '$ref' => '#/$defs/foo-bar-tilde~0-slash~1-braces%7B%7D-def',
53 },
54 )->TO_JSON,
55 { valid => true },
56 'can follow $ref with escaped components',
57 );
58 };
59
60 subtest 'local anchor' => sub {
61 is_equal(
62 $js->evaluate(
63 true,
64 {
65 '$defs' => {
66 true => {
67 '$anchor' => 'true',
68 },
69 },
70 '$ref' => '#true',
71 },
72 )->TO_JSON,
73 { valid => true },
74 'can follow local $ref to an $anchor to a true schema',
75 );
76
77 cmp_result(
78 $js->evaluate(
79 true,
80 {
81 '$defs' => {
82 false => {
83 '$anchor' => 'false',
84 not => true,
85 },
86 },
87 '$ref' => '#false',
88 },
89 )->TO_JSON,
90 superhashof({ valid => false }),
91 'can follow local $ref to an $anchor to a false schema',
92 );
93
94 is(
95 dies {
96 my $result = $js->evaluate(true, { '$ref' => 'http://example.com#nowhere' });
97 like(
98 (($result->errors)[0])->error,
99 qr{^EXCEPTION: unable to find resource "http://example.com\#nowhere"},
100 'got error for unresolvable ref',
101 );
102 },
103 undef,
104 'no exception',
105 );
106 };
107
108 subtest '$id with an empty fragment' => sub {
109 my $js = JSON::Schema::Modern->new(max_depth => 2);
110 is_equal(
111 $js->evaluate(
112 1,
113 {
114 '$defs' => {
115 foo => {
116 '$id' => 'http://localhost:4242/my_foo#',
117 type => 'string',
118 },
119 reference_to_foo => {
120 '$ref' => 'http://localhost:4242/my_foo',
121 },
122 },
123 allOf => [
124 { '$ref' => 'http://localhost:4242/my_foo' },
125 { '$ref' => '#/$defs/reference_to_foo' },
126 ],
127 },
128 )->TO_JSON,
129 {
130 valid => false,
131 errors => [
132 {
133 instanceLocation => '',
134 keywordLocation => '/allOf/0/$ref/type',
135 absoluteKeywordLocation => 'http://localhost:4242/my_foo#/type',
136 error => 'got integer, not string',
137 },
138 {
139 absoluteKeywordLocation => 'http://localhost:4242/my_foo',
140 error => 'EXCEPTION: maximum evaluation depth (2) exceeded',
141 instanceLocation => '',
142 keywordLocation => "/allOf/1/\$ref/\$ref",
143 },
144 ],
145 },
146 '$id with empty fragment can be found by $ref that did not include it; fragment not included in error either',
147 );
148 };
149
150 $js = JSON::Schema::Modern->new(specification_version => 'draft2019-09');
151 subtest '$recursiveRef without nesting behaves like $ref' => sub {
152 is_equal(
153 $js->evaluate(
154 { foo => { bar => 'hello', baz => 1 } },
155 {
156 '$id' => 'http://localhost:4242',
157 '$recursiveAnchor' => true,
158 anyOf => [
159 { type => 'string' },
160 {
161 type => 'object',
162 additionalProperties => { '$recursiveRef' => '#' },
163 },
164 ],
165 },
166 )->TO_JSON,
167 {
168 valid => false,
169 errors => [
170 {
171 instanceLocation => '',
172 keywordLocation => '/anyOf/0/type',
173 absoluteKeywordLocation => 'http://localhost:4242#/anyOf/0/type',
174 error => 'got object, not string',
175 },
176 # /anyOf/1 with ''
177 # /anyOf/1/additionalProperties/$recursiveRef with '/foo'
178 # /anyOf/1/additionalProperties/$recursiveRef/anyOf/0 - wrong type
179 {
180 instanceLocation => '/foo',
181 keywordLocation => '/anyOf/1/additionalProperties/$recursiveRef/anyOf/0/type',
182 absoluteKeywordLocation => 'http://localhost:4242#/anyOf/0/type',
183 error => 'got object, not string',
184 },
185 # /anyOf/1/additionalProperties/$recursiveRef/anyOf/1 with /foo
186 # additionalProperties: consider /foo/bar
187 # /anyOf/1/additionalProperties/$recursiveRef/anyOf/1/additionalProperties/$recursiveRef
188 # /anyOf/1/additionalProperties/$recursiveRef/anyOf/1/additionalProperties/$recursiveRef/anyOf/0 - is string, so no error
189 # /anyOf/1/additionalProperties/$recursiveRef/anyOf/1/additionalProperties/$recursiveRef/anyOf/1 - is object
190 # additionalProperties: consider /foo/baz
191 # is neither string or object
192 {
193 instanceLocation => '/foo/baz',
194 keywordLocation => '/anyOf/1/additionalProperties/$recursiveRef/anyOf/1/additionalProperties/$recursiveRef/anyOf/0/type',
195 absoluteKeywordLocation => 'http://localhost:4242#/anyOf/0/type',
196 error => 'got integer, not string',
197 },
198 {
199 instanceLocation => '/foo/baz',
200 keywordLocation => '/anyOf/1/additionalProperties/$recursiveRef/anyOf/1/additionalProperties/$recursiveRef/anyOf/1/type',
201 absoluteKeywordLocation => 'http://localhost:4242#/anyOf/1/type',
202 error => 'got integer, not object',
203 },
204 {
205 instanceLocation => '/foo/baz',
206 keywordLocation => '/anyOf/1/additionalProperties/$recursiveRef/anyOf/1/additionalProperties/$recursiveRef/anyOf',
207 absoluteKeywordLocation => 'http://localhost:4242#/anyOf',
208 error => 'no subschemas are valid',
209 },
210 # and now we start to unwind.
211 {
212 instanceLocation => '/foo',
213 keywordLocation => '/anyOf/1/additionalProperties/$recursiveRef/anyOf/1/additionalProperties',
214 absoluteKeywordLocation => 'http://localhost:4242#/anyOf/1/additionalProperties',
215 error => 'not all additional properties are valid',
216 },
217 {
218 instanceLocation => '/foo',
219 keywordLocation => '/anyOf/1/additionalProperties/$recursiveRef/anyOf',
220 absoluteKeywordLocation => 'http://localhost:4242#/anyOf',
221 error => 'no subschemas are valid',
222 },
223 {
224 instanceLocation => '',
225 keywordLocation => '/anyOf/1/additionalProperties',
226 absoluteKeywordLocation => 'http://localhost:4242#/anyOf/1/additionalProperties',
227 error => 'not all additional properties are valid',
228 },
229 {
230 instanceLocation => '',
231 keywordLocation => '/anyOf',
232 absoluteKeywordLocation => 'http://localhost:4242#/anyOf',
233 error => 'no subschemas are valid',
234 },
235 ],
236 },
237 '$recursiveRef without nested $recursiveAnchor behaves like $ref',
238 );
239 };
240
241 subtest '$recursiveRef without $recursiveAnchor behaves like $ref' => sub {
242 is_equal(
243 $js->evaluate(
244 { foo => { bar => 1 } },
245 {
246 properties => { foo => { '$recursiveRef' => '#' } },
247 additionalProperties => false,
248 },
249 )->TO_JSON,
250 {
251 valid => false,
252 errors => [
253 {
254 instanceLocation => '/foo/bar',
255 keywordLocation => '/properties/foo/$recursiveRef/additionalProperties',
256 absoluteKeywordLocation => '#/additionalProperties',
257 error => 'additional property not permitted',
258 },
259 {
260 instanceLocation => '/foo',
261 keywordLocation => '/properties/foo/$recursiveRef/additionalProperties',
262 absoluteKeywordLocation => '#/additionalProperties',
263 error => 'not all additional properties are valid',
264 },
265 {
266 instanceLocation => '',
267 keywordLocation => '/properties',
268 error => 'not all properties are valid',
269 },
270 ],
271 },
272 '$recursiveRef without $recursiveAnchor behaves like $ref',
273 );
274 };
275
276 subtest '$recursiveAnchor must be at a schema resource root' => sub {
277 my $schema = {
278 '$defs' => {
279 myobject => {
280 '$recursiveAnchor' => true,
281 anyOf => [
282 { type => 'integer' },
283 {
284 type => 'object',
285 additionalProperties => { '$recursiveRef' => '#' },
286 },
287 ],
288 },
289 },
290 anyOf => [
291 { type => 'integer' },
292 { '$ref' => '#/$defs/myobject' },
293 ],
294 };
295
296 is_equal(
297 $js->evaluate({ foo => 1 }, $schema)->TO_JSON,
298 {
299 valid => false,
300 errors => [
301 {
302 keywordLocation => '/$defs/myobject/$recursiveAnchor',
303 error => '"$recursiveAnchor" keyword used without "$id"',
304 },
305 ],
306 },
307 '$recursiveAnchor can only appear at a schema resource root',
308 );
309
310 $schema = clone($schema);
311 $schema->{'$defs'}{myobject}{'$id'} = 'myobject.json';
312
313 is_equal(
314 $js->evaluate({ foo => 1 }, $schema)->TO_JSON,
315 {
316 valid => true,
317 },
318 'schema now valid when an $id is added',
319 );
320
321 is_equal(
322 $js->evaluate(
323 { foo => 1 },
324 {
325 '$defs' => {
326 inner => {
327 '$recursiveAnchor' => true, # this is illegal - canonical uri has a fragment
328 type => [ qw(integer object) ],
329 additionalProperties => { '$recursiveRef' => '#/$defs/inner' },
330 },
331 },
332 type => 'object',
333 additionalProperties => { '$recursiveRef' => '#/$defs/inner' },
334 },
335 )->TO_JSON,
336 {
337 valid => false,
338 errors => [
339 {
340 keywordLocation => '/$defs/inner/$recursiveAnchor',
341 error => '"$recursiveAnchor" keyword used without "$id"',
342 },
343 ],
344 },
345 '$recursiveAnchor can only appear at a schema resource root',
346 );
347
348 is_equal(
349 $js->evaluate(
350 { foo => 1 },
351 {
352 allOf => [
353 {
354 '$recursiveAnchor' => true,
355 type => [ qw(integer object) ],
356 additionalProperties => { '$recursiveRef' => '#/allOf/1' },
357 },
358 ],
359 },
360 )->TO_JSON,
361 {
362 valid => false,
363 errors => [
364 {
365 keywordLocation => '/allOf/0/$recursiveAnchor',
366 error => '"$recursiveAnchor" keyword used without "$id"',
367 },
368 ],
369 },
370 'properly detecting a bad $recursiveAnchor even before passing through a $ref',
371 );
372 };
373
374 subtest '$recursiveAnchor and $recursiveRef - standard usecases' => sub {
375 my $schema = {
376 '$id' => 'https://base.com',
377 #'$recursiveAnchor' => true, # the presence of this keyword changes everything
378 type => [ 'object', 'integer' ],
379 additionalProperties => {
380 '$id' => 'https://innerbase.com',
381 #'$recursiveAnchor' => true, # the presence of this keyword changes everything
382 type => [ 'object', 'boolean' ],
383 additionalProperties => {
384 '$ref' => '#', # if this was a $recursiveRef and there are $recursiveAnchors, we will go to base.
385 },
386 },
387 };
388
389 is_equal(
390 $js->evaluate({ foo => { bar => 1 } }, $schema)->TO_JSON,
391 {
392 valid => false,
393 errors => my $errors = [
394 {
395 instanceLocation => '/foo/bar',
396 keywordLocation => '/additionalProperties/additionalProperties/$ref/type',
397 absoluteKeywordLocation => 'https://innerbase.com#/type',
398 error => 'got integer, not one of object, boolean',
399 },
400 {
401 instanceLocation => '/foo',
402 keywordLocation => '/additionalProperties/additionalProperties',
403 absoluteKeywordLocation => 'https://innerbase.com#/additionalProperties',
404 error => 'not all additional properties are valid',
405 },
406 {
407 instanceLocation => '',
408 keywordLocation => '/additionalProperties',
409 absoluteKeywordLocation => 'https://base.com#/additionalProperties',
410 error => 'not all additional properties are valid',
411 },
412 ],
413 },
414 'validation requires the override that is not in scope',
415 );
416
417 # now make the ref a recursiveRef, but still won't recurse to base because no recursiveanchor.
418
419 $js->{_resource_index} = {};
420 $schema->{additionalProperties}{additionalProperties}{'$recursiveRef'} =
421 delete $schema->{additionalProperties}{additionalProperties}{'$ref'};
422
423 is_equal(
424 $js->evaluate({ foo => { bar => 1 } }, $schema)->TO_JSON,
425 {
426 valid => false,
427 errors => [
428 +{
429 $errors->[0]->%*,
430 keywordLocation => ($errors->[0]{keywordLocation} =~ s/ref/recursiveRef/r),
431 },
432 $errors->@[1..2],
433 ],
434 },
435 '$recursiveRef requires a $recursiveAnchor that does not exist',
436 );
437
438 # now we will recurse to the base.
439
440 $js->{_resource_index} = {};
441 $schema->{'$recursiveAnchor'} = true;
442
443 is_equal(
444 $js->evaluate({ foo => true }, $schema)->TO_JSON,
445 {
446 valid => true,
447 },
448 '$recursiveRef with both $recursiveAnchors in scope',
449 );
450 };
451
452 subtest '$recursiveRef without $recursiveAnchor' => sub {
453 my $schema = {
454 '$id' => 'strings_only',
455 '$defs' => {
456 allow_ints => {
457 '$id' => 'allow_ints',
458 anyOf => [
459 { type => 'integer' },
460 { type => 'object', additionalProperties => { '$ref' => '#' } },
461 ],
462 },
463 },
464 anyOf => [
465 { type => 'string' },
466 { type => 'object', additionalProperties => { '$ref' => '#' } },
467 ],
468 };
469
470 is_equal(
471 $js->evaluate(
472 { foo => 1 },
473 $schema,
474 )->TO_JSON,
475 {
476 valid => false,
477 errors => my $errors = [
478 {
479 instanceLocation => '',
480 keywordLocation => '/anyOf/0/type',
481 absoluteKeywordLocation => 'strings_only#/anyOf/0/type',
482 error => 'got object, not string',
483 },
484 {
485 instanceLocation => '/foo',
486 keywordLocation => '/anyOf/1/additionalProperties/$ref/anyOf/0/type',
487 absoluteKeywordLocation => 'strings_only#/anyOf/0/type',
488 error => 'got integer, not string',
489 },
490 {
491 instanceLocation => '/foo',
492 keywordLocation => '/anyOf/1/additionalProperties/$ref/anyOf/1/type',
493 absoluteKeywordLocation => 'strings_only#/anyOf/1/type',
494 error => 'got integer, not object',
495 },
496 {
497 instanceLocation => '/foo',
498 keywordLocation => '/anyOf/1/additionalProperties/$ref/anyOf',
499 absoluteKeywordLocation => 'strings_only#/anyOf',
500 error => 'no subschemas are valid',
501 },
502 {
503 instanceLocation => '',
504 keywordLocation => '/anyOf/1/additionalProperties',
505 absoluteKeywordLocation => 'strings_only#/anyOf/1/additionalProperties',
506 error => 'not all additional properties are valid',
507 },
508 {
509 instanceLocation => '',
510 keywordLocation => '/anyOf',
511 absoluteKeywordLocation => 'strings_only#/anyOf',
512 error => 'no subschemas are valid',
513 },
514 ],
515 },
516 '$ref - one level recursion',
517 );
518
519 $js->{_resource_index} = {};
520
521 is_equal(
522 $js->evaluate(
523 { foo => 1 },
524 $js->_json_decoder->decode($js->_json_decoder->encode($schema) =~ s/\$ref/\$recursiveRef/gr),
525 )->TO_JSON,
526 {
527 valid => false,
528 errors => $js->_json_decoder->decode($js->_json_decoder->encode($errors) =~ s/\$ref/\$recursiveRef/gr),
529 },
530 '$recursiveRef with no $recursiveAnchor in scope has the same outcome',
531 );
532 };
533
534 subtest '$recursiveAnchor in our dynamic scope, but not in the target schema' => sub {
535 my $schema = {
536 '$id' => 'base',
537 '$recursiveAnchor' => true,
538 anyOf => [
539 { type => 'boolean' },
540 {
541 type => 'object',
542 additionalProperties => {
543 '$id' => 'inner',
544 # note: no $recursiveAnchor here! so we do NOT recurse to the base.
545 anyOf => [
546 { type => 'integer' },
547 { type => 'object', additionalProperties => { '$recursiveRef' => '#' } },
548 ],
549 },
550 },
551 ],
552 };
553
554 is_equal(
555 $js->evaluate(
556 { foo => { bar => 1 } },
557 $schema,
558 )->TO_JSON,
559 {
560 valid => true,
561 },
562 '$recursiveAnchor does not exist in the target schema - local recursion only, so integers match',
563 );
564
565 is_equal(
566 $js->evaluate(
567 { foo => true },
568 'base',
569 )->TO_JSON,
570 {
571 valid => false,
572 errors => [
573 {
574 instanceLocation => '',
575 keywordLocation => '/anyOf/0/type',
576 absoluteKeywordLocation => 'base#/anyOf/0/type',
577 error => 'got object, not boolean',
578 },
579 {
580 instanceLocation => '/foo',
581 keywordLocation => '/anyOf/1/additionalProperties/anyOf/0/type',
582 absoluteKeywordLocation => 'inner#/anyOf/0/type',
583 error => 'got boolean, not integer',
584 },
585 {
586 instanceLocation => '/foo',
587 keywordLocation => '/anyOf/1/additionalProperties/anyOf/1/type',
588 absoluteKeywordLocation => 'inner#/anyOf/1/type',
589 error => 'got boolean, not object',
590 },
591 {
592 instanceLocation => '/foo',
593 keywordLocation => '/anyOf/1/additionalProperties/anyOf',
594 absoluteKeywordLocation => 'inner#/anyOf',
595 error => 'no subschemas are valid',
596 },
597 {
598 instanceLocation => '',
599 keywordLocation => '/anyOf/1/additionalProperties',
600 absoluteKeywordLocation => 'base#/anyOf/1/additionalProperties',
601 error => 'not all additional properties are valid',
602 },
603 {
604 instanceLocation => '',
605 keywordLocation => '/anyOf',
606 absoluteKeywordLocation => 'base#/anyOf',
607 error => 'no subschemas are valid',
608 },
609 ],
610 },
611 '$recursiveAnchor does not exist in the target schema - no recursion',
612 );
613
614 is_equal(
615 $js->evaluate(
616 { foo => { bar => true } },
617 'base',
618 )->TO_JSON,
619 {
620 valid => false,
621 errors => [
622 {
623 instanceLocation => '',
624 keywordLocation => '/anyOf/0/type',
625 absoluteKeywordLocation => 'base#/anyOf/0/type',
626 error => 'got object, not boolean',
627 },
628 {
629 instanceLocation => '/foo',
630 keywordLocation => '/anyOf/1/additionalProperties/anyOf/0/type',
631 absoluteKeywordLocation => 'inner#/anyOf/0/type',
632 error => 'got object, not integer',
633 },
634 {
635 instanceLocation => '/foo/bar',
636 keywordLocation => '/anyOf/1/additionalProperties/anyOf/1/additionalProperties/$recursiveRef/anyOf/0/type',
637 absoluteKeywordLocation => 'inner#/anyOf/0/type',
638 error => 'got boolean, not integer',
639 },
640 {
641 instanceLocation => '/foo/bar',
642 keywordLocation => '/anyOf/1/additionalProperties/anyOf/1/additionalProperties/$recursiveRef/anyOf/1/type',
643 absoluteKeywordLocation => 'inner#/anyOf/1/type',
644 error => 'got boolean, not object',
645 },
646 {
647 instanceLocation => '/foo/bar',
648 keywordLocation => '/anyOf/1/additionalProperties/anyOf/1/additionalProperties/$recursiveRef/anyOf',
649 absoluteKeywordLocation => 'inner#/anyOf',
650 error => 'no subschemas are valid',
651 },
652 {
653 instanceLocation => '/foo',
654 keywordLocation => '/anyOf/1/additionalProperties/anyOf/1/additionalProperties',
655 absoluteKeywordLocation => 'inner#/anyOf/1/additionalProperties',
656 error => 'not all additional properties are valid',
657 },
658 {
659 instanceLocation => '/foo',
660 keywordLocation => '/anyOf/1/additionalProperties/anyOf',
661 absoluteKeywordLocation => 'inner#/anyOf',
662 error => 'no subschemas are valid',
663 },
664 {
665 instanceLocation => '',
666 keywordLocation => '/anyOf/1/additionalProperties',
667 absoluteKeywordLocation => 'base#/anyOf/1/additionalProperties',
668 error => 'not all additional properties are valid',
669 },
670 {
671 instanceLocation => '',
672 keywordLocation => '/anyOf',
673 absoluteKeywordLocation => 'base#/anyOf',
674 error => 'no subschemas are valid',
675 },
676 ],
677 },
678 '$recursiveAnchor does not exist in the target schema - local recursion only',
679 );
680 };
681
682 $js = JSON::Schema::Modern->new;
683 subtest '$dynamicRef without nesting behaves like $ref' => sub {
684 is_equal(
685 $js->evaluate(
686 { foo => { bar => 'hello', baz => 1 } },
687 {
688 '$id' => 'http://localhost:4242',
689 '$dynamicAnchor' => 'hi',
690 anyOf => [
691 { type => 'string' },
692 {
693 type => 'object',
694 additionalProperties => { '$dynamicRef' => '#hi' },
695 },
696 ],
697 },
698 )->TO_JSON,
699 {
700 valid => false,
701 errors => [
702 {
703 instanceLocation => '',
704 keywordLocation => '/anyOf/0/type',
705 absoluteKeywordLocation => 'http://localhost:4242#/anyOf/0/type',
706 error => 'got object, not string',
707 },
708 # /anyOf/1 with ''
709 # /anyOf/1/additionalProperties/$dynamicRef with '/foo'
710 # /anyOf/1/additionalProperties/$dynamicRef/anyOf/0 - wrong type
711 {
712 instanceLocation => '/foo',
713 keywordLocation => '/anyOf/1/additionalProperties/$dynamicRef/anyOf/0/type',
714 absoluteKeywordLocation => 'http://localhost:4242#/anyOf/0/type',
715 error => 'got object, not string',
716 },
717 # /anyOf/1/additionalProperties/$dynamicRef/anyOf/1 with /foo
718 # additionalProperties: consider /foo/bar
719 # /anyOf/1/additionalProperties/$dynamicRef/anyOf/1/additionalProperties/$dynamicRef
720 # /anyOf/1/additionalProperties/$dynamicRef/anyOf/1/additionalProperties/$dynamicRef/anyOf/0 - is string, so no error
721 # /anyOf/1/additionalProperties/$dynamicRef/anyOf/1/additionalProperties/$dynamicRef/anyOf/1 - is object
722 # additionalProperties: consider /foo/baz
723 # is neither string or object
724 {
725 instanceLocation => '/foo/baz',
726 keywordLocation => '/anyOf/1/additionalProperties/$dynamicRef/anyOf/1/additionalProperties/$dynamicRef/anyOf/0/type',
727 absoluteKeywordLocation => 'http://localhost:4242#/anyOf/0/type',
728 error => 'got integer, not string',
729 },
730 {
731 instanceLocation => '/foo/baz',
732 keywordLocation => '/anyOf/1/additionalProperties/$dynamicRef/anyOf/1/additionalProperties/$dynamicRef/anyOf/1/type',
733 absoluteKeywordLocation => 'http://localhost:4242#/anyOf/1/type',
734 error => 'got integer, not object',
735 },
736 {
737 instanceLocation => '/foo/baz',
738 keywordLocation => '/anyOf/1/additionalProperties/$dynamicRef/anyOf/1/additionalProperties/$dynamicRef/anyOf',
739 absoluteKeywordLocation => 'http://localhost:4242#/anyOf',
740 error => 'no subschemas are valid',
741 },
742 # and now we start to unwind.
743 {
744 instanceLocation => '/foo',
745 keywordLocation => '/anyOf/1/additionalProperties/$dynamicRef/anyOf/1/additionalProperties',
746 absoluteKeywordLocation => 'http://localhost:4242#/anyOf/1/additionalProperties',
747 error => 'not all additional properties are valid',
748 },
749 {
750 instanceLocation => '/foo',
751 keywordLocation => '/anyOf/1/additionalProperties/$dynamicRef/anyOf',
752 absoluteKeywordLocation => 'http://localhost:4242#/anyOf',
753 error => 'no subschemas are valid',
754 },
755 {
756 instanceLocation => '',
757 keywordLocation => '/anyOf/1/additionalProperties',
758 absoluteKeywordLocation => 'http://localhost:4242#/anyOf/1/additionalProperties',
759 error => 'not all additional properties are valid',
760 },
761 {
762 instanceLocation => '',
763 keywordLocation => '/anyOf',
764 absoluteKeywordLocation => 'http://localhost:4242#/anyOf',
765 error => 'no subschemas are valid',
766 },
767 ],
768 },
769 '$dynamicRef without nested $dynamicAnchor behaves like $ref',
770 );
771 };
772
773 subtest '$recursiveRef without $dynamicAnchor behaves like $ref' => sub {
774 is_equal(
775 $js->evaluate(
776 { foo => { bar => 1 } },
777 {
778 properties => { foo => { '$dynamicRef' => '#' } },
779 additionalProperties => false,
780 },
781 )->TO_JSON,
782 {
783 valid => false,
784 errors => [
785 {
786 instanceLocation => '/foo/bar',
787 keywordLocation => '/properties/foo/$dynamicRef/additionalProperties',
788 absoluteKeywordLocation => '#/additionalProperties',
789 error => 'additional property not permitted',
790 },
791 {
792 instanceLocation => '/foo',
793 keywordLocation => '/properties/foo/$dynamicRef/additionalProperties',
794 absoluteKeywordLocation => '#/additionalProperties',
795 error => 'not all additional properties are valid',
796 },
797 {
798 instanceLocation => '',
799 keywordLocation => '/properties',
800 error => 'not all properties are valid',
801 },
802 ],
803 },
804 '$dynamicRef without $dynamicAnchor behaves like $ref',
805 );
806 };
807
808 subtest '$dynamicAnchor and $dynamicRef - standard usecases' => sub {
809 my $schema = {
810 '$id' => 'https://base.com',
811 #'$dynamicAnchor' => 'thingy', # adding this, and changing the $ref, will make us recurse here.
812 type => [ 'object', 'integer' ],
813 additionalProperties => {
814 '$id' => 'https://innerbase.com', # without $dynamicRef, we will recurse here.
815 #'$dynamicAnchor' => 'thingy',
816 type => [ 'object', 'boolean' ],
817 additionalProperties => {
818 '$ref' => '#', # if this was a $dynamicRef and there are $dynamicAnchors, we will go to base.
819 },
820 },
821 };
822
823 is_equal(
824 $js->evaluate({ foo => { bar => 1 } }, $schema)->TO_JSON,
825 {
826 valid => false,
827 errors => my $errors = [
828 {
829 instanceLocation => '/foo/bar',
830 keywordLocation => '/additionalProperties/additionalProperties/$ref/type',
831 absoluteKeywordLocation => 'https://innerbase.com#/type',
832 error => 'got integer, not one of object, boolean',
833 },
834 {
835 instanceLocation => '/foo',
836 keywordLocation => '/additionalProperties/additionalProperties',
837 absoluteKeywordLocation => 'https://innerbase.com#/additionalProperties',
838 error => 'not all additional properties are valid',
839 },
840 {
841 instanceLocation => '',
842 keywordLocation => '/additionalProperties',
843 absoluteKeywordLocation => 'https://base.com#/additionalProperties',
844 error => 'not all additional properties are valid',
845 },
846 ],
847 },
848 'validation requires the override that is not in scope',
849 );
850
851 # now make the ref a dynamicRef, but still won't recurse to base because no dynamicanchor.
852
853 $js->{_resource_index} = {};
854 $schema->{additionalProperties}{additionalProperties}{'$dynamicRef'} =
855 delete $schema->{additionalProperties}{additionalProperties}{'$ref'}; # '#'
856 $errors->[0]{keywordLocation} =~ s/ref/dynamicRef/;
857
858 is_equal(
859 $js->evaluate({ foo => { bar => 1 } }, $schema)->TO_JSON,
860 {
861 valid => false,
862 errors => $errors,
863 },
864 '$dynamicRef requires a $dynamicAnchor that does not exist',
865 );
866
867 # we still won't recurse to the base because $dynamicRef doesn't use the anchor URI.
868 $js->{_resource_index} = {};
869 $schema->{additionalProperties}{'$dynamicAnchor'} = 'thingy';
870
871 is_equal(
872 $js->evaluate({ foo => { bar => 1 } }, $schema)->TO_JSON,
873 {
874 valid => false,
875 errors => $errors,
876 },
877 '$dynamicRef must use a URI containing the dynamic anchor fragment',
878 );
879
880 # use the anchor URI for $dynamicRef, but we still won't recurse to the base because there is no
881 # outer $dynamicAnchor.
882 $js->{_resource_index} = {};
883 $schema->{additionalProperties}{additionalProperties}{'$dynamicRef'} = '#thingy';
884
885 is_equal(
886 $js->evaluate({ foo => { bar => 1 } }, $schema)->TO_JSON,
887 {
888 valid => false,
889 errors => $errors,
890 },
891 'there is no outer $dynamicAnchor in scope to recurse to',
892 );
893
894 # change $dynamicRef back to $ref, but use the fragment uri.
895 $js->{_resource_index} = {};
896 $schema->{additionalProperties}{additionalProperties}{'$ref'} =
897 delete $schema->{additionalProperties}{additionalProperties}{'$dynamicRef'}; # '#thingy'
898 $errors->[0]{keywordLocation} =~ s/dynamicRef/ref/;
899
900 is_equal(
901 $js->evaluate({ foo => { bar => 1 } }, $schema)->TO_JSON,
902 {
903 valid => false,
904 errors => $errors,
905 },
906 'we have an outer $dynamicAnchor, and are using the fragment URI, but we used $ref rather than $dynamicRef',
907 );
908
909 # now add a $dynamicAnchor to base, but we still won't recurse to the base because $dynamicRef
910 # doesn't use the anchor.
911 $js->{_resource_index} = {};
912 delete $schema->{additionalProperties}{additionalProperties}{'$ref'};
913 $schema->{additionalProperties}{additionalProperties}{'$dynamicRef'} = '#';
914 $schema->{'$dynamicAnchor'} = 'thingy';
915 $errors->[0]{keywordLocation} =~ s/ref/dynamicRef/;
916
917 is_equal(
918 $js->evaluate({ foo => { bar => 1 } }, $schema)->TO_JSON,
919 {
920 valid => false,
921 errors => $errors,
922 },
923 'there is an outer $dynamicAnchor in scope to recurse to, but $dynamicRef must use a URI containing the dynamic anchor fragment',
924 );
925
926 $js->{_resource_index} = {};
927 $schema->{additionalProperties}{additionalProperties}{'$dynamicRef'} = '#thingy';
928
929 is_equal(
930 $js->evaluate({ foo => { bar => 1 } }, $schema)->TO_JSON,
931 {
932 valid => true,
933 },
934 'now everything is in place to recurse to the base',
935 );
936
937 $js->{_resource_index} = {};
938 delete $schema->{additionalProperties}{'$dynamicAnchor'};
939 $schema->{additionalProperties}{additionalProperties}{'$dynamicRef'} = '#';
940 is_equal(
941 $js->evaluate({ foo => { bar => 1 } }, $schema)->TO_JSON,
942 {
943 valid => false,
944 errors => $errors,
945 },
946 'there is no $dynamicAnchor at the original target, and no anchor used in the target URI',
947 );
948 };
949
950 subtest '$dynamicRef to $dynamicAnchor not directly in the evaluation path' => sub {
951 $js->{_resource_index} = {};
952 my $schema = {
953 '$id' => 'base',
954 '$defs' => {
955 override => {
956 # this is in base uri 'base'
957 #'$dynamicAnchor' => 'thingy',
958 type => 'number',
959 },
960 start => {
961 '$id' => 'start',
962 '$defs' => {
963 main => {
964 # start#thingy
965 '$dynamicAnchor' => 'thingy',
966 type => 'string',
967 },
968 },
969 '$dynamicRef' => '#thingy', # -> start#thingy ( -> base#thingy ), when second anchor is in place
970 },
971 },
972 '$ref' => 'start',
973 };
974
975 is_equal(
976 $js->evaluate(42, $schema)->TO_JSON,
977 {
978 valid => false,
979 errors => my $errors = [
980 {
981 instanceLocation => '',
982 keywordLocation => '/$ref/$dynamicRef/type',
983 absoluteKeywordLocation => 'start#/$defs/main/type',
984 error => 'got integer, not string',
985 },
986 ],
987 },
988 'second dynamic anchor is not in the evaluation path, but we found it via dynamic scope - type does not match',
989 );
990
991 $js->{_resource_index} = {};
992 $schema->{'$defs'}{override}{'$anchor'} = 'thingy';
993
994 is_equal(
995 $js->evaluate(42, $schema)->TO_JSON,
996 {
997 valid => false,
998 errors => $errors,
999 },
1000 'regular $anchor in dynamic scope should not be used by $dynamicRef',
1001 );
1002
1003 $js->{_resource_index} = {};
1004 delete $schema->{'$defs'}{override}{'$anchor'};
1005 $schema->{'$defs'}{override}{'$dynamicAnchor'} = 'some_other_thingy';
1006
1007 is_equal(
1008 $js->evaluate(42, $schema)->TO_JSON,
1009 {
1010 valid => false,
1011 errors => $errors,
1012 },
1013 'some other $dynamicAnchor in dynamic scope should not be used by $dynamicRef',
1014 );
1015
1016 $js->{_resource_index} = {};
1017 $schema->{'$defs'}{override}{'$dynamicAnchor'} = 'thingy';
1018
1019 is_equal(
1020 $js->evaluate(42, $schema)->TO_JSON,
1021 {
1022 valid => true,
1023 },
1024 'second dynamic anchor is not in the evaluation path, but we found it via dynamic scope - type matches',
1025 );
1026
1027 $js->{_resource_index} = {};
1028 my $canonical_uri = delete $schema->{'$id'};
1029
1030 $js->add_schema($canonical_uri => $schema);
1031 is_equal(
1032 $js->evaluate(42, $canonical_uri)->TO_JSON,
1033 {
1034 valid => true,
1035 },
1036 'the first dynamic scope is set by document uri, not just the $id keyword',
1037 );
1038 };
1039
1040 subtest 'multiple layers in the dynamic scope' => sub {
1041 $js->{_resource_index} = {};
1042 my $schema = {
1043 # We $ref from base -> first#/$defs/stuff -> second#/$defs/stuff -> third#/$defs/stuff
1044 # and then follow a $dynamicRef to #length.
1045 # At no point do we ever actually evaluate at the root schema for each scope.
1046 # The dynamic scope is [ base, first, second, third ] and we check the scopes in order,
1047 # therefore the first scope we find with a dynamic anchor "length" is "second".
1048 '$id' => 'base',
1049 '$ref' => 'first#/$defs/stuff',
1050 '$defs' => {
1051 first => {
1052 '$id' => 'first',
1053 '$defs' => {
1054 stuff => { # first#/$defs/stuff
1055 '$ref' => 'second#/$defs/stuff',
1056 },
1057 length => { # first#length
1058 # no $dynamicAnchor here!
1059 maxLength => 1,
1060 },
1061 },
1062 },
1063 second => {
1064 '$id' => 'second',
1065 '$defs' => {
1066 stuff => { # second#/$defs/stuff
1067 '$ref' => 'third#/$defs/stuff',
1068 },
1069 length => { # second#length
1070 '$dynamicAnchor' => 'length',
1071 maxLength => 2, # <-- this is the scope that we should find and evaluate
1072 },
1073 },
1074 },
1075 third => {
1076 '$id' => 'third',
1077 '$defs' => {
1078 stuff => { # third#/$defs/stuff
1079 '$dynamicRef' => '#length',
1080 },
1081 length => { # third#length
1082 '$dynamicAnchor' => 'length',
1083 maxLength => 3, # this should never get evaluated
1084 }
1085 },
1086 },
1087 },
1088 };
1089 is_equal(
1090 $js->evaluate('hello', $schema)->TO_JSON,
1091 {
1092 valid => false,
1093 errors => [
1094 {
1095 instanceLocation => '',
1096 keywordLocation => '/$ref/$ref/$ref/$dynamicRef/maxLength',
1097 absoluteKeywordLocation => 'second#/$defs/length/maxLength',
1098 error => 'length is greater than 2',
1099 },
1100 ],
1101 },
1102 'dynamic scopes are pushed onto the stack even when its root resource (and $id keyword) are not directly evaluated',
1103 );
1104 };
1105
1106 subtest 'after leaving a dynamic scope, it should not be used by a $dynamicRef' => sub {
1107 $js->{_resource_index} = {};
1108 my $schema = {
1109 '$id' => 'main',
1110 if => {
1111 '$id' => 'first_scope',
1112 '$defs' => {
1113 thingy => {
1114 # this is first_scope#thingy
1115 '$dynamicAnchor' => 'thingy',
1116 type => 'number',
1117 },
1118 },
1119 },
1120 'then' => {
1121 '$id' => 'second_scope',
1122 '$ref' => 'start',
1123 '$defs' => {
1124 'thingy' => {
1125 # this is second_scope#thingy, the final destination of the $dynamicRef
1126 '$dynamicAnchor' => 'thingy',
1127 type => 'null',
1128 },
1129 },
1130 },
1131 '$defs' => {
1132 start => {
1133 # this is the landing spot from $ref
1134 '$id' => 'start',
1135 '$dynamicRef' => 'inner_scope#thingy',
1136 },
1137 'thingy' => {
1138 # this is the first stop by the $dynamicRef
1139 '$id' => 'inner_scope',
1140 '$dynamicAnchor' => 'thingy',
1141 type => 'string',
1142 }
1143 }
1144 };
1145
1146 is_equal(
1147 $js->evaluate(undef, $schema)->TO_JSON,
1148 {
1149 valid => true,
1150 },
1151 'first_scope is no longer in scope, so it is not used by $dynamicRef',
1152 );
1153 };
1154
1155 subtest 'anchors do not match' => sub {
1156 $js->{_resource_index} = {};
1157 my $schema = {
1158 '$defs' => {
1159 enhanced => {
1160 '$dynamicAnchor' => 'thingy', # change this to $anchor and watch what happens
1161 minimum => 2,
1162 },
1163 orig => {
1164 '$id' => 'orig',
1165 '$dynamicAnchor' => 'thingy',
1166 minimum => 10,
1167 },
1168 },
1169 '$dynamicRef' => 'orig#thingy',
1170 };
1171
1172 is_equal(
1173 $js->evaluate(1, $schema)->TO_JSON,
1174 {
1175 valid => false,
1176 errors => [
1177 {
1178 instanceLocation => '',
1179 keywordLocation => '/$dynamicRef/minimum',
1180 absoluteKeywordLocation => '#/$defs/enhanced/minimum',
1181 error => 'value is less than 2',
1182 },
1183 ],
1184 },
1185 '$dynamicRef goes to enhanced schema',
1186 );
1187
1188 $js->{_resource_index} = {};
1189 $schema->{'$defs'}{enhanced}{'$anchor'} = delete $schema->{'$defs'}{enhanced}{'$dynamicAnchor'};
1190 $schema->{'$defs'}{enhanced}{'$dynamicAnchor'} = 'somethingelse';
1191
1192 is_equal(
1193 $js->evaluate(1, $schema)->TO_JSON,
1194 {
1195 valid => false,
1196 errors => [
1197 {
1198 instanceLocation => '',
1199 keywordLocation => '/$dynamicRef/minimum',
1200 absoluteKeywordLocation => 'orig#/minimum',
1201 error => 'value is less than 10',
1202 },
1203 ],
1204 },
1205 '$dynamicRef -> $dynamicAnchor -> $anchor is a no go: we stay at the original schema',
1206 );
1207 };
1208
1209 subtest 'reference to a non-schema location' => sub {
1210 $js->{_resource_index} = {};
1211 my $schema = {
1212 '$defs' => {
1213 anchor => {
1214 '$dynamicAnchor' => 'my_anchor',
1215 '$dynamicRef' => 'http://example.com/otherdoc#/example/not_a_schema',
1216 },
1217 },
1218 type => 'object',
1219 properties => {
1220 '$ref' => {
1221 '$ref' => 'http://example.com/otherdoc#/example/not_a_schema',
1222 },
1223 '$dynamicRef' => {
1224 '$dynamicRef' => 'http://example.com/otherdoc#/example/not_a_schema',
1225 },
1226 },
1227 };
1228 $js->add_schema('http://example.com/mydoc', $schema);
1229 $js->add_schema({ '$id' => 'http://example.com/otherdoc', example => { not_a_schema => true } });
1230
1231 is_equal(
1232 $js->evaluate({ '$ref' => 1 }, $schema)->TO_JSON,
1233 {
1234 valid => false,
1235 errors => [
1236 {
1237 instanceLocation => '/$ref',
1238 keywordLocation => '/properties/$ref/$ref',
1239 error => 'EXCEPTION: bad reference to "http://example.com/otherdoc#/example/not_a_schema": not a schema',
1240 },
1241 ],
1242 },
1243 '$ref to a non-schema is not permitted',
1244 );
1245
1246 is_equal(
1247 $js->evaluate({ '$dynamicRef' => 1 }, '')->TO_JSON,
1248 {
1249 valid => false,
1250 errors => [
1251 {
1252 instanceLocation => '/$dynamicRef',
1253 keywordLocation => '/properties/$dynamicRef/$dynamicRef',
1254 error => 'EXCEPTION: bad reference to "http://example.com/otherdoc#/example/not_a_schema": not a schema',
1255 },
1256 ],
1257 },
1258 '$dynamicRef to a non-schema is not permitted',
1259 );
1260
1261 $js->{_resource_index} = {};
1262 $schema = {
1263 '$id' => '/foo',
1264 '$schema' => 'https://json-schema.org/draft/2019-09/schema',
1265 '$recursiveAnchor' => true,
1266 type => 'object',
1267 properties => {
1268 '$recursiveRef' => {
1269 '$recursiveRef' => 'http://example.com/otherdoc#/example/not_a_schema',
1270 },
1271 },
1272 };
1273
1274 $js->add_schema('http://example.com/mydoc', $schema);
1275 $js->add_schema({ '$id' => 'http://example.com/otherdoc', example => { not_a_schema => true } });
1276
1277 is_equal(
1278 $js->evaluate({ '$recursiveRef' => 1 }, $schema)->TO_JSON,
1279 {
1280 valid => false,
1281 errors => [
1282 {
1283 instanceLocation => '/$recursiveRef',
1284 keywordLocation => '/properties/$recursiveRef/$recursiveRef',
1285 absoluteKeywordLocation => '/foo#/properties/$recursiveRef/$recursiveRef',
1286 error => 'EXCEPTION: bad reference to "http://example.com/otherdoc#/example/not_a_schema": not a schema',
1287 },
1288 ],
1289 },
1290 '$recursiveRef to a non-schema is not permitted',
1291 );
1292
1293 package MyDocument {
1294 use strict; use warnings;
1295 use Moo;
1296 extends 'JSON::Schema::Modern::Document';
1297 use experimental 'signatures';
1298
1299 sub traverse ($self, @) {
1300 return {
1301 initial_schema_uri => $self->canonical_uri,
1302 errors => [],
1303 specification_version => 'draft2020-12',
1304 vocabularies => [],
1305 identifiers => {},
1306 subschemas => [],
1307 };
1308 }
1309 };
1310
1311 $js->{_resource_index} = {};
1312
1313 my $doc = MyDocument->new(
1314 schema => [ 'not a json schema' ],
1315 canonical_uri => 'https://my_non_schema',
1316 );
1317 $js->add_document($doc);
1318
1319 $schema = {
1320 '$id' => '/foo',
1321 '$schema' => 'https://my_non_schema',
1322 };
1323
1324 is_equal(
1325 $js->evaluate(1, $schema)->TO_JSON,
1326 {
1327 valid => false,
1328 errors => [
1329 {
1330 keywordLocation => '/$schema',
1331 # we haven't processed $id yet, so we don't know the absolute location
1332 error => 'EXCEPTION: bad reference to $schema "https://my_non_schema": not a schema',
1333 },
1334 ],
1335 },
1336 '$schema to a non-schema is not permitted',
1337 );
1338 };
1339
1340 subtest 'evaluate at a non-schema location' => sub {
1341 $js->{_resource_index} = {};
1342 $js->add_schema('http://my_schema', { example => { not_a_schema => true } });
1343
1344 is_equal(
1345 $js->evaluate(1, 'http://my_schema#/example/not_a_schema')->TO_JSON,
1346 {
1347 valid => false,
1348 errors => [
1349 {
1350 instanceLocation => '',
1351 keywordLocation => '',
1352 error => 'EXCEPTION: "http://my_schema#/example/not_a_schema" is not a schema',
1353 },
1354 ],
1355 },
1356 'evaluating at a non-schema location is not permitted',
1357 );
1358 };
1359
1360 subtest 'evaluate at a subschema, with $dynamicRef' => sub {
1361 # from a real bug I encountered while writing a t/parameters.t test in OpenAPI-Modern!
1362 # if we evaluate in the middle of a document, and a $dynamicRef is involved, mayhem ensues.
1363 $js->{_resource_index} = {};
1364 $js->add_schema({
1365 '$id' => 'http://strict_metaschema',
1366 '$defs' => {
1367 schema => {
1368 '$dynamicAnchor' => 'meta',
1369 type => 'object', # disallows boolean
1370 },
1371 parameter => {
1372 '$ref' => 'http://loose_metaschema#/$defs/parameter',
1373 },
1374 },
1375 '$ref' => 'http://loose_metaschema#/intentionally/bad/reference',
1376 });
1377
1378 $js->add_schema({
1379 '$id' => 'http://loose_metaschema',
1380 '$defs' => {
1381 schema => {
1382 '$dynamicAnchor' => 'meta',
1383 type => [ 'object', 'boolean' ],
1384 },
1385 parameter => {
1386 type => 'object',
1387 properties => {
1388 name => { type => 'string' },
1389 schema => { '$dynamicRef' => '#meta' },
1390 },
1391 },
1392 },
1393 });
1394
1395 is_equal(
1396 $js->evaluate(
1397 { name => 'hi', schema => false },
1398 'http://strict_metaschema#/$defs/parameter',
1399 )->TO_JSON,
1400 {
1401 valid => false,
1402 errors => [
1403 {
1404 instanceLocation => '/schema',
1405 keywordLocation => '/$ref/properties/schema/$dynamicRef/type',
1406 absoluteKeywordLocation => 'http://strict_metaschema#/$defs/schema/type',
1407 error => 'got boolean, not object',
1408 },
1409 {
1410 instanceLocation => '',
1411 keywordLocation => '/$ref/properties',
1412 absoluteKeywordLocation => 'http://loose_metaschema#/$defs/parameter/properties',
1413 error => 'not all properties are valid',
1414 },
1415 ],
1416 },
1417 'correctly navigated a $dynamicRef while evaluating in the middle of a document',
1418 );
1419 };
1420
1421 done_testing;