Debian Codegraph @

t/max_depth.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 open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8
14
15 use lib 't/lib';
16 use Helper;
17
18 my $js = JSON::Schema::Modern->new(max_depth => 6);
19
20 is_equal(
21 $js->evaluate(
22 [ [ [ [ [ 1 ] ] ] ] ],
23 {
24 items => { '$ref' => '#' },
25 },
26 )->TO_JSON,
27 {
28 valid => false,
29 errors => [
30 {
31 instanceLocation => '/0/0/0/0',
32 keywordLocation => '/items/$ref/items/$ref/items/$ref/items',
33 absoluteKeywordLocation => '#/items',
34 error => 'EXCEPTION: maximum evaluation depth (6) exceeded',
35 },
36 ],
37 },
38 'evaluation is halted when traversal gets too deep',
39 );
40
41 is_equal(
42 $js->evaluate(
43 1,
44 {
45 '$defs' => {
46 loop_a => {
47 '$ref' => '#/$defs/loop_b',
48 },
49 loop_b => {
50 '$ref' => '#/$defs/loop_a',
51 },
52 },
53 '$ref' => '#/$defs/loop_a',
54 },
55 )->TO_JSON,
56 {
57 valid => false,
58 errors => [
59 {
60 instanceLocation => '',
61 keywordLocation => '/$ref/$ref/$ref',
62 absoluteKeywordLocation => '#/$defs/loop_a',
63 error => 'EXCEPTION: infinite loop detected (same location evaluated twice)',
64 },
65 ],
66 },
67 'evaluation is halted when an instance location is evaluated against the same schema location a second time',
68 );
69
70 is_equal(
71 $js->evaluate(
72 { foo => 1 },
73 {
74 '$defs' => { mydef => { '$id' => '/properties/foo' } },
75 properties => {
76 foo => {
77 '$ref' => '/properties/foo',
78 },
79 },
80 },
81 )->TO_JSON,
82 { valid => true },
83 'the seen counter does not confuse URI paths and fragments: /properties/foo vs #/properties/foo',
84 );
85
86 is_equal(
87 $js->evaluate(
88 { foo => 1 },
89 {
90 '$defs' => {
91 int => { type => 'integer' },
92 },
93 anyOf => [
94 { additionalProperties => { '$ref' => '#/$defs/int' } },
95 { additionalProperties => { '$ref' => '#/$defs/int' } },
96 ],
97 }
98 )->TO_JSON,
99 { valid => true },
100 'the seen counter does not confuse two subschemas that both apply the same definition to the same instance location',
101 );
102
103 done_testing;