Hetzner/Schema.pm

filter: Statementstmt 96.3% 105 / 109 Branchbran 92.1% 35 / 38 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 84.6% 11 / 13 Podpod 100% 1 / 1 totaltotal 93.4% 158 / 169 CC 29 SCAR 27.3
CC
29
Cov
95%
_get_params
24.3
validate
6.0
__ANON__
2.0
CRAP 15.4
« Hetzner/APIBase.pm ↑ Summary Hetzner/API/Zones.pm »
1 package DNS::Hetzner::Schema;
2 25 $DNS::Hetzner::Schema::VERSION = '0.05';
3 # ABSTRACT: OpenAPI schema for the DNS API
4  
5 25 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 ▶use Mojo::Base -strict, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 25 ▶use JSON::Validator;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 25 ▶use JSON::Validator::Formats;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 25 ▶use List::Util qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 25 ▶use Mojo::JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 25 ▶use Mojo::Loader qw(data_section);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 25 ▶use Mojo::Util qw(camelize);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 25 ▶use constant IV_SIZE => eval 'require Config;$Config::Config{ivsize}';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 106 ▶sub validate ( $class, $operation, $params = {} ) {
Subroutine validate: called CC=6 SCAR=17.9
Pod documented
19 106 my ($spec, $validator) = _get_params($operation);
20  
21 my %check_params = map {
22 106 my $camelized = camelize $_;
23 129 $camelized =~ s{Id$}{ID};
24  
25 $spec->{param_names}->{$camelized} ?
26 ($camelized => $params->{$_}) :
27 129 ▶ ($_ => $params->{$_});
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'param_names'}{$camelized} ? : 19 110 129
28 } keys $params->%*;
29  
30 106 my @errors = $validator->validate(
31 \%check_params,
32 );
33  
34 106 ▶ if ( @errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 48 58 106
35 48 return ( undef, @errors );
36 }
37  
38 58 my %request_params;
39  
40 58 for my $param_name ( sort keys $spec->{param_names}->%* ) {
41 147 my $key = $spec->{param_names}->{$param_name};
42  
43 147 ▶ next if !$check_params{$param_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $check_params{$param_name} 78 69 147
44  
45 69 $request_params{$key}->{$param_name} = $check_params{$param_name};
46 }
47  
48 58 return \%request_params;
49 }
50  
51 106 ▶sub _get_params ($operation) {
Subroutine _get_params: called CC=24 SCAR=31.9
52 106 state %operation_params;
53  
54 106 my $op_data = $operation_params{$operation};
55 106 ▶ return $op_data->@* if $op_data;
Branch: 1 branch
BranchTrueFalseTotal
if $op_data 84 22 106
56  
57 22 my $api_spec = data_section(__PACKAGE__, 'openapi.json');
58 22 $api_spec =~ s{/components/schemas}{}g;
59  
60 22 my $data = decode_json( $api_spec );
61 22 my $schemas = $data->{components}->{schemas};
62  
63 22 my %paths = $data->{paths}->%*;
64  
65 22 my $op;
66  
67 22 for my $path ( keys %paths ) {
68  
69 METHOD:
70 220 for my $method_name ( keys $paths{$path}->%* ) {
71 660 ▶ next METHOD if 'HASH' ne ref $paths{$path}->{$method_name};
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" ne ref $paths{$path}{$method_name} 220 440 660
72  
73 440 my $method = $paths{$path}->{$method_name};
74 440 ▶ if ( $method->{operationId} && $method->{operationId} eq $operation ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method->{"operationId"} and $method->{"operationId"} eq $operation) 22 418 440
Condition66%2/3
$method->{"operationId"} and $method->{"operationId"} eq $operation
!ll&&!rl&&r
041822
MC/DC50%1/2
$method->{"operationId"} and $method->{"operationId"} eq $operation
$method->{"operationId"} $method->{"operationId"} eq $operation
Truth table66%2/3
A and B
A= $method->{"operationId"}B= $method->{"operationId"} eq $operation
ABresult
0X0
100
111
75 22 $op = $method;
76 }
77 }
78 }
79  
80 22 ▶ return {} if !$op;
Branch: 1 branch
BranchTrueFalseTotal
unless $op 0 22 22
81  
82 22 my $params = $op->{parameters};
83  
84 22 my %properties;
85 my @required;
86 22 my %param_names;
87  
88 PARAM:
89 22 for my $param ( $params->@* ) {
90 23 ▶ next PARAM if $param->{name} eq 'Auth-API-Token';
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"name"} eq "Auth-API-Token" 0 23 23
91  
92 23 my $name = $param->{name};
93  
94 23 ▶ if ( $param->{required} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"required"}) 11 12 23
95 11 push @required, $name;
96 }
97  
98 23 $param_names{$name} = $param->{in};
99 23 $properties{$name} = $param;
100 }
101  
102 22 my ($content_type, $body_required);
103 22 ▶ if ( $op->{requestBody} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->{"requestBody"}) 11 11 22
104 11 my $body = $op->{requestBody}->{content};
105 11 $body_required = $op->{requestBody}->{required};
106  
107 11 ($content_type) = sort keys $body->%*;
108  
109 11 ▶ if ( $content_type eq 'application/json' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content_type eq 'application/json') { } 9 2 11
elsif ($content_type eq 'text/plain') { } 2 0 2
110 9 my $schema = $body->{$content_type}->{schema};
111 9 ▶ my $prop = $schema->{properties} || {};
Condition100%2/2
$schema->{'properties'} || {}
l!l
27
MC/DC100%1/1
$schema->{'properties'} || {}
$schema->{'properties'}
Truth table100%2/2
A
A= $schema->{'properties'}
Aresult
11
00
112 9 for my $property ( keys $prop->%* ) {
113 2 $properties{$property} = delete $prop->{$property};
114 2 $param_names{$property} = 'body';
115 }
116  
117 9 ▶ if ( $schema->{'$ref'} ) { # '$ref' is not a typo. The key is named '$ref'!
Branch: 1 branch
BranchTrueFalseTotal
if ($schema->{"\$ref"}) 7 2 9
118 7 $properties{'$ref'} = $schema->{'$ref'};
119 }
120 }
121 elsif ( $content_type eq 'text/plain' ) {
122 2 $properties{text} = { type => "string" };
123 2 $param_names{text} = 'body';
124 }
125 }
126  
127 22 my $spec = {
128 type => 'object',
129 required => \@required,
130 body_required => $body_required,
131 properties => \%properties,
132 param_names => \%param_names,
133 content_type => $content_type,
134 $schemas->%*,
135 };
136  
137 22 my $validator = JSON::Validator->new->schema($spec);
138  
139 0 ▶ $validator->formats->{uint32} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 0 my ($sub) = JSON::Validator::Formats->can('_match_number');
141  
142 0 $sub->( unint32 => $_[0], 'L' );
143 22 };
144  
145 0 ▶ $validator->formats->{uint64} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
146 0 my ($sub) = JSON::Validator::Formats->can('_match_number');
147  
148 0 $sub->( unint32 => $_[0], IV_SIZE >= 8 ? 'Q' : '' );
149 22 };
150  
151 22 ▶ if ( $spec->{properties}->{'$ref'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"properties"}{"\$ref"}) 7 15 22
152 7 my @ref = $spec->{properties}->{'$ref'};
153 7 ▶ while ( my $ref = shift @ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = shift @ref) 18 7 25
154 18 $ref =~ s{^#}{};
155  
156 18 my $data = $validator->get( $ref );
157 18 ▶ if ( $data->{properties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"properties"}) 11 7 18
158 11 for my $property ( keys $data->{properties}->%* ) {
159 82 ▶ next if $data->{properties}->{$property}->{readOnly};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"properties"}{$property}{"readOnly"} 60 22 82
160  
161 22 $spec->{param_names}->{$property} = 'body';
162 }
163 }
164  
165 18 ▶ if ( $data->{allOf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"allOf"}) 11 7 18
166 11 push @ref, map{ $_->{'$ref'} } $data->{allOf}->@*;
167 11 ▶ if ( $data->{required} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"required"}) 7 4 11
168 7 push $spec->{required}->@*, $data->{required}->@*;
169 }
170 }
171 }
172 }
173  
174 22 $spec->{required}->@* = uniq $spec->{required}->@*;
175  
176 22 $operation_params{$operation} = [ $spec, $validator ];
177 22 return $operation_params{$operation}->@*;
178 }
179  
180 25 1;
181  
182 =pod
183  
184 =encoding UTF-8
185  
186 =head1 NAME
187  
188 DNS::Hetzner::Schema - OpenAPI schema for the DNS API
189  
190 =head1 VERSION
191  
192 version 0.05
193  
194 =head1 SYNOPSIS
195  
196 =head1 METHODS
197  
198 =head2 validate
199  
200 =head1 AUTHOR
201  
202 Renee Baecker <reneeb@cpan.org>
203  
204 =head1 COPYRIGHT AND LICENSE
205  
206 This software is Copyright (c) 2020 by Renee Baecker.
207  
208 This is free software, licensed under:
209  
210 The Artistic License 2.0 (GPL Compatible)
211  
212 =cut
213  
214 __DATA__
« Hetzner/APIBase.pm ↑ Summary Hetzner/API/Zones.pm »