Skip to content

Commit 63c2115

Browse files
committed
Version specific allow_extensions for Discriminator
A change in OpenAPI 3.1 is that the Discriminator object can accept extensions, when previously this was not allowed. In order to code this I've had to adjust the DSL so that the allow_extensions method can accept a block. We didn't have unit tests for the DSL so I'm only testing this at the node level. This should be ok as it has full coverage.
1 parent 44e868d commit 63c2115

File tree

6 files changed

+49
-3
lines changed

6 files changed

+49
-3
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

lib/openapi3_parser/node_factory/discriminator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
module 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,

lib/openapi3_parser/node_factory/object.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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

lib/openapi3_parser/node_factory/object_factory/dsl.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff 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

lib/openapi3_parser/node_factory/object_factory/validator.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff 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
)

spec/lib/openapi3_parser/node_factory/discriminator_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,32 @@
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
1240
end

0 commit comments

Comments
 (0)