Debian Codegraph @

t/content-encoding.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 use utf8;
14
15 use lib 't/lib';
16 use Helper;
17 use Test2::Warnings qw(warnings :no_end_test had_no_warnings);
18
19 subtest 'unrecognized encoding formats do not result in errors, when not asserting' => sub {
20 my $js = JSON::Schema::Modern->new(collect_annotations => 1);
21
22 is_equal(
23 $js->evaluate(
24 'hello',
25 {
26 type => 'string',
27 contentEncoding => 'base64',
28 contentMediaType => 'image/png',
29 contentSchema => false,
30 },
31 )->TO_JSON,
32 {
33 valid => true,
34 annotations => [
35 {
36 instanceLocation => '',
37 keywordLocation => '/contentEncoding',
38 annotation => 'base64',
39 },
40 {
41 instanceLocation => '',
42 keywordLocation => '/contentMediaType',
43 annotation => 'image/png',
44 },
45 {
46 instanceLocation => '',
47 keywordLocation => '/contentSchema',
48 annotation => false,
49 },
50 ],
51 },
52 'in evaluate(), annotations are collected and no validation is performed',
53 );
54 };
55
56 subtest 'media_type and encoding handlers (legacy interfaces)' => sub {
57 my $js = JSON::Schema::Modern->new;
58
59 my @warnings = warnings {
60 cmp_result(
61 $js->get_media_type('application/json')->(\'{"alpha": "a string"}'),
62 \ { alpha => 'a string' },
63 'application/json media_type decoder',
64 );
65 };
66 cmp_result(
67 \@warnings,
68 [ re(qr/^\$jsm->get_media_type is deprecated; use the function in JSON::Schema::Modern::Utilities instead/) ],
69 'warned once when using deprecated get_media_type',
70 );
71
72 cmp_result($js->get_media_type('*/*'), undef, '*/* has no default match');
73
74 cmp_result($js->get_media_type('text/plain')->(\'foo'), \'foo', 'default text/plain media_type decoder');
75
76 cmp_result($js->get_media_type('tExt/PLaIN')->(\'foo'), \'foo', 'getter uses the casefolded name');
77
78 @warnings = warnings {
79 $js->add_media_type('furble/*' => sub { \1 });
80 };
81 cmp_result(
82 \@warnings,
83 [ re(qr/^\$jsm->add_media_type is deprecated; use the function in JSON::Schema::Modern::Utilities instead/) ],
84 'warned once when using deprecated add_media_type',
85 );
86
87 cmp_result(
88 $js->get_media_type('furble/bloop')->(\''),
89 \'1',
90 'deprecated getter matches to wildcard entries',
91 );
92 cmp_result(
93 JSON::Schema::Modern::Utilities::decode_media_type('furble/bloop', \''),
94 \'1',
95 'global decoder matches to wildcard entries',
96 );
97
98 $js->add_media_type('mytext/*' => sub { \'wildcard' });
99 cmp_result($js->get_media_type('myTExT/plain')->(\'foo'), \'wildcard', 'getter uses new override entry for wildcard');
100
101 $js->add_media_type('mytext/plain' => sub { \'plain' });
102 cmp_result($js->get_media_type('MYTExT/plain')->(\'foo'), \'plain', 'getter prefers case-insensitive matches to wildcard entries');
103 cmp_result($js->get_media_type('myTExT/blop')->(\'foo'), \'wildcard', 'getter matches to wildcard entries');
104 cmp_result($js->get_media_type('myTExT/*')->(\'foo'), \'wildcard', 'text/* matches itself');
105
106 $js->add_media_type('*/*' => sub { \'wildercard' });
107 cmp_result($js->get_media_type('myTExT/plain')->(\'foo'), \'plain', 'getter still prefers case-insensitive matches to wildcard entries');
108 cmp_result($js->get_media_type('myTExT/blop')->(\'foo'), \'wildcard', 'text/* is preferred to */*');
109 cmp_result($js->get_media_type('*/*')->(\'foo'), \'wildercard', '*/* matches */*, once defined');
110 cmp_result($js->get_media_type('fOO/bar')->(\'foo'), \'wildercard', '*/* is returned as a last resort');
111
112 cmp_result(
113 $js->get_media_type('application/x-www-form-urlencoded')->(\qq!\x{c3}\x{a9}clair=\x{e0}\x{b2}\x{a0}\x{5f}\x{e0}\x{b2}\x{a0}!),
114 \ { 'éclair' => 'ಠ_ಠ' },
115 'application/x-www-form-urlencoded happy path with unicode',
116 );
117
118 cmp_result(
119 $js->get_media_type('application/x-ndjson')->(\qq!{"foo":1,"bar":2}\n["a","b",3]\r\n"\x{e0}\x{b2}\x{a0}\x{5f}\x{e0}\x{b2}\x{a0}"!),
120 \ [ { foo => 1, bar => 2 }, [ 'a', 'b', 3 ], 'ಠ_ಠ' ],
121 'application/x-ndjson happy path with unicode',
122 );
123
124 like(
125 dies { $js->get_media_type('application/x-ndjson')->(\qq!{"foo":1,"bar":2}\n["a","b",]!) },
126 qr/^parse error at line 2: malformed JSON string/,
127 'application/x-ndjson dies with line number of the bad data',
128 );
129
130
131 my $js2 = JSON::Schema::Modern->new;
132
133 cmp_result(
134 $js2->get_media_type('furble/bloop')->(\''),
135 \'1',
136 'deprecated getter on second instance finds the right definition',
137 );
138
139 cmp_result(
140 $js2->get_media_type('fOO/bar')->(\''),
141 \'wildercard',
142 '*/* definition is visible to the second instance',
143 );
144
145 cmp_result(
146 JSON::Schema::Modern::Utilities::decode_media_type('myTExT/*', \'foo'),
147 \'wildcard',
148 'global decoder still exists',
149 );
150
151 JSON::Schema::Modern::Utilities::delete_media_type('furble/*');
152 JSON::Schema::Modern::Utilities::delete_media_type('*/*');
153 is($js->get_media_type('furble/bloop'), undef, 'media-type deletion is global');
154 is($js2->get_media_type('furble/bloop'), undef, 'media-type deletion is global');
155
156
157 # MIME::Base64::decode("eyJmb28iOiAiYmFyIn0K") -> {"foo": "bar"}
158 # Cpanel::JSON::XS->new->allow_nonref(1)->utf8(0)->decode(q!{"foo": "bar"}!) -> { foo => 'bar' }
159
160 cmp_result(
161 $js->get_media_type('application/json')->($js->get_encoding('base64')->(\'eyJmb28iOiAiYmFyIn0K')),
162 \ { foo => 'bar' },
163 'base64 encoding decoder + application/json media_type decoder',
164 );
165
166 cmp_result(
167 $js->get_media_type('application/json')->($js->get_encoding('base64url')->(\'eyJmb28iOiJiYXIifQ')),
168 \ { foo => 'bar' },
169 'base64url encoding decoder + application/json media_type decoder',
170 );
171
172
173 undef $js;
174 cmp_result(
175 scalar JSON::Schema::Modern::Utilities::decode_media_type('myTExT/*', \'foo'),
176 undef,
177 'deleted JSM instances delete their media-types',
178 );
179
180 is_equal(
181 $js2->get_media_type('fOO/bar'),
182 undef,
183 '*/* definition was deleted by the evaluator instance that added it',
184 );
185 };
186
187 subtest 'draft2020-12 assertions' => sub {
188 my $js = JSON::Schema::Modern->new;
189
190 is_equal(
191 $js->evaluate(
192 my $data = { encoded_object => 'eyJmb28iOiAiYmFyIn0K' },
193 my $schema = {
194 type => 'object',
195 properties => {
196 encoded_object => my $subschema = {
197 type => 'string',
198 contentEncoding => 'base64',
199 contentMediaType => 'application/json',
200 contentSchema => {
201 type => 'object',
202 properties => {
203 x => {
204 type => 'string',
205 default => 'default value',
206 },
207 },
208 additionalProperties => {
209 const => 'ಠ_ಠ',
210 },
211 },
212 },
213 },
214 },
215 )->TO_JSON,
216 { valid => true },
217 'under the current spec version, content* keywords are not assertions',
218 );
219
220 is_equal(
221 $js->evaluate(
222 { encoded_object => 'blur^p=' }, # invalid base64
223 $schema,
224 { validate_content_schemas => 1 },
225 )->TO_JSON,
226 {
227 valid => false,
228 errors => [
229 {
230 instanceLocation => '/encoded_object',
231 keywordLocation => '/properties/encoded_object/contentEncoding',
232 error => 'could not decode base64 string: invalid characters',
233 },
234 {
235 instanceLocation => '',
236 keywordLocation => '/properties',
237 error => 'not all properties are valid',
238 },
239 ],
240 },
241 'contentEncoding first decodes the string, erroring if it can\'t',
242 );
243
244 cmp_result(
245 $js->evaluate(
246 { encoded_object => 'bm90IGpzb24=' }, # base64-encoded "not json"
247 $schema,
248 { validate_content_schemas => 1 },
249 )->TO_JSON,
250 {
251 valid => false,
252 errors => [
253 {
254 instanceLocation => '/encoded_object',
255 keywordLocation => '/properties/encoded_object/contentMediaType',
256 error => re(qr{^could not decode application/json string: \'null\' expected, at character offset 0}),
257 },
258 {
259 instanceLocation => '',
260 keywordLocation => '/properties',
261 error => 'not all properties are valid',
262 },
263 ],
264 },
265 'then contentMediaType parses the decoded string, erroring if it can\'t, and does not continue with the schema',
266 );
267
268 is_equal(
269 $js->evaluate(
270 { encoded_object => 'eyJoaSI6MX0=' }, # base64-encoded, json-encoded { hi => 1 }
271 $schema,
272 { validate_content_schemas => 1 },
273 )->TO_JSON,
274 {
275 valid => false,
276 errors => [
277 {
278 instanceLocation => '/encoded_object/hi',
279 keywordLocation => '/properties/encoded_object/contentSchema/additionalProperties/const',
280 error => 'value does not match (wrong type: integer vs string)',
281 },
282 {
283 instanceLocation => '/encoded_object',
284 keywordLocation => '/properties/encoded_object/contentSchema/additionalProperties',
285 error => 'not all additional properties are valid',
286 },
287 {
288 instanceLocation => '/encoded_object',
289 keywordLocation => '/properties/encoded_object/contentSchema',
290 error => 'subschema is not valid',
291 },
292 {
293 instanceLocation => '',
294 keywordLocation => '/properties',
295 error => 'not all properties are valid',
296 },
297 ],
298 },
299 'contentSchema evaluates the decoded data',
300 );
301
302 is_equal(
303 $js->evaluate(
304 { encoded_object => 'bnVsbA==' }, # base64-encoded, json-encoded undef
305 $schema,
306 { validate_content_schemas => 1 },
307 )->TO_JSON,
308 {
309 valid => false,
310 errors => [
311 {
312 instanceLocation => '/encoded_object',
313 keywordLocation => '/properties/encoded_object/contentSchema/type',
314 error => 'got null, not object',
315 },
316 {
317 instanceLocation => '/encoded_object',
318 keywordLocation => '/properties/encoded_object/contentSchema',
319 error => 'subschema is not valid',
320 },
321 {
322 instanceLocation => '',
323 keywordLocation => '/properties',
324 error => 'not all properties are valid',
325 },
326 ],
327 },
328 'null data is handled properly',
329 );
330
331 is_equal(
332 (my $result = $js->evaluate(
333 { encoded_object => 'eyJoaSI6IuCyoF/gsqAifQ==' }, # base64-encoded, json-encoded { hi => "ಠ_ಠ" }
334 $schema,
335 {
336 validate_content_schemas => 1,
337 with_defaults => 1,
338 callbacks => {
339 type => sub ($self, $schema, $state) {
340 return 1 if $state->{data_path} ne '/properties/encoded_object';
341 is_equal(
342 $state->{data},
343 'eyJoaSI6IuCyoF/gsqAifQ==',
344 'before the content* keywords are evaluated, the instance data is still a string',
345 );
346 },
347 },
348 },
349 ))->TO_JSON,
350 {
351 valid => true,
352 defaults => {
353 '/encoded_object/x' => 'default value',
354 },
355 },
356 'contentSchema successfully evaluates the decoded data',
357 );
358
359 is_equal(
360 $result->data,
361 {
362 encoded_object => {
363 hi => 'ಠ_ಠ',
364 x => 'default value',
365 },
366 },
367 'result object contains the instance data with the encoded data fully deserialized',
368 );
369
370
371 is_equal(
372 ($result = $js->evaluate(
373 'eyJoaSI6IuCyoF/gsqAifQ==', # base64-encoded, json-encoded { hi => 'ಠ_ಠ' }
374 $subschema,
375 {
376 validate_content_schemas => 1,
377 callbacks => {
378 contentEncoding => sub ($self, $schema, $state) {
379 is_equal(
380 $state->{data},
381 qq!{"hi":"\xe0\xb2\xa0\x5f\xe0\xb2\xa0"}!,
382 'after contentEncoding, the instance data is a decoded string',
383 );
384 },
385 contentMediaType => sub ($self, $schema, $state) {
386 is_equal(
387 $state->{data},
388 { hi => 'ಠ_ಠ' },
389 'after contentMediaType, the instance data is a decoded object',
390 );
391 },
392 },
393 }))->TO_JSON,
394 { valid => true },
395 'decode and populate content into the top level of the result data',
396 );
397
398 is_equal(
399 $result->data,
400 { hi => 'ಠ_ಠ' },
401 'result object contains the instance data with the encoded data fully deserialized into he root',
402 );
403 };
404
405 subtest 'draft7 assertions' => sub {
406 my $js = JSON::Schema::Modern->new(specification_version => 'draft7');
407
408 is_equal(
409 $js->evaluate(
410 { encoded_object => 'blur^p=' }, # invalid base64
411 my $schema = {
412 type => 'object',
413 properties => {
414 encoded_object => {
415 contentEncoding => 'base64',
416 contentMediaType => 'application/json',
417 contentSchema => {
418 type => 'object',
419 additionalProperties => {
420 const => 'ಠ_ಠ',
421 },
422 },
423 },
424 },
425 },
426 )->TO_JSON,
427 {
428 valid => false,
429 errors => [
430 {
431 instanceLocation => '/encoded_object',
432 keywordLocation => '/properties/encoded_object/contentEncoding',
433 error => 'could not decode base64 string: invalid characters',
434 },
435 {
436 instanceLocation => '',
437 keywordLocation => '/properties',
438 error => 'not all properties are valid',
439 },
440 ],
441 },
442 'in draft7, assertion behaviour is the default',
443 );
444
445 cmp_result(
446 $js->evaluate(
447 { encoded_object => 'bm90IGpzb24=' }, # base64-encoded "not json"
448 $schema,
449 )->TO_JSON,
450 {
451 valid => false,
452 errors => [
453 {
454 instanceLocation => '/encoded_object',
455 keywordLocation => '/properties/encoded_object/contentMediaType',
456 error => re(qr{^could not decode application/json string: \'null\' expected, at character offset 0}),
457 },
458 {
459 instanceLocation => '',
460 keywordLocation => '/properties',
461 error => 'not all properties are valid',
462 },
463 ],
464 },
465 'in draft7, then contentMediaType parses the decoded string, erroring if it can\'t, and does not continue with the schema',
466 );
467
468 is_equal(
469 $js->evaluate(
470 { encoded_object => 'eyJoaSI6MX0=' }, # base64-encoded, json-encoded { hi => 1 }
471 $schema,
472 )->TO_JSON,
473 { valid => true },
474 'under draft7, content* are assertions by default, but contentSchema does not exist',
475 );
476 };
477
478 subtest 'more assertions' => sub {
479 my $js = JSON::Schema::Modern->new;
480
481 is_equal(
482 $js->evaluate(
483 'a string',
484 {
485 contentEncoding => 'whargarbl',
486 contentMediaType => 'whargarbl',
487 contentSchema => false,
488 },
489 {
490 validate_content_schemas => 1,
491 },
492 )->TO_JSON,
493 {
494 valid => false,
495 errors => [
496 {
497 instanceLocation => '',
498 keywordLocation => '/contentEncoding',
499 error => 'cannot find decoder for contentEncoding "whargarbl"',
500 },
501 ],
502 },
503 'evaluation aborts with an unrecognized contentEncoding',
504 );
505
506 is_equal(
507 $js->evaluate(
508 'a string',
509 {
510 contentMediaType => 'whargarbl',
511 contentSchema => false,
512 },
513 {
514 validate_content_schemas => 1,
515 },
516 )->TO_JSON,
517 {
518 valid => false,
519 errors => [
520 {
521 instanceLocation => '',
522 keywordLocation => '/contentMediaType',
523 error => 'cannot find decoder for contentMediaType "whargarbl"',
524 },
525 ],
526 },
527 'evaluation aborts with an unrecognized contentMediaType',
528 );
529 };
530
531 subtest 'use of an absolute URI and different dialect within contentSchema' => sub {
532 my $js = JSON::Schema::Modern->new(
533 validate_content_schemas => 1,
534 collect_annotations => 1,
535 );
536
537 $js->add_schema({
538 '$id' => 'https://my_metaschema',
539 '$schema' => 'https://json-schema.org/draft/2020-12/schema',
540 '$vocabulary' => {
541 'https://json-schema.org/draft/2020-12/vocab/core' => true,
542 'https://json-schema.org/draft/2020-12/vocab/validation' => true,
543 },
544 });
545
546 my $subschema;
547 is_equal(
548 $js->evaluate(
549 { foo => '{"bar":1}' },
550 {
551 '$id' => 'https://example.com',
552 additionalProperties => {
553 contentMediaType => 'application/json',
554 contentSchema => $subschema = {
555 '$id' => 'https://foo.com',
556 '$schema' => 'https://my_metaschema',
557 '$defs' => {
558 my_def => { type => 'object', blah => 1 },
559 },
560 '$ref' => '#/$defs/my_def',
561 bloop => 2,
562 properties => { bar => false }, # this keyword should only annotate
563 },
564 },
565 },
566 )->TO_JSON,
567 {
568 valid => true,
569 annotations => [
570 {
571 instanceLocation => '/foo',
572 keywordLocation => '/additionalProperties/contentMediaType',
573 absoluteKeywordLocation => 'https://example.com#/additionalProperties/contentMediaType',
574 annotation => 'application/json',
575 },
576 {
577 instanceLocation => '/foo',
578 keywordLocation => '/additionalProperties/contentSchema/$ref/blah',
579 absoluteKeywordLocation => 'https://foo.com#/$defs/my_def/blah',
580 annotation => 1,
581 },
582 {
583 instanceLocation => '/foo',
584 keywordLocation => '/additionalProperties/contentSchema/bloop',
585 absoluteKeywordLocation => 'https://foo.com#/bloop',
586 annotation => 2,
587 },
588 {
589 instanceLocation => '/foo',
590 keywordLocation => '/additionalProperties/contentSchema/properties',
591 absoluteKeywordLocation => 'https://foo.com#/properties',
592 annotation => { bar => false },
593 },
594 {
595 instanceLocation => '/foo',
596 keywordLocation => '/additionalProperties/contentSchema',
597 absoluteKeywordLocation => 'https://example.com#/additionalProperties/contentSchema',
598 annotation => $subschema,
599 },
600 {
601 instanceLocation => '',
602 keywordLocation => '/additionalProperties',
603 absoluteKeywordLocation => 'https://example.com#/additionalProperties',
604 annotation => [ 'foo' ],
605 },
606 ],
607 },
608 'evaluation of the subschema correctly uses the new $id and $schema',
609 );
610 };
611
612 had_no_warnings if $ENV{AUTHOR_TESTING};
613 done_testing;