Validator/URIResolver.pm

filter: Statementstmt 93.9% 93 / 99 Branchbran 75.0% 27 / 36 Conditioncond 72.0% 18 / 25 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 9 totaltotal 80.7% 164 / 203 CC 31 SCAR 23.8
CC
31
Cov
86%
cache_id_dfs
20.8
new
8.2
cache_resolve
5.7
CRAP 10.8
« Validator/OAS30.pm ↑ Summary Validator/Draft4.pm »
1 package JSONSchema::Validator::URIResolver;
2  
3 # ABSTRACT: URI resolver
4  
5 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 12 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 12 ▶use JSONSchema::Validator::JSONPointer 'json_pointer';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use JSONSchema::Validator::Util qw(get_resource decode_content);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # what keys contain the schema? Required to find an $id in a schema
19 12 my $SEARCH_ID = {
20 value => {
21 additionalItems => 1,
22 items => 1,
23 additionalProperties => 1,
24 not => 1,
25 propertyNames => 1,
26 contains => 1,
27 if => 1,
28 then => 1,
29 else => 1
30 },
31 kv_value => {
32 properties => 1,
33 patternProperties => 1,
34 dependencies => 1,
35 definitions => 1
36 },
37 arr_value => {
38 items => 1,
39 allOf => 1,
40 anyOf => 1,
41 oneOf => 1
42 }
43 };
44  
45 321 ▶sub new {
Subroutine new: called CC=8 SCAR=21.0
Pod undocumented
46 321 my ($class, %params) = @_;
47  
48 321 ▶ croak 'URIResolver: validator must be specified' unless $params{validator};
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"validator"} 0 321 321
49 321 ▶ croak 'URIResolver: schema must be specified' unless defined $params{schema};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"schema"} 0 321 321
50  
51 321 my $validator = $params{validator};
52 321 my $schema = $params{schema};
53 321 ▶ my $base_uri = $params{base_uri} // '';
Condition50%1/2
$params{"base_uri"} // ""
l!l
3210
MC/DC0%0/1
$params{"base_uri"} // ""
$params{"base_uri"}
Truth table50%1/2
A
A= $params{"base_uri"}
Aresult
11
00
54  
55 321 ▶ my $scheme_handlers = $params{scheme_handlers} // {};
Condition50%1/2
$params{"scheme_handlers"} // {}
l!l
3210
MC/DC0%0/1
$params{"scheme_handlers"} // {}
$params{"scheme_handlers"}
Truth table50%1/2
A
A= $params{"scheme_handlers"}
Aresult
11
00
56  
57 321 weaken($validator);
58  
59 321 my $self = {
60 validator => $validator,
61 cache => {
62 $base_uri => $schema
63 },
64 scheme_handlers => $scheme_handlers
65 };
66  
67 321 bless $self, $class;
68  
69 321 ▶ if ('#' eq substr $base_uri, -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ("#" eq substr($base_uri, -1)) 21 300 321
70 21 $base_uri = substr $base_uri, 0, - 1;
71 21 $self->{cache}{$base_uri} = $schema;
72 }
73  
74 321 ▶ $self->cache_id(URI->new($base_uri), $schema) if $validator->using_id_with_ref && ref $schema eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $validator->using_id_with_ref and ref $schema eq "HASH" 280 41 321
Condition100%3/3
$validator->using_id_with_ref and ref $schema eq "HASH"
!ll&&!rl&&r
392280
MC/DC100%2/2
$validator->using_id_with_ref and ref $schema eq "HASH"
$validator->using_id_with_ref ref $schema eq "HASH"
Truth table100%3/3
A and B
A= $validator->using_id_with_refB= ref $schema eq "HASH"
ABresult
0X0
100
111
75  
76 321 return $self;
77 }
78  
79 5243 ▶sub validator { shift->{validator} }
Subroutine validator: called CC=1 SCAR=0.0
Pod undocumented
80 0 ▶sub scheme_handlers { shift->{scheme_handlers} }
Subroutine scheme_handlers: not called CC=1 SCAR=6.9
Pod undocumented
81 1381 ▶sub cache { shift->{cache} }
Subroutine cache: called CC=1 SCAR=0.0
Pod undocumented
82  
83 # self - URIResolver
84 # origin_uri - URI
85 # return (scope|string, schema)
86 454 ▶sub resolve {
Subroutine resolve: called CC=2 SCAR=6.9
Pod undocumented
87 454 my ($self, $origin_uri) = @_;
88  
89 454 ▶ return ($origin_uri->as_string, $self->cache->{$origin_uri->as_string}) if exists $self->cache->{$origin_uri->as_string};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->cache->{$origin_uri->as_string} 312 142 454
90  
91 142 my $uri = $origin_uri->clone;
92 142 $uri->fragment(undef);
93  
94 142 my $schema = $self->cache_resolve($uri);
95 142 return $self->fragment_resolve($origin_uri, $schema);
96 }
97  
98 # self - URIResolver
99 # uri - URI
100 # return schema
101 142 ▶sub cache_resolve {
Subroutine cache_resolve: called CC=3 SCAR=17.3
Pod undocumented
102 142 my ($self, $uri) = @_;
103  
104 142 my $scheme = $uri->scheme;
105  
106 142 ▶ return $self->cache->{$uri->as_string} if exists $self->cache->{$uri->as_string};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->cache->{$uri->as_string} 142 0 142
107  
108 0 my ($response, $mime_type) = get_resource($self->scheme_handlers, $uri->as_string);
109 0 my $schema = decode_content($response, $mime_type, $uri->as_string);
110  
111 0 $self->cache->{$uri->as_string} = $schema;
112  
113 0 ▶ $self->cache_id($uri, $schema) if $self->validator->using_id_with_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $self->validator->using_id_with_ref 0 0 0
114  
115 0 return $schema;
116 }
117  
118 # self - URIResolver
119 # uri - URI
120 # schema - HASH/ARRAY
121 # return (scope|string, schema)
122 142 ▶sub fragment_resolve {
Subroutine fragment_resolve: called CC=2 SCAR=6.9
Pod undocumented
123 142 my ($self, $uri, $schema) = @_;
124 142 ▶ return ($uri->as_string, $self->cache->{$uri->as_string}) if exists $self->cache->{$uri->as_string};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->cache->{$uri->as_string} 0 142 142
125  
126 142 my $enc = Encode::find_encoding("UTF-8");
127 142 my $fragment = $enc->decode(uri_unescape($uri->fragment), 1);
128  
129 142 my $pointer = json_pointer->new(
130 scope => $uri->as_string,
131 value => $schema,
132 validator => $self->validator
133 );
134  
135 # try to use fragment as json pointer
136 142 $pointer = $pointer->get($fragment);
137 142 my $subschema = $pointer->value;
138 142 my $current_scope = $pointer->scope;
139  
140 142 $self->cache->{$uri->as_string} = $subschema;
141  
142 142 return ($current_scope, $subschema);
143 }
144  
145 # self - URIResolver
146 # uri - URI
147 # schema - HASH/ARRAY
148 280 ▶sub cache_id {
Subroutine cache_id: called CC=1 SCAR=0.0
Pod undocumented
149 280 my ($self, $uri, $schema) = @_;
150  
151 # try to find id/$id and cache it to properly handle links in $ref
152 # https://json-schema.org/understanding-json-schema/structuring.html#using-id-with-ref
153  
154 280 my $scopes = [$uri];
155 280 $self->cache_id_dfs($schema, $scopes);
156 }
157  
158 # self - URIResolver
159 # schema - HASH/ARRAY
160 # scopes - [URI, ...]
161 3504 ▶sub cache_id_dfs {
Subroutine cache_id_dfs: called CC=20 SCAR=30.4
Pod undocumented
162 3504 my ($self, $schema, $scopes) = @_;
163 3504 ▶ return unless ref $schema eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $schema eq "HASH" 0 3504 3504
164  
165 # skip all fields (id field too) if $ref present (draft 4-7)
166 3504 ▶ return if exists $schema->{'$ref'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $schema->{"\$ref"} 1024 2480 3504
167  
168 2480 ▶ if (exists $schema->{$self->validator->ID_FIELD} && !ref $schema->{$self->validator->ID_FIELD}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$schema{$self->validator->ID_FIELD} and not ref $$schema{$self->validator->ID_FIELD}) 47 2433 2480
Condition66%2/3
exists $$schema{$self->validator->ID_FIELD} and not ref $$schema{$self->validator->ID_FIELD}
!ll&&!rl&&r
2433047
MC/DC50%1/2
exists $$schema{$self->validator->ID_FIELD} and not ref $$schema{$self->validator->ID_FIELD}
exists $$schema{$self->validator->ID_FIELD} not ref $$schema{$self->validator->ID_FIELD}
Truth table66%2/3
A and not(B)
A= exists $$schema{$self->validator->ID_FIELD}B= not ref $$schema{$self->validator->ID_FIELD}
ABresult
0X0
100
111
169 47 my $id = URI->new($schema->{$self->validator->ID_FIELD});
170 47 my $scope = $scopes->[-1];
171  
172 47 ▶ $id = ($scope && $scope->as_string) ? $id->abs($scope) : $id;
Branch: 1 branch
BranchTrueFalseTotal
$scope && $scope->as_string ? : 47 0 47
Condition33%1/3
$scope and $scope->as_string
!ll&&!rl&&r
0047
MC/DC0%0/2
$scope and $scope->as_string
$scope $scope->as_string
Truth table33%1/3
A and B
A= $scopeB= $scope->as_string
ABresult
0X0
100
111
173  
174 47 $self->cache->{$id->as_string} = $schema;
175 47 push @$scopes, $id;
176 }
177  
178 2480 for my $k (keys %$schema) {
179 4313 ▶ if ($SEARCH_ID->{value}{$k} && ref $schema->{$k} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($SEARCH_ID->{"value"}{$k} and ref $schema->{$k} eq "HASH") 405 3908 4313
Condition100%3/3
$SEARCH_ID->{"value"}{$k} and ref $schema->{$k} eq "HASH"
!ll&&!rl&&r
3730178405
MC/DC100%2/2
$SEARCH_ID->{"value"}{$k} and ref $schema->{$k} eq "HASH"
$SEARCH_ID->{"value"}{$k} ref $schema->{$k} eq "HASH"
Truth table100%3/3
A and B
A= $SEARCH_ID->{"value"}{$k}B= ref $schema->{$k} eq "HASH"
ABresult
0X0
100
111
180 405 $self->cache_id_dfs($schema->{$k}, $scopes);
181 }
182  
183 4313 ▶ if ($SEARCH_ID->{arr_value}{$k} && ref $schema->{$k} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($SEARCH_ID->{"arr_value"}{$k} and ref $schema->{$k} eq "ARRAY") 289 4024 4313
Condition100%3/3
$SEARCH_ID->{"arr_value"}{$k} and ref $schema->{$k} eq "ARRAY"
!ll&&!rl&&r
3862162289
MC/DC100%2/2
$SEARCH_ID->{"arr_value"}{$k} and ref $schema->{$k} eq "ARRAY"
$SEARCH_ID->{"arr_value"}{$k} ref $schema->{$k} eq "ARRAY"
Truth table100%3/3
A and B
A= $SEARCH_ID->{"arr_value"}{$k}B= ref $schema->{$k} eq "ARRAY"
ABresult
0X0
100
111
184 289 for my $value (@{$schema->{$k}}) {
185 609 ▶ next unless ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 0 609 609
186 609 $self->cache_id_dfs($value, $scopes);
187 }
188 }
189  
190 4313 ▶ if ($SEARCH_ID->{kv_value}{$k} && ref $schema->{$k} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($SEARCH_ID->{"kv_value"}{$k} and ref $schema->{$k} eq "HASH") 513 3800 4313
Condition66%2/3
$SEARCH_ID->{"kv_value"}{$k} and ref $schema->{$k} eq "HASH"
!ll&&!rl&&r
38000513
MC/DC50%1/2
$SEARCH_ID->{"kv_value"}{$k} and ref $schema->{$k} eq "HASH"
$SEARCH_ID->{"kv_value"}{$k} ref $schema->{$k} eq "HASH"
Truth table66%2/3
A and B
A= $SEARCH_ID->{"kv_value"}{$k}B= ref $schema->{$k} eq "HASH"
ABresult
0X0
100
111
191 513 for my $kv_key (keys %{$schema->{$k}}) {
192 2245 my $value = $schema->{$k}{$kv_key};
193 2245 ▶ next unless ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 35 2210 2245
194 2210 $self->cache_id_dfs($value, $scopes);
195 }
196 }
197 }
198  
199 2480 ▶ if (exists $schema->{$self->validator->ID_FIELD} && !ref $schema->{$self->validator->ID_FIELD}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$schema{$self->validator->ID_FIELD} and not ref $$schema{$self->validator->ID_FIELD}) 47 2433 2480
Condition66%2/3
exists $$schema{$self->validator->ID_FIELD} and not ref $$schema{$self->validator->ID_FIELD}
!ll&&!rl&&r
2433047
MC/DC50%1/2
exists $$schema{$self->validator->ID_FIELD} and not ref $$schema{$self->validator->ID_FIELD}
exists $$schema{$self->validator->ID_FIELD} not ref $$schema{$self->validator->ID_FIELD}
Truth table66%2/3
A and not(B)
A= exists $$schema{$self->validator->ID_FIELD}B= not ref $$schema{$self->validator->ID_FIELD}
ABresult
0X0
100
111
200 47 pop @$scopes;
201 }
202 }
203  
204 12 1;
205  
206 __END__
« Validator/OAS30.pm ↑ Summary Validator/Draft4.pm »