| 1 | 18 | ▶ | use strictures 2; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 2 | package JSON::Schema::Modern::Vocabulary::OpenAPI; | ||||||||||||||||||||||||||||||||
| 3 | # vim: set ts=8 sts=2 sw=2 tw=100 et : | ||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Implementation of the JSON Schema OpenAPI vocabulary | ||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | 18 | our $VERSION = '0.151'; | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 18 | ▶ | use 5.020; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 18 | ▶ | use utf8; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 18 | ▶ | use Moo; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 18 | ▶ | use strictures 2; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 18 | ▶ | use stable 0.031 'postderef'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 18 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 18 | ▶ | no autovivification warn => qw(fetch store exists delete); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 18 | ▶ | use if "$]" >= 5.022, experimental => 're_strict'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 18 | ▶ | no if "$]" >= 5.031009, feature => 'indirect'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 18 | ▶ | no if "$]" >= 5.033001, feature => 'multidimensional'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 18 | ▶ | no if "$]" >= 5.033006, feature => 'bareword_filehandles'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 18 | ▶ | no if "$]" >= 5.041009, feature => 'smartmatch'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | 18 | ▶ | no feature 'switch'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | 18 | ▶ | use JSON::Schema::Modern::Utilities qw(assert_keyword_type annotate_self E is_type jsonp); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 22 | 18 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | 18 | with 'JSON::Schema::Modern::Vocabulary'; | |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 570 | ▶ | sub vocabulary { | ||||||||||||||||||||||||||||||
|
Subroutine
vocabulary: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 27 | 570 | 'https://spec.openapis.org/oas/3.1/vocab/base' => 'draft2020-12', | |||||||||||||||||||||||||||||||
| 28 | 'https://spec.openapis.org/oas/3.2/vocab/base' => 'draft2020-12', | ||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | 1696 | ▶ | sub keywords { | ||||||||||||||||||||||||||||||
|
Subroutine
keywords: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 32 | 1696 | qw(discriminator example externalDocs xml); | |||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 15 | ▶ | sub _traverse_keyword_discriminator ($self, $schema, $state) { | ||||||||||||||||||||||||||||||
|
Subroutine
_traverse_keyword_discriminator: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||
| 36 | 15 | ▶ | return if not assert_keyword_type($state, $schema, 'object'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | # "the discriminator field MUST be a required field" | ||||||||||||||||||||||||||||||||
| 39 | return E($state, 'missing required property "propertyName"') | ||||||||||||||||||||||||||||||||
| 40 | 13 | ▶ | if not exists $schema->{discriminator}{propertyName}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 41 | return E({ %$state, _keyword_path_suffix => 'propertyName' }, 'discriminator propertyName is not a string') | ||||||||||||||||||||||||||||||||
| 42 | 11 | ▶ | if not is_type('string', $schema->{discriminator}{propertyName}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 9 | my $valid = 1; | |||||||||||||||||||||||||||||||
| 45 | 9 | ▶ | if (exists $schema->{discriminator}{mapping}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 46 | 7 | ▶ | return if not assert_keyword_type({ %$state, _keyword_path_suffix => 'mapping' }, $schema, 'object'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 47 | return E({ %$state, _keyword_path_suffix => 'mapping' }, 'discriminator mapping is not an object ') | ||||||||||||||||||||||||||||||||
| 48 | 7 | ▶ | if not is_type('object', $schema->{discriminator}{mapping}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | 5 | foreach my $mapping_key (sort keys $schema->{discriminator}{mapping}->%*) { | |||||||||||||||||||||||||||||||
| 50 | 5 | my $uri = $schema->{discriminator}{mapping}{$mapping_key}; | |||||||||||||||||||||||||||||||
| 51 | 5 | ▶ | $valid = E({ %$state, _keyword_path_suffix => [ 'mapping', $mapping_key ] }, 'discriminator mapping value for "%s" is not a string', $mapping_key), next if not is_type('string', $uri); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 7 | return $valid; | |||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 11 | ▶ | sub _eval_keyword_discriminator ($self, $data, $schema, $state) { | ||||||||||||||||||||||||||||||
|
Subroutine
_eval_keyword_discriminator: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||||||
| 59 | # Note: the spec is unclear of the expected behaviour when the data instance is not an object | ||||||||||||||||||||||||||||||||
| 60 | 11 | ▶ | return 1 if not is_type('object', $data); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | # v3.1.2 §4.8.25.1: "This property SHOULD be required in the payload schema, as the behavior when | ||||||||||||||||||||||||||||||||
| 63 | # the property is absent is undefined." | ||||||||||||||||||||||||||||||||
| 64 | # v3.2.0 §4.25.1: "The discriminating property MAY be defined as required or optional, but when | ||||||||||||||||||||||||||||||||
| 65 | # defined as optional the Discriminator Object MUST include a defaultMapping field that specifies | ||||||||||||||||||||||||||||||||
| 66 | # which schema is expected to validate the structure of the model when the discriminating property | ||||||||||||||||||||||||||||||||
| 67 | # is not present." | ||||||||||||||||||||||||||||||||
| 68 | 11 | my $discriminator_key = $schema->{discriminator}{propertyName}; | |||||||||||||||||||||||||||||||
| 69 | return E($state, 'missing required discriminator property "%s" and no defaultMapping present', | ||||||||||||||||||||||||||||||||
| 70 | $discriminator_key) | ||||||||||||||||||||||||||||||||
| 71 | 11 | ▶ | if not exists $data->{$discriminator_key} and not exists $schema->{discriminator}{defaultMapping}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $$data{$discriminator_key} or exists $$schema{"discriminator"}{"defaultMapping"}
MC/DC100%2/2
exists $$data{$discriminator_key} or exists $$schema{"discriminator"}{"defaultMapping"}
exists $$data{$discriminator_key}
exists $$schema{"discriminator"}{"defaultMapping"}
Truth table100%3/3
A or B
A= exists $$data{$discriminator_key}B= exists $$schema{"discriminator"}{"defaultMapping"}
| |||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 8 | ▶ | my $discriminator_value = $data->{$discriminator_key} // $schema->{discriminator}{defaultMapping}; | ||||||||||||||||||||||||||||||
|
Condition66%2/3
$$data{$discriminator_key} // $$schema{"discriminator"}{"defaultMapping"}
MC/DC0%0/2
$$data{$discriminator_key} // $$schema{"discriminator"}{"defaultMapping"}
$$data{$discriminator_key}
$$schema{"discriminator"}{"defaultMapping"}
Truth table66%2/3
A // B
A= $$data{$discriminator_key}B= $$schema{"discriminator"}{"defaultMapping"}
| |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 8 | ▶ | if (exists $schema->{discriminator}{mapping} and exists $schema->{discriminator}{mapping}{$discriminator_value}) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
exists $$schema{"discriminator"}{"mapping"} and exists $$schema{"discriminator"}{"mapping"}{$discriminator_value}
MC/DC50%1/2
exists $$schema{"discriminator"}{"mapping"} and exists $$schema{"discriminator"}{"mapping"}{$discriminator_value}
exists $$schema{"discriminator"}{"mapping"}
exists $$schema{"discriminator"}{"mapping"}{$discriminator_value}
Truth table66%2/3
A and B
A= exists $$schema{"discriminator"}{"mapping"}B= exists $$schema{"discriminator"}{"mapping"}{$discriminator_value}
| |||||||||||||||||||||||||||||||||
| 76 | # use 'mapping' to determine which schema to use. | ||||||||||||||||||||||||||||||||
| 77 | # Note that the spec uses an example that assumes that the mapping value can be appended to | ||||||||||||||||||||||||||||||||
| 78 | # /components/schemas/, _as well as_ being treated as a uri-reference, but this is ambiguous. | ||||||||||||||||||||||||||||||||
| 79 | # For now we will handle it by prepending '#/components/schemas' if it is not already a | ||||||||||||||||||||||||||||||||
| 80 | # fragment-only uri reference. | ||||||||||||||||||||||||||||||||
| 81 | 3 | my $mapping = $schema->{discriminator}{mapping}{$discriminator_value}; | |||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | return $self->eval_subschema_at_uri($data, $schema, | ||||||||||||||||||||||||||||||||
| 84 | { %$state, keyword => $state->{keyword}.jsonp('', 'mapping', $discriminator_value) }, | ||||||||||||||||||||||||||||||||
| 85 | 3 | ▶ | Mojo::URL->new($mapping =~ /^#/ ? $mapping : '#/components/schemas/'.$mapping)->to_abs($state->{initial_schema_uri}), | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 86 | ); | ||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||
| 88 | elsif ($state->{document}->get_entity_at_location('/components/schemas/'.$discriminator_value) eq 'schema') { | ||||||||||||||||||||||||||||||||
| 89 | return $self->eval_subschema_at_uri($data, $schema, { %$state, keyword => $state->{keyword}.'/propertyName' }, | ||||||||||||||||||||||||||||||||
| 90 | 4 | Mojo::URL->new->fragment(jsonp('/components/schemas', $discriminator_value))->to_abs($state->{initial_schema_uri}), | |||||||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | else { | ||||||||||||||||||||||||||||||||
| 94 | # v3.2.0 §4.25.5, "Discriminator Property": If the discriminating value does not match an | ||||||||||||||||||||||||||||||||
| 95 | # implicit or explicit mapping, no schema can be determined and validation SHOULD fail. | ||||||||||||||||||||||||||||||||
| 96 | 1 | return E({ %$state, data_path => jsonp($state->{data_path}, $discriminator_key) }, | |||||||||||||||||||||||||||||||
| 97 | 'invalid %s: "%s"', $discriminator_key, $discriminator_value); | ||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub _traverse_keyword_example { 1 } | ||||||||||||||||||||||||||||||
|
Subroutine
_traverse_keyword_example: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | sub _traverse_keyword_externalDocs ($self, $schema, $state) { | ||||||||||||||||||||||||||||||
|
Subroutine
_traverse_keyword_externalDocs: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | return if not assert_keyword_type($state, $schema, 'object'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | ()= E($state, 'url is required', $state->{keyword}) if not exists $schema->{$state->{keyword}}{url}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | ()= E($state, 'url is not a string') if not is_type('string', $schema->{$state->{keyword}}{url}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 108 | 0 | 1; | |||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub _traverse_keyword_xml ($self, $schema, $state) { | ||||||||||||||||||||||||||||||
|
Subroutine
_traverse_keyword_xml: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | return if not assert_keyword_type($state, $schema, 'object'); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | 0 | foreach my $field (qw(nodeType name namespace prefix)) { | |||||||||||||||||||||||||||||||
| 115 | ()= E($state, '%s is not a string', $field) | ||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if not is_type('string', $schema->{$state->{keyword}}{$field}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 0 | foreach my $field (qw(attribute wrapped)) { | |||||||||||||||||||||||||||||||
| 120 | ()= E($state, '%s is not a boolean', $field) | ||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if not is_type('boolean', $schema->{$state->{keyword}}{$field}); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 18 | 1; | |||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||||||||||||