File Coverage

blib/lib/JSON/Schema/Modern/Vocabulary/OpenAPI.pm
Criterion Covered Total %
statement 72 84 85.7
branch 24 26 92.3
condition 1 3 33.3
subroutine 16 22 72.7
pod 0 2 0.0
total 113 137 82.4


line stmt bran cond sub pod time code
1 9     9   478422 use strict;
  9         27  
  9         307  
2 9     9   50 use warnings;
  9         23  
  9         524  
3             package JSON::Schema::Modern::Vocabulary::OpenAPI;
4             # vim: set ts=8 sts=2 sw=2 tw=100 et :
5             # ABSTRACT: Implementation of the JSON Schema OpenAPI vocabulary
6              
7             our $VERSION = '0.020';
8              
9 9     9   193 use 5.020; # for fc, unicode_strings features
  9         34  
10 9     9   47 use Moo;
  9         17  
  9         76  
11 9     9   3208 use strictures 2;
  9         82  
  9         325  
12 9     9   1677 use experimental qw(signatures postderef);
  9         22  
  9         62  
13 9     9   1694 use if "$]" >= 5.022, experimental => 're_strict';
  9         31  
  9         128  
14 9     9   781 no if "$]" >= 5.031009, feature => 'indirect';
  9         35  
  9         57  
15 9     9   436 no if "$]" >= 5.033001, feature => 'multidimensional';
  9         21  
  9         56  
16 9     9   401 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  9         34  
  9         43  
17 9     9   508 use JSON::Schema::Modern::Utilities 0.524 qw(assert_keyword_type annotate_self E is_type jsonp);
  9         177  
  9         619  
18 9     9   61 use namespace::clean;
  9         28  
  9         74  
19              
20             with 'JSON::Schema::Modern::Vocabulary';
21              
22             sub vocabulary {
23 82     82 0 88812 'https://spec.openapis.org/oas/3.1/vocab/base' => 'draft2020-12',
24             }
25              
26             sub keywords {
27 441     441 0 1154213 qw(discriminator example externalDocs xml);
28             }
29              
30 12     12   148 sub _traverse_keyword_discriminator ($self, $schema, $state) {
  12         21  
  12         20  
  12         18  
  12         16  
31 12 100       38 return if not assert_keyword_type($state, $schema, 'object');
32              
33             # "the discriminator field MUST be a required field"
34             return E($state, 'missing required field propertyName')
35 11 100       316 if not exists $schema->{discriminator}{propertyName};
36             return E({ %$state, _schema_path_suffix => 'propertyName' }, 'discriminator propertyName is not a string')
37 10 100       50 if not is_type('string', $schema->{discriminator}{propertyName});
38              
39 9         179 my $valid = 1;
40 9 100       33 if (exists $schema->{discriminator}{mapping}) {
41 8 100       69 return if not assert_keyword_type({ %$state, _schema_path_suffix => 'mapping' }, $schema, 'object');
42             return E({ %$state, _schema_path_suffix => 'mapping' }, 'discriminator mapping is not an object ')
43 7 50       190 if not is_type('object', $schema->{discriminator}{mapping});
44 7         93 foreach my $mapping_key (sort keys $schema->{discriminator}{mapping}->%*) {
45 7         16 my $uri = $schema->{discriminator}{mapping}{$mapping_key};
46 7 100       19 $valid = E({ %$state, _schema_path_suffix => [ 'mapping', $mapping_key ] }, 'discriminator mapping value for "%s" is not a string', $mapping_key), next if not is_type('string', $uri);
47             }
48             }
49              
50             $valid = E($state, 'missing sibling keyword: one of oneOf, anyOf, allOf')
51 8 100       626 if not grep exists $schema->{$_}, qw(oneOf anyOf allOf);
52              
53 8         465 return 1;
54             }
55              
56 6     6   103 sub _eval_keyword_discriminator ($self, $data, $schema, $state) {
  6         13  
  6         9  
  6         12  
  6         13  
  6         10  
57             # Note: the spec is unclear of the expected behaviour when the data instance is not an object
58 6 50       19 return 1 if not is_type('object', $data);
59              
60 6         80 my $discriminator_key = $schema->{discriminator}{propertyName};
61              
62             # property with name <propertyName> MUST be present in the data payload
63             return E($state, 'missing required discriminator field "%s"', $discriminator_key)
64 6 100       24 if not exists $data->{$discriminator_key};
65              
66 5         12 my $discriminator_value = $data->{$discriminator_key};
67              
68             # if /components/$discriminator_value exists, that schema must validate
69             my $uri = Mojo::URL->new->fragment(jsonp('', qw(components schemas), $discriminator_value))
70 5         20 ->to_abs($state->{initial_schema_uri});
71 5 100 33     1155 if (my $component_schema_info = $state->{evaluator}->_fetch_from_uri($uri)) {
    100          
72 2         1269 $state = { %$state, _schema_path_suffix => 'propertyName' };
73             }
74             elsif (exists $schema->{discriminator}{mapping} and exists $schema->{discriminator}{mapping}{$discriminator_value}) {
75             # use 'mapping' to determine which schema to use.
76 2         762 $uri = Mojo::URL->new($schema->{discriminator}{mapping}{$discriminator_value});
77 2         360 $state = { %$state, _schema_path_suffix => [ 'mapping', $discriminator_value ] };
78             }
79             else {
80             # If the discriminator value does not match an implicit or explicit mapping, no schema can be
81             # determined and validation SHOULD fail.
82 1         392 return E($state, 'invalid %s: "%s"', $discriminator_key, $discriminator_value);
83             }
84              
85 4 100       33 return E($state, 'subschema for %s: %s is invalid', $discriminator_key, $discriminator_value)
86             if not $self->eval_subschema_at_uri($data, $schema, $state, $uri);
87 2         61 return 1;
88             }
89              
90 0     0     sub _traverse_keyword_example { 1 }
91              
92 0     0     sub _eval_keyword_example ($self, $data, $schema, $state) {
  0            
  0            
  0            
  0            
  0            
93 0           annotate_self($state, $schema);
94             }
95              
96             # until we do something with these values, we do not bother checking the structure
97 0     0     sub _traverse_keyword_externalDocs { 1 }
98              
99 0     0     sub _eval_keyword_externalDocs { goto \&_eval_keyword_example }
100              
101             # until we do something with these values, we do not bother checking the structure
102 0     0     sub _traverse_keyword_xml { 1 }
103              
104 0     0     sub _eval_keyword_xml { goto \&_eval_keyword_example }
105              
106             1;
107              
108             __END__
109              
110             =pod
111              
112             =encoding UTF-8
113              
114             =head1 NAME
115              
116             JSON::Schema::Modern::Vocabulary::OpenAPI - Implementation of the JSON Schema OpenAPI vocabulary
117              
118             =head1 VERSION
119              
120             version 0.020
121              
122             =head1 DESCRIPTION
123              
124             =for Pod::Coverage vocabulary keywords
125              
126             =for stopwords metaschema
127              
128             Implementation of the JSON Schema "OpenAPI" vocabulary, indicated in metaschemas
129             with the URI C<https://spec.openapis.org/oas/3.1/vocab/base> and formally specified in
130             L<https://spec.openapis.org/oas/v3.1.0#schema-object>.
131              
132             This vocabulary is normally made available by using the metaschema
133             L<https://spec.openapis.org/oas/3.1/dialect/base>.
134              
135             =head1 SUPPORT
136              
137             Bugs may be submitted through L<https://github.com/karenetheridge/JSON-Schema-Modern-Document-OpenAPI/issues>.
138              
139             I am also usually active on irc, as 'ether' at C<irc.perl.org> and C<irc.libera.chat>.
140              
141             You can also find me on the L<JSON Schema Slack server|https://json-schema.slack.com> and L<OpenAPI Slack
142             server|https://open-api.slack.com>, which are also great resources for finding help.
143              
144             =head1 AUTHOR
145              
146             Karen Etheridge <ether@cpan.org>
147              
148             =head1 COPYRIGHT AND LICENCE
149              
150             This software is copyright (c) 2021 by Karen Etheridge.
151              
152             This is free software; you can redistribute it and/or modify it under
153             the same terms as the Perl 5 programming language system itself.
154              
155             =cut