Skip to content

Commit

Permalink
Allow 3.1 references to have summary and description
Browse files Browse the repository at this point in the history
In OpenAPI 3.1 references can have summary and description metadata for
nodes that support those fields. They operate in a cascading pattern
where each reference can overwrite the previous ones.
  • Loading branch information
kevindew committed May 1, 2022
1 parent 0680be6 commit 840ed6c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ For OpenAPI 3.1
- [x] Support summary field on Info node
- [ ] Create a maxi OpenAPI 3.1 integration test to collate all the known changes
- [ ] jsonSchemaDialect should default to OAS one
- [ ] Allow summary and description in Reference objects
- [x] Allow summary and description in Reference objects
- [ ] Add identifier to License node, make mutually exclusive with URL
- [ ] ServerVariable enum must not be empty
- [ ] Add pathItems to components
Expand Down
6 changes: 6 additions & 0 deletions lib/openapi3_parser/node_factory/reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class Reference < NodeFactory::Object
include Referenceable

field "$ref", input_type: String, required: true, factory: :ref_factory
field "summary",
input_type: String,
allowed: ->(context) { context.openapi_version >= "3.1" }
field "description",
input_type: String,
allowed: ->(context) { context.openapi_version >= "3.1" }

attr_reader :factory

Expand Down
78 changes: 78 additions & 0 deletions spec/lib/openapi3_parser/node_factory/reference_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,82 @@ def create_node(input)
expect(instance.resolves?([instance.context.source_location])).to be false
end
end

describe "summary field" do
let(:factory) do
Openapi3Parser::NodeFactory::OptionalReference.new(
Openapi3Parser::NodeFactory::Contact
)
end

it "accepts a summary field for OpenAPI >= v3.1" do
factory_context = create_node_factory_context(
{
"$ref" => "#/item",
"summary" => "summary contents"
},
document_input: {
"openapi" => "3.1.0",
"item" => {}
}
)
expect(described_class.new(factory_context, factory)).to be_valid
end

it "rejects a summary field for OpenAPI < v3.1" do
factory_context = create_node_factory_context(
{
"$ref" => "#/item",
"summary" => "summary contents"
},
document_input: {
"openapi" => "3.0.0",
"item" => {}
}
)
instance = described_class.new(factory_context, factory)

expect(instance).not_to be_valid
expect(instance).to have_validation_error("#/").with_message("Unexpected fields: summary")
end
end

describe "description field" do
let(:factory) do
Openapi3Parser::NodeFactory::OptionalReference.new(
Openapi3Parser::NodeFactory::Contact
)
end

it "accepts a description field for OpenAPI >= v3.1" do
factory_context = create_node_factory_context(
{
"$ref" => "#/item",
"description" => "description contents"
},
document_input: {
"openapi" => "3.1.0",
"item" => {}
}
)
expect(described_class.new(factory_context, factory)).to be_valid
end

it "rejects a description field for OpenAPI < v3.1" do
factory_context = create_node_factory_context(
{
"$ref" => "#/item",
"description" => "description contents"
},
document_input: {
"openapi" => "3.0.0",
"item" => {}
}
)
instance = described_class.new(factory_context, factory)

expect(instance).not_to be_valid
expect(instance).to have_validation_error("#/").with_message("Unexpected fields: description")
end
end
end
12 changes: 12 additions & 0 deletions spec/support/examples/v3.1/changes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ info:
# identifier: Apache-2.0
# jsonSchemaDialect: https://spec.openapis.org/oas/3.1/dialect/base
components:
examples:
FullExample:
summary: Original summary
description: Original description
value: anything
ReferencedExample:
summary: Referenced summary
description: Referenced Description
$ref: "#/components/examples/FullExample"
DoubleReferencedExample:
summary: Double referenced summary
$ref: "#/components/examples/ReferencedExample"
schemas:
BasicSchema:
description: "My basic schema"
Expand Down

0 comments on commit 840ed6c

Please sign in to comment.