JSON/Schema/Modern/Vocabulary/OpenAPI.pm

filter: Statementstmt 85.1% 80 / 94 Branchbran 58.7% 27 / 46 Conditioncond 70.5% 12 / 17 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 87.5% 21 / 24 Podpod 0.0% 0 / 2 totaltotal 74.2% 144 / 194 CC 34 SCAR 28.9
CC
34
Cov
76%
_traverse_keyword_xml
72.0
_traverse_keyword_externalDocs
20.0
_traverse_keyword_discriminator
16.4
CRAP 17.9
« OpenAPI/Modern.pm ↑ Summary JSON/Schema/Modern/Vocabulary/OpenAPI_3_0.pm »
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.153';
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 OpenAPI::Modern::Utilities 'elem';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 18 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 18 with 'JSON::Schema::Modern::Vocabulary';
26  
27 581 ▶sub vocabulary {
Subroutine vocabulary: called CC=1 SCAR=0.0
Pod undocumented
28 581 'https://spec.openapis.org/oas/3.1/vocab/base' => 'draft2020-12',
29 'https://spec.openapis.org/oas/3.2/vocab/base' => 'draft2020-12',
30 }
31  
32 1728 ▶sub keywords {
Subroutine keywords: called CC=1 SCAR=0.0
Pod undocumented
33 1728 qw(discriminator example externalDocs xml);
34 }
35  
36 18 ▶sub _traverse_keyword_discriminator ($self, $schema, $state) {
Subroutine _traverse_keyword_discriminator: called CC=16 SCAR=28.0
37 18 ▶ return if not assert_keyword_type($state, $schema, 'object');
Branch: 1 branch
BranchTrueFalseTotal
unless assert_keyword_type($state, $schema, "object") 2 16 18
38  
39 16 my $valid = 1;
40  
41 # "the discriminator field MUST be a required field"
42 $valid = E($state, 'missing required property "propertyName"')
43 16 ▶ if not exists $schema->{discriminator}{propertyName};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{"discriminator"}{"propertyName"} 5 11 16
44  
45 $valid = E({ %$state, _keyword_path_suffix => 'propertyName' }, 'discriminator propertyName is not a string')
46 16 ▶ if not is_type('string', $schema->{discriminator}{propertyName});
Branch: 1 branch
BranchTrueFalseTotal
unless is_type("string", $$schema{"discriminator"}{"propertyName"}) 7 9 16
47  
48 # defaultMapping was added in 3.2.0
49 $valid = E({ %$state, _keyword_path_suffix => 'defaultMapping' }, 'discriminator defaultMapping is not a string')
50 if exists $schema->{discriminator}{defaultMapping}
51 16 ▶ and not is_type('string', $schema->{discriminator}{defaultMapping});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$schema{"discriminator"}{"defaultMapping"} and not is_type("string", $$schema{"discriminator"}{"defaultMapping"}) 0 16 16
Condition66%2/3
exists $$schema{"discriminator"}{"defaultMapping"} and not is_type("string", $$schema{"discriminator"}{"defaultMapping"})
!ll&&!rl&&r
1420
MC/DC0%0/2
exists $$schema{"discriminator"}{"defaultMapping"} and not is_type("string", $$schema{"discriminator"}{"defaultMapping"})
exists $$schema{"discriminator"}{"defaultMapping"} not is_type("string", $$schema{"discriminator"}{"defaultMapping"})
Truth table66%2/3
A and not(B)
A= exists $$schema{"discriminator"}{"defaultMapping"}B= not is_type("string", $$schema{"discriminator"}{"defaultMapping"})
ABresult
0X0
100
111
52  
53 16 ▶ if (exists $schema->{discriminator}{mapping}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$schema{"discriminator"}{"mapping"}) 7 9 16
54 7 ▶ return if not assert_keyword_type({ %$state, _keyword_path_suffix => 'mapping' }, $schema, 'object');
Branch: 1 branch
BranchTrueFalseTotal
unless assert_keyword_type({%$state, "_keyword_path_suffix", "mapping"}, $schema, "object") 0 7 7
55 return E({ %$state, _keyword_path_suffix => 'mapping' }, 'discriminator mapping is not an object ')
56 7 ▶ if not is_type('object', $schema->{discriminator}{mapping});
Branch: 1 branch
BranchTrueFalseTotal
unless is_type("object", $$schema{"discriminator"}{"mapping"}) 2 5 7
57  
58 5 foreach my $mapping_key (sort keys $schema->{discriminator}{mapping}->%*) {
59 5 my $uri = $schema->{discriminator}{mapping}{$mapping_key};
60 5 ▶ $valid = E({ %$state, _keyword_path_suffix => [ 'mapping', $mapping_key ] }, 'discriminator mapping value for "%s" is not a string', $mapping_key)
Branch: 1 branch
BranchTrueFalseTotal
unless is_type("string", $uri) 2 3 5
61 if not is_type('string', $uri);
62 }
63 }
64  
65 # do our best to figure out what OAS version we're checking against
66 14 my ($oas_version) = $state->{metaschema_uri} =~ m{/oas/(\d+\.\d+)/}a;
67  
68 14 foreach my $keyword (keys $schema->{discriminator}->%*) {
69 18 ▶ next if $keyword =~ /^x-/
Branch: 2 branches
BranchTrueFalseTotal
if $keyword =~ /^x-/u or elem($keyword, [("propertyName", "mapping"), ($oas_version // "3.999") >= "3.2" ? "defaultMapping" : ()]) 15 3 18
($oas_version // 3.999) >= '3.2' ? : 12 6 18
Condition60%3/5
$keyword =~ /^x-/u or elem($keyword, [("propertyName", "mapping"), ($oas_version // "3.999") >= "3.2" ? "defaultMapping" : ()])
l!l&&r!l&&!r
0153
$oas_version // "3.999"
l!l
180
MC/DC50%1/2
$keyword =~ /^x-/u or elem($keyword, [("propertyName", "mapping"), ($oas_version // "3.999") >= "3.2" ? "defaultMapping" : ()])
$keyword =~ /^x-/u elem($keyword, [("propertyName", "mapping"), ($oas_version // "3.999") >= "3.2" ? "defaultMapping" : ()])
MC/DC0%0/1
$oas_version // "3.999"
$oas_version
Truth table66%2/3
A or B
A= $keyword =~ /^x-/uB= elem($keyword, [("propertyName", "mapping"), ($oas_version // "3.999") >= "3.2" ? "defaultMapping" : ()])
ABresult
1X1
011
000
Truth table50%1/2
A
A= $oas_version
Aresult
11
00
70 or elem($keyword, [qw(propertyName mapping), ($oas_version//3.999) >= '3.2' ? 'defaultMapping' : ()]);
71 3 $valid = E({ %$state, _keyword_path_suffix => $keyword }, 'unevaluated property is not permitted');
72 }
73  
74 14 return $valid;
75 }
76  
77 11 ▶sub _eval_keyword_discriminator ($self, $data, $schema, $state) {
Subroutine _eval_keyword_discriminator: called CC=9 SCAR=22.1
78 # Note: the spec is unclear of the expected behaviour when the data instance is not an object
79 11 ▶ return 1 if not is_type('object', $data);
Branch: 1 branch
BranchTrueFalseTotal
unless is_type("object", $data) 0 11 11
80  
81 # v3.1.2 §4.8.25.1: "This property SHOULD be required in the payload schema, as the behavior when
82 # the property is absent is undefined."
83 # v3.2.0 §4.25.1: "The discriminating property MAY be defined as required or optional, but when
84 # defined as optional the Discriminator Object MUST include a defaultMapping field that specifies
85 # which schema is expected to validate the structure of the model when the discriminating property
86 # is not present."
87 11 my $discriminator_key = $schema->{discriminator}{propertyName};
88 return E($state, 'missing required discriminator property "%s" and no defaultMapping present',
89 $discriminator_key)
90 11 ▶ if not exists $data->{$discriminator_key} and not exists $schema->{discriminator}{defaultMapping};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$data{$discriminator_key} or exists $$schema{"discriminator"}{"defaultMapping"} 3 8 11
Condition100%3/3
exists $$data{$discriminator_key} or exists $$schema{"discriminator"}{"defaultMapping"}
l!l&&r!l&&!r
713
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"}
ABresult
1X1
011
000
91  
92 8 ▶ my $discriminator_value = $data->{$discriminator_key} // $schema->{discriminator}{defaultMapping};
Condition66%2/3
$$data{$discriminator_key} // $$schema{"discriminator"}{"defaultMapping"}
l!l&&r!l&&!r
710
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"}
ABresult
1X1
011
000
93  
94 8 ▶ if (exists $schema->{discriminator}{mapping} and exists $schema->{discriminator}{mapping}{$discriminator_value}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $$schema{'discriminator'}{'mapping'} and exists $$schema{'discriminator'}{'mapping'}{$discriminator_value}) { } 3 5 8
elsif ($$state{'document'}->get_entity_at_location('/components/schemas/' . $discriminator_value) eq 'schema') { } 4 1 5
Condition66%2/3
exists $$schema{"discriminator"}{"mapping"} and exists $$schema{"discriminator"}{"mapping"}{$discriminator_value}
!ll&&!rl&&r
053
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}
ABresult
0X0
100
111
95 # use 'mapping' to determine which schema to use.
96 # Note that the spec uses an example that assumes that the mapping value can be appended to
97 # /components/schemas/, _as well as_ being treated as a uri-reference, but this is ambiguous.
98 # For now we will handle it by prepending '#/components/schemas' if it is not already a
99 # fragment-only uri reference.
100 3 my $mapping = $schema->{discriminator}{mapping}{$discriminator_value};
101  
102 return $self->eval_subschema_at_uri($data, $schema,
103 { %$state, keyword => $state->{keyword}.jsonp('', 'mapping', $discriminator_value) },
104 3 ▶ Mojo::URL->new($mapping =~ /^#/ ? $mapping : '#/components/schemas/'.$mapping)->to_abs($state->{initial_schema_uri}),
Branch: 1 branch
BranchTrueFalseTotal
$mapping =~ /^#/u ? : 2 1 3
105 );
106 }
107 elsif ($state->{document}->get_entity_at_location('/components/schemas/'.$discriminator_value) eq 'schema') {
108 return $self->eval_subschema_at_uri($data, $schema, { %$state, keyword => $state->{keyword}.'/propertyName' },
109 4 Mojo::URL->new->fragment(jsonp('/components/schemas', $discriminator_value))->to_abs($state->{initial_schema_uri}),
110 );
111 }
112 else {
113 # v3.2.0 §4.25.5, "Discriminator Property": If the discriminating value does not match an
114 # implicit or explicit mapping, no schema can be determined and validation SHOULD fail.
115 1 return E({ %$state, data_path => jsonp($state->{data_path}, $discriminator_key) },
116 'invalid %s: "%s"', $discriminator_key, $discriminator_value);
117 }
118 }
119  
120 0 ▶sub _traverse_keyword_example { 1 }
Subroutine _traverse_keyword_example: not called CC=1 SCAR=6.9
121  
122 0 ▶sub _traverse_keyword_externalDocs ($self, $schema, $state) {
Subroutine _traverse_keyword_externalDocs: not called CC=4 SCAR=30.0
123 0 ▶ return if not assert_keyword_type($state, $schema, 'object');
Branch: 1 branch
BranchTrueFalseTotal
unless assert_keyword_type($state, $schema, "object") 0 0 0
124  
125 0 ▶ ()= E($state, 'url is required', $state->{keyword}) if not exists $schema->{$state->{keyword}}{url};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$$state{"keyword"}}{"url"} 0 0 0
126 0 ▶ ()= E($state, 'url is not a string') if not is_type('string', $schema->{$state->{keyword}}{url});
Branch: 1 branch
BranchTrueFalseTotal
unless is_type("string", $$schema{$$state{"keyword"}}{"url"}) 0 0 0
127 0 1;
128 }
129  
130 0 ▶sub _traverse_keyword_xml ($self, $schema, $state) {
Subroutine _traverse_keyword_xml: not called CC=8 SCAR=42.8
131 0 ▶ return if not assert_keyword_type($state, $schema, 'object');
Branch: 1 branch
BranchTrueFalseTotal
unless assert_keyword_type($state, $schema, "object") 0 0 0
132  
133 0 my $valid = 1;
134  
135 0 foreach my $field (qw(nodeType name namespace prefix)) {
136 0 ▶ next if not exists $schema->{$state->{keyword}}{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$$state{"keyword"}}{$field} 0 0 0
137 $valid = E($state, '%s is not a string', $field)
138 0 ▶ if not is_type('string', $schema->{$state->{keyword}}{$field});
Branch: 1 branch
BranchTrueFalseTotal
unless is_type("string", $$schema{$$state{"keyword"}}{$field}) 0 0 0
139 }
140  
141 0 foreach my $field (qw(attribute wrapped)) {
142 0 ▶ next if not exists $schema->{$state->{keyword}}{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$$state{"keyword"}}{$field} 0 0 0
143 $valid = E($state, '%s is not a boolean', $field)
144 0 ▶ if not is_type('boolean', $schema->{$state->{keyword}}{$field});
Branch: 1 branch
BranchTrueFalseTotal
unless is_type("boolean", $$schema{$$state{"keyword"}}{$field}) 0 0 0
145 }
146  
147 0 return $valid;
148 }
149  
150 18 1;
151  
152 __END__
« OpenAPI/Modern.pm ↑ Summary JSON/Schema/Modern/Vocabulary/OpenAPI_3_0.pm »