Skip to content

Commit 659737d

Browse files
committed
Make extension regex configurable
This has been provided so that the OpenAPI 3.1 Schema can accept a schema with extensions without the `x-` prefix that OpenAPI nodes require.
1 parent 3fccad0 commit 659737d

7 files changed

Lines changed: 23 additions & 25 deletions

File tree

lib/openapi3_parser/node_factory/object.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Object
1111

1212
def_delegators "self.class",
1313
:field_configs,
14-
:allowed_extensions?,
14+
:extension_regex,
1515
:mutually_exclusive_fields,
1616
:allowed_default?,
1717
:validations

lib/openapi3_parser/node_factory/object_factory/dsl.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,12 @@ def field_configs
1717
@field_configs ||= {}
1818
end
1919

20-
def allow_extensions
21-
@allow_extensions = true
20+
def allow_extensions(regex: EXTENSION_REGEX)
21+
@extension_regex = regex
2222
end
2323

24-
def allowed_extensions?
25-
if instance_variable_defined?(:@allow_extensions)
26-
@allow_extensions == true
27-
else
28-
false
29-
end
24+
def extension_regex
25+
@extension_regex ||= nil
3026
end
3127

3228
def mutually_exclusive(*fields, required: false)

lib/openapi3_parser/node_factory/object_factory/validator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def check_required_fields
4141
def check_unexpected_fields
4242
Validators::UnexpectedFields.call(
4343
validatable,
44-
allow_extensions: factory.allowed_extensions?,
44+
extension_regex: factory.extension_regex,
4545
allowed_fields: factory.allowed_fields,
4646
raise_on_invalid: raise_on_invalid
4747
)

lib/openapi3_parser/node_factory/schema/oas_dialect_3_1.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ module Openapi3Parser
66
module NodeFactory
77
module Schema
88
class OasDialect3_1 < NodeFactory::Object # rubocop:disable Naming/ClassAndModuleCamelCase
9+
# Allows any extension as per:
10+
# https://github.com/OAI/OpenAPI-Specification/blob/a1facce1b3621df3630cb692e9fbe18a7612ea6d/versions/3.1.0.md#fixed-fields-20
11+
allow_extensions(regex: /.*/)
912
end
1013
end
1114
end

lib/openapi3_parser/validators/unexpected_fields.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ def self.call(*args, **kwargs)
1414

1515
def call(validatable,
1616
allowed_fields:,
17-
allow_extensions: true,
17+
extension_regex: nil,
1818
raise_on_invalid: true)
1919
fields = unexpected_fields(validatable.input,
2020
allowed_fields,
21-
allow_extensions)
21+
extension_regex)
2222
return if fields.empty?
2323

2424
if raise_on_invalid
@@ -35,11 +35,11 @@ def call(validatable,
3535

3636
private
3737

38-
def unexpected_fields(input, allowed_fields, allow_extensions)
38+
def unexpected_fields(input, allowed_fields, extension_regex)
3939
extra_keys = input.keys - allowed_fields
40-
return extra_keys unless allow_extensions
40+
return extra_keys unless extension_regex
4141

42-
extra_keys.grep_v(NodeFactory::EXTENSION_REGEX)
42+
extra_keys.grep_v(extension_regex)
4343
end
4444
end
4545
end

spec/integration/open_v3.1_examples_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
context "when using the webhook example" do
1313
let(:path) { File.join(__dir__, "..", "support", "examples", "v3.1", "webhook-example.yaml") }
1414

15-
xit "is a valid document" do
15+
it "is a valid document" do
1616
expect(document).to be_valid
1717
end
1818

spec/lib/openapi3_parser/validators/unexpected_fields_spec.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,22 @@
2727
end
2828
end
2929

30-
describe "allow_extensions option" do
30+
describe "extension_regex option" do
3131
let(:validatable) do
3232
create_validatable({ "x-extension" => "my extension",
3333
"x-extension-2" => "my other extension" })
3434
end
3535

36-
it "defaults to allowing extensions" do
36+
it "defaults to disallowing extensions" do
37+
validatable = create_validatable({ "extension" => "my extension" })
3738
expect { described_class.call(validatable, allowed_fields: []) }
38-
.not_to raise_error
39+
.to raise_error(Openapi3Parser::Error::UnexpectedFields, "Unexpected fields for #/: extension")
3940
end
4041

41-
it "raises an error when allow_extensions is false" do
42-
expect { described_class.call(validatable, allowed_fields: [], allow_extensions: false) }
43-
.to raise_error(
44-
Openapi3Parser::Error::UnexpectedFields,
45-
"Unexpected fields for #/: x-extension and x-extension-2"
46-
)
42+
it "accepts a regex of the pattern of extension that will be accepted" do
43+
validatable = create_validatable({ "x-extension" => "my extension" })
44+
expect { described_class.call(validatable, allowed_fields: [], extension_regex: /^x-.*/) }
45+
.not_to raise_error
4746
end
4847
end
4948

0 commit comments

Comments
 (0)