Skip to content

Commit 84c0722

Browse files
committed
Allow 3.1 references to have summary and description
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.
1 parent 6a7b230 commit 84c0722

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ For OpenAPI 3.1
5050
- [x] Support summary field on Info node
5151
- [ ] Create a maxi OpenAPI 3.1 integration test to collate all the known changes
5252
- [ ] jsonSchemaDialect should default to OAS one
53-
- [ ] Allow summary and description in Reference objects
53+
- [x] Allow summary and description in Reference objects
5454
- [ ] Add identifier to License node, make mutually exclusive with URL
5555
- [ ] ServerVariable enum must not be empty
5656
- [ ] Add pathItems to components

lib/openapi3_parser/node_factory/reference.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ class Reference < NodeFactory::Object
99
include Referenceable
1010

1111
field "$ref", input_type: String, required: true, factory: :ref_factory
12+
field "summary",
13+
input_type: String,
14+
allowed: ->(context) { context.openapi_version >= "3.1" }
15+
field "description",
16+
input_type: String,
17+
allowed: ->(context) { context.openapi_version >= "3.1" }
1218

1319
attr_reader :factory
1420

spec/lib/openapi3_parser/node_factory/reference_spec.rb

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,82 @@ def create_node(input)
120120
expect(instance.resolves?([instance.context.source_location])).to be false
121121
end
122122
end
123+
124+
describe "summary field" do
125+
let(:factory) do
126+
Openapi3Parser::NodeFactory::OptionalReference.new(
127+
Openapi3Parser::NodeFactory::Contact
128+
)
129+
end
130+
131+
it "accepts a summary field for OpenAPI >= v3.1" do
132+
factory_context = create_node_factory_context(
133+
{
134+
"$ref" => "#/item",
135+
"summary" => "summary contents"
136+
},
137+
document_input: {
138+
"openapi" => "3.1.0",
139+
"item" => {}
140+
}
141+
)
142+
expect(described_class.new(factory_context, factory)).to be_valid
143+
end
144+
145+
it "rejects a summary field for OpenAPI < v3.1" do
146+
factory_context = create_node_factory_context(
147+
{
148+
"$ref" => "#/item",
149+
"summary" => "summary contents"
150+
},
151+
document_input: {
152+
"openapi" => "3.0.0",
153+
"item" => {}
154+
}
155+
)
156+
instance = described_class.new(factory_context, factory)
157+
158+
expect(instance).not_to be_valid
159+
expect(instance).to have_validation_error("#/").with_message("Unexpected fields: summary")
160+
end
161+
end
162+
163+
describe "description field" do
164+
let(:factory) do
165+
Openapi3Parser::NodeFactory::OptionalReference.new(
166+
Openapi3Parser::NodeFactory::Contact
167+
)
168+
end
169+
170+
it "accepts a description field for OpenAPI >= v3.1" do
171+
factory_context = create_node_factory_context(
172+
{
173+
"$ref" => "#/item",
174+
"description" => "description contents"
175+
},
176+
document_input: {
177+
"openapi" => "3.1.0",
178+
"item" => {}
179+
}
180+
)
181+
expect(described_class.new(factory_context, factory)).to be_valid
182+
end
183+
184+
it "rejects a description field for OpenAPI < v3.1" do
185+
factory_context = create_node_factory_context(
186+
{
187+
"$ref" => "#/item",
188+
"description" => "description contents"
189+
},
190+
document_input: {
191+
"openapi" => "3.0.0",
192+
"item" => {}
193+
}
194+
)
195+
instance = described_class.new(factory_context, factory)
196+
197+
expect(instance).not_to be_valid
198+
expect(instance).to have_validation_error("#/").with_message("Unexpected fields: description")
199+
end
200+
end
123201
end

spec/support/examples/v3.1/changes.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ info:
88
# identifier: Apache-2.0
99
# jsonSchemaDialect: https://spec.openapis.org/oas/3.1/dialect/base
1010
components:
11+
examples:
12+
FullExample:
13+
summary: Original summary
14+
description: Original description
15+
value: anything
16+
ReferencedExample:
17+
summary: Referenced summary
18+
description: Referenced Description
19+
$ref: "#/components/examples/FullExample"
20+
DoubleReferencedExample:
21+
summary: Double referenced summary
22+
$ref: "#/components/examples/ReferencedExample"
1123
schemas:
1224
BasicSchema:
1325
description: "My basic schema"

0 commit comments

Comments
 (0)