File tree Expand file tree Collapse file tree 6 files changed +49
-3
lines changed
lib/openapi3_parser/node_factory
spec/lib/openapi3_parser/node_factory Expand file tree Collapse file tree 6 files changed +49
-3
lines changed Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ For OpenAPI 3.1
5757- [ ] Callbacks can now reference a PathItem - previously required them
5858- [ ] Check out whether pathItem references match the rules for relative resolution
5959- [ ] Parameter object can have space delimited or pipeDelimited styles
60- - [ ] Discriminator object can be extended
60+ - [x ] Discriminator object can be extended
6161- [x] mutualTLS as a security scheme
6262- [ ] I think strictness of Security Requirement rules has changed
6363
Original file line number Diff line number Diff line change 55module Openapi3Parser
66 module NodeFactory
77 class Discriminator < NodeFactory ::Object
8+ allow_extensions { |context | context . openapi_version >= "3.1" }
9+
810 field "propertyName" , input_type : String , required : true
911 field "mapping" , input_type : Hash ,
1012 validate : :validate_mapping ,
Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ class Object
1212 def_delegators "self.class" ,
1313 :field_configs ,
1414 :extension_regex ,
15+ :allowed_extensions? ,
1516 :mutually_exclusive_fields ,
1617 :allowed_default? ,
1718 :validations
Original file line number Diff line number Diff line change @@ -17,8 +17,21 @@ def field_configs
1717 @field_configs ||= { }
1818 end
1919
20- def allow_extensions ( regex : EXTENSION_REGEX )
20+ def allow_extensions ( regex : EXTENSION_REGEX , & block )
2121 @extension_regex = regex
22+ @allowed_extensions = block || true
23+ end
24+
25+ def allowed_extensions? ( context )
26+ @allowed_extensions ||= nil
27+
28+ allowed = if @allowed_extensions . respond_to? ( :call )
29+ @allowed_extensions . call ( context )
30+ else
31+ @allowed_extensions
32+ end
33+
34+ !!allowed
2235 end
2336
2437 def extension_regex
Original file line number Diff line number Diff line change @@ -39,9 +39,11 @@ def check_required_fields
3939 end
4040
4141 def check_unexpected_fields
42+ extension_regex = factory . extension_regex if factory . allowed_extensions? ( validatable . context )
43+
4244 Validators ::UnexpectedFields . call (
4345 validatable ,
44- extension_regex : factory . extension_regex ,
46+ extension_regex : extension_regex ,
4547 allowed_fields : factory . allowed_fields ,
4648 raise_on_invalid : raise_on_invalid
4749 )
Original file line number Diff line number Diff line change 99 }
1010 end
1111 end
12+
13+ describe "allow extensions" do
14+ it "accepts extensions for OpenAPI 3.1" do
15+ factory_context = create_node_factory_context (
16+ {
17+ "propertyName" => "test" ,
18+ "x-extension" => "value"
19+ } ,
20+ document_input : { "openapi" => "3.1.0" }
21+ )
22+
23+ instance = described_class . new ( factory_context )
24+ expect ( instance ) . to be_valid
25+ end
26+
27+ it "rejects extensions for OpenAPI < 3.1" do
28+ factory_context = create_node_factory_context (
29+ {
30+ "propertyName" => "test" ,
31+ "x-extension" => "value"
32+ } ,
33+ document_input : { "openapi" => "3.0.0" }
34+ )
35+
36+ instance = described_class . new ( factory_context )
37+ expect ( instance ) . not_to be_valid
38+ end
39+ end
1240end
You can’t perform that action at this time.
0 commit comments