JSON/Schema/Modern/Vocabulary/OpenAPI_3_0.pm

filter: Statementstmt 98.6% 74 / 75 Branchbran 100% 10 / 10 Conditioncond 46.1% 18 / 39 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 95.4% 21 / 22 Podpod 0.0% 0 / 2 totaltotal 78.0% 128 / 164 CC 16 SCAR 28.5
CC
16
Cov
82%
_eval_keyword_type
38.8
__ANON__
1.0
BEGIN
1.0
CRAP 17.4
« JSON/Schema/Modern/Vocabulary/OpenAPI.pm ↑ Summary OpenAPI/Modern/Utilities.pm »
1 4 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package JSON::Schema::Modern::Vocabulary::OpenAPI_3_0;
3 # vim: set ts=8 sts=2 sw=2 tw=100 et :
4 # ABSTRACT: Implementation of the JSON Schema OpenAPI 3.0 pseudo-vocabulary
5  
6 4 our $VERSION = '0.153';
7  
8 4 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use stable 0.031 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶no autovivification warn => qw(fetch store exists delete);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use if "$]" >= 5.022, experimental => 're_strict';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶no if "$]" >= 5.031009, feature => 'indirect';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶no if "$]" >= 5.033001, feature => 'multidimensional';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶no if "$]" >= 5.033006, feature => 'bareword_filehandles';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶no if "$]" >= 5.041009, feature => 'smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶no feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use JSON::Schema::Modern::Utilities qw(get_type E);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 4 with 'JSON::Schema::Modern::Vocabulary';
26  
27 25 ▶sub vocabulary {}
Subroutine vocabulary: called CC=1 SCAR=0.0
Pod undocumented
28  
29 4 my $vocabulary_mapping = {
30 Core => [ '$ref' ],
31 Validation => [ qw(
32 type
33 enum
34 multipleOf
35 maximum
36 exclusiveMaximum
37 minimum
38 exclusiveMinimum
39 maxLength
40 minLength
41 pattern
42 maxItems
43 minItems
44 uniqueItems
45 maxProperties
46 minProperties
47 required
48 ) ],
49 Applicator => [ qw(
50 allOf
51 anyOf
52 oneOf
53 not
54 items
55 properties
56 additionalProperties
57 ) ],
58 Format => [ 'format' ],
59 MetaData => [ qw(
60 title
61 description
62 default
63 deprecated
64 readOnly
65 writeOnly
66 example
67 ) ],
68 # unique to OAS; all of these have null implementations
69 Other => [ qw(
70 nullable
71 discriminator
72 externalDocs
73 ) ],
74 };
75  
76 2 ▶sub keywords {
Subroutine keywords: called CC=1 SCAR=0.0
Pod undocumented
77 # most of these can be directly mapped to JSON Schema vocabulary keywords (3.0.4 §4.7.24.1).
78 2 map @$_, $vocabulary_mapping->@{qw(Core Validation Applicator Format MetaData Other)};
79 }
80  
81  
82 # v3.0.4 §4.4: "Data types in the OAS are based on the non-null types supported by the JSON Schema
83 # Validation Specification Draft Wright-00: “boolean”, “object”, “array”, “number”, “string”, or
84 # “integer”. See nullable for an alternative solution to “null” as a type."
85 # v3.0.4 §4.7.24.1: ""items" MUST be present if type is "array"."
86 # "["type"] Value MUST be a string. Multiple types via an array are not supported."
87  
88 15 ▶sub _eval_keyword_type ($class, $data, $schema, $state) {
Subroutine _eval_keyword_type: called CC=16 SCAR=36.6
89 15 ▶ return 1 if not defined $data and $schema->{nullable};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $data and $$schema{"nullable"} 4 11 15
Condition100%3/3
not defined $data and $$schema{"nullable"}
!ll&&!rl&&r
834
MC/DC100%2/2
not defined $data and $$schema{"nullable"}
not defined $data $$schema{"nullable"}
Truth table100%3/3
not(A) and B
A= not defined $dataB= $$schema{"nullable"}
ABresult
0X0
100
111
90  
91 11 my $type = get_type($data, { legacy_ints => 1 });
92 11 my $want = $schema->{type};
93  
94 return 1 if
95 $type eq $want or ($want eq 'number' and $type eq 'integer')
96 or ($type eq 'string' and $state->{stringy_numbers} and looks_like_number($data)
97 and ($want eq 'number' or ($want eq 'integer' and $data == int($data))))
98 11 ▶ or ($want eq 'boolean' and $state->{scalarref_booleans} and $type eq 'reference to SCALAR');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq $want or $want eq "number" and $type eq "integer" or $type eq "string" and $$state{"stringy_numbers"} and &looks_like_number($data) and $want eq "number" || $want eq "integer" && $data == int $data or $want eq "boolean" and $$state{"scalarref_booleans"} and $type eq "reference to SCALAR" 6 5 11
Condition36%12/33
$type eq $want or $want eq "number" and $type eq "integer" or $type eq "string" and $$state{"stringy_numbers"} and &looks_like_number($data) and $want eq "number" || $want eq "integer" && $data == int $data or $want eq "boolean" and $$state{"scalarref_booleans"} and $type eq "reference to SCALAR"
l!l&&r!l&&!r
605
$type eq $want or $want eq "number" and $type eq "integer" or $type eq "string" and $$state{"stringy_numbers"} and &looks_like_number($data) and $want eq "number" || $want eq "integer" && $data == int $data
l!l&&r!l&&!r
605
$type eq $want or $want eq "number" and $type eq "integer"
l!l&&r!l&&!r
605
$want eq "number" and $type eq "integer"
!ll&&!rl&&r
500
$type eq "string" and $$state{"stringy_numbers"} and &looks_like_number($data) and $want eq "number" || $want eq "integer" && $data == int $data
!ll&&!rl&&r
500
$type eq "string" and $$state{"stringy_numbers"} and &looks_like_number($data)
!ll&&!rl&&r
500
$type eq "string" and $$state{"stringy_numbers"}
!ll&&!rl&&r
500
$want eq "number" or $want eq "integer" and $data == int $data
l!l&&r!l&&!r
000
$want eq "integer" and $data == int $data
!ll&&!rl&&r
000
$want eq "boolean" and $$state{"scalarref_booleans"} and $type eq "reference to SCALAR"
!ll&&!rl&&r
500
$want eq "boolean" and $$state{"scalarref_booleans"}
!ll&&!rl&&r
500
MC/DC8%1/12
$type eq $want or $want eq "number" and $type eq "integer" or $type eq "string" and $$state{"stringy_numbers"} and &looks_like_number($data) and $want eq "number" || $want eq "integer" && $data == int $data or $want eq "boolean" and $$state{"scalarref_booleans"} and $type eq "reference to SCALAR"
$type eq $want $want eq "number" $type eq "integer" $type eq "string" $$state{"stringy_numbers"} &looks_like_number($data) $want eq "number" $want eq "integer" $data == int $data $want eq "boolean" $$state{"scalarref_booleans"} $type eq "reference to SCALAR"
Truth table4%2/46
A or B and C or D and E and F and G or H and I or J and K and L
A= $type eq $wantB= $want eq "number"C= $type eq "integer"D= $type eq "string"E= $$state{"stringy_numbers"}F= &looks_like_number($data)G= $want eq "number"H= $want eq "integer"I= $data == int $dataJ= $want eq "boolean"K= $$state{"scalarref_booleans"}L= $type eq "reference to SCALAR"
ABCDEFGHIJKLresult
1XXXXXXXXXXX1
011XXXXXXXXX1
00X1111XXXXX1
00X111011XXX1
0101111XXXXX1
010111011XXX1
00X0XXXXX1111
00X10XXXX1111
00X110XXX1111
00X11100X1111
00X1110101111
0100XXXXX1111
01010XXXX1111
010110XXX1111
01011100X1111
0101110101111
00X0XXXXX0XX0
00X0XXXXX10X0
00X0XXXXX1100
00X10XXXX0XX0
00X10XXXX10X0
00X10XXXX1100
00X110XXX0XX0
00X110XXX10X0
00X110XXX1100
00X11100X0XX0
00X11100X10X0
00X11100X1100
00X1110100XX0
00X11101010X0
00X1110101100
0100XXXXX0XX0
0100XXXXX10X0
0100XXXXX1100
01010XXXX0XX0
01010XXXX10X0
01010XXXX1100
010110XXX0XX0
010110XXX10X0
010110XXX1100
01011100X0XX0
01011100X10X0
01011100X1100
0101110100XX0
01011101010X0
0101110101100
99  
100 5 ▶ return E($state, 'got %s, not %s%s', $type, $want, $schema->{nullable} ? ' or null' : '');
Branch: 1 branch
BranchTrueFalseTotal
$$schema{'nullable'} ? : 2 3 5
101 }
102  
103 4 foreach my $vocabulary (keys %$vocabulary_mapping) {
104 24 foreach my $keyword ($vocabulary_mapping->{$vocabulary}->@*) {
105  
106 # There is no need to rigorously traverse schemas in v3.0 OpenAPI documents, as there are no
107 # embedded identifiers to find ("id" is not a valid keyword).
108 # The document validation pass on the document that we already do is sufficient to check the
109 # schema keywords' syntax, and this saves us from having to implement custom methods for every
110 # keyword that uses a subset of normal draft4 syntax (see v3.0.4 §4.7.24.1).
111  
112 140 my $k = $keyword =~ s/^\$//r;
113  
114 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
115 0 ▶ *{__PACKAGE__.'::_traverse_keyword_'.$k} = sub { 1 };
Subroutine __ANON__: not called CC=1 SCAR=0.4
116  
117 140 ▶ next if $keyword eq 'type';
Branch: 1 branch
BranchTrueFalseTotal
if $keyword eq "type" 4 136 140
118  
119 # these keywords are no-ops
120 136 ▶ next if $vocabulary eq 'MetaData' or $vocabulary eq 'Other';
Branch: 1 branch
BranchTrueFalseTotal
if $vocabulary eq "MetaData" or $vocabulary eq "Other" 40 96 136
Condition100%3/3
$vocabulary eq "MetaData" or $vocabulary eq "Other"
l!l&&r!l&&!r
281296
MC/DC100%2/2
$vocabulary eq "MetaData" or $vocabulary eq "Other"
$vocabulary eq "MetaData" $vocabulary eq "Other"
Truth table100%3/3
A or B
A= $vocabulary eq "MetaData"B= $vocabulary eq "Other"
ABresult
1X1
011
000
121  
122 96 my $name = '_eval_keyword_'.$k;
123 96 *{__PACKAGE__.'::'.$name} = *{'JSON::Schema::Modern::Vocabulary::'.$vocabulary.'::'.$name};
124 }
125 }
126  
127 4 1;
128  
129 __END__
« JSON/Schema/Modern/Vocabulary/OpenAPI.pm ↑ Summary OpenAPI/Modern/Utilities.pm »