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   465003 use strict;
  9         23  
  9         303  
2 9     9   49 use warnings;
  9         21  
  9         479  
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.019';
8              
9 9     9   158 use 5.020; # for fc, unicode_strings features
  9         34  
10 9     9   48 use Moo;
  9         19  
  9         51  
11 9     9   3074 use strictures 2;
  9         52  
  9         291  
12 9     9   1457 use experimental qw(signatures postderef);
  9         20  
  9         60  
13 9     9   1373 use if "$]" >= 5.022, experimental => 're_strict';
  9         22  
  9         128  
14 9     9   730 no if "$]" >= 5.031009, feature => 'indirect';
  9         21  
  9         58  
15 9     9   470 no if "$]" >= 5.033001, feature => 'multidimensional';
  9         30  
  9         49  
16 9     9   489 no if "$]" >= 5.033006, feature => 'bareword_filehandles';
  9         35  
  9         52  
17 9     9   493 use JSON::Schema::Modern::Utilities 0.524 qw(assert_keyword_type annotate_self E is_type jsonp);
  9         169  
  9         576  
18 9     9   57 use namespace::clean;
  9         20  
  9         63  
19              
20             with 'JSON::Schema::Modern::Vocabulary';
21              
22             sub vocabulary {
23 47     47 0 51572 'https://spec.openapis.org/oas/3.1/vocab/base' => 'draft2020-12',
24             }
25              
26             sub keywords {
27 298     298 0 897623 qw(discriminator example externalDocs xml);
28             }
29              
30 12     12   164 sub _traverse_keyword_discriminator ($self, $schema, $state) {
  12         22  
  12         21  
  12         23  
  12         17  
31 12 100       41 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       392 if not exists $schema->{discriminator}{propertyName};
36             return E({ %$state, _schema_path_suffix => 'propertyName' }, 'discriminator propertyName is not a string')
37 10 100       28 if not is_type('string', $schema->{discriminator}{propertyName});
38              
39 9         211 my $valid = 1;
40 9 100       27 if (exists $schema->{discriminator}{mapping}) {
41 8 100       74 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       211 if not is_type('object', $schema->{discriminator}{mapping});
44 7         102 foreach my $mapping_key (sort keys $schema->{discriminator}{mapping}->%*) {
45 7         17 my $uri = $schema->{discriminator}{mapping}{$mapping_key};
46 7 100       17 $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       731 if not grep exists $schema->{$_}, qw(oneOf anyOf allOf);
52              
53 8         605 return 1;
54             }
55              
56 6     6   117 sub _eval_keyword_discriminator ($self, $data, $schema, $state) {
  6         14  
  6         10  
  6         13  
  6         12  
  6         11  
57             # Note: the spec is unclear of the expected behaviour when the data instance is not an object
58 6 50       18 return 1 if not is_type('object', $data);
59              
60 6         94 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         21 ->to_abs($state->{initial_schema_uri});
71 5 100 33     1436 if (my $component_schema_info = $state->{evaluator}->_fetch_from_uri($uri)) {
    100          
72 2         1572 $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         871 $uri = Mojo::URL->new($schema->{discriminator}{mapping}{$discriminator_value});
77 2         438 $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         436 return E($state, 'invalid %s: "%s"', $discriminator_key, $discriminator_value);
83             }
84              
85 4 100       24 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         73 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.019
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