Skip to content

Commit 89975e2

Browse files
committed
Add identifier, as a mutually exclusive field, to License
This adds the identifier field to License - as outlined in the OpenAPI 3.1 Specification [1]. A couple of things I considered doing for this, but ultimately didn't do, were: - Determine whether the SPDX license expression format could be validated with a regex. It looked like it probably could but not sure if we want to be that strict. - Add a hook in that would allow us to decide which OpenAPI versions would use the mutually_exclusive DSL method. I decided this wasn't necessary for now since the only benefit seems to be a slightly nicer error message.
1 parent ed2f422 commit 89975e2

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ For OpenAPI 3.1
5151
- [ ] Create a maxi OpenAPI 3.1 integration test to collate all the known changes
5252
- [ ] jsonSchemaDialect should default to OAS one
5353
- [x] Allow summary and description in Reference objects
54-
- [ ] Add identifier to License node, make mutually exclusive with URL
54+
- [x] Add identifier to License node, make mutually exclusive with URL
5555
- [ ] ServerVariable enum must not be empty
5656
- [ ] Add pathItems to components
5757
- [ ] Callbacks can now reference a PathItem - previously required them

lib/openapi3_parser/node/license.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ def name
1111
self["name"]
1212
end
1313

14+
# @return [String, nil]
15+
def identifier
16+
self["identifier"]
17+
end
18+
1419
# @return [String, nil]
1520
def url
1621
self["url"]

lib/openapi3_parser/node_factory/license.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ module NodeFactory
99
class License < NodeFactory::Object
1010
allow_extensions
1111
field "name", input_type: String, required: true
12+
field "identifier",
13+
input_type: String,
14+
allowed: ->(context) { context.openapi_version >= "3.1" }
1215
field "url",
1316
input_type: String,
1417
validate: Validation::InputValidator.new(Validators::Url)
18+
mutually_exclusive "identifier", "url"
1519

1620
def build_node(data, node_context)
1721
Node::License.new(data, node_context)

spec/lib/openapi3_parser/node_factory/license_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,39 @@
2424
expect(instance).to have_validation_error("#/url")
2525
end
2626
end
27+
28+
describe "identifier field" do
29+
it "accepts an identifier field for OpenAPI >= v3.1" do
30+
factory_context = create_node_factory_context(
31+
minimal_license_definition.merge({ "identifier" => "Apache-2.0" }),
32+
document_input: { "openapi" => "3.1.0" }
33+
)
34+
expect(described_class.new(factory_context)).to be_valid
35+
end
36+
37+
it "rejects an identifier field for OpenAPI < v3.1" do
38+
factory_context = create_node_factory_context(
39+
minimal_license_definition.merge({ "identifier" => "Apache-2.0" }),
40+
document_input: { "openapi" => "3.0.0" }
41+
)
42+
instance = described_class.new(factory_context)
43+
44+
expect(instance).not_to be_valid
45+
expect(instance).to have_validation_error("#/").with_message("Unexpected fields: identifier")
46+
end
47+
48+
it "rejects both a license and a url field (mutually exclusive)" do
49+
factory_context = create_node_factory_context(
50+
minimal_license_definition.merge({
51+
"identifier" => "Apache-2.0",
52+
"url" => "https://example.com/url"
53+
}),
54+
document_input: { "openapi" => "3.1.0" }
55+
)
56+
instance = described_class.new(factory_context)
57+
58+
expect(instance).not_to be_valid
59+
expect(instance).to have_validation_error("#/").with_message("identifier and url are mutually exclusive fields")
60+
end
61+
end
2762
end

0 commit comments

Comments
 (0)