Skip to content

Commit a4d6dbb

Browse files
committed
Add jsonSchemaDialect field to openapi node
This is a field introduced in OpenAPI 3.1 and it defaults to the OpenAPI 3.1 dialect. My intention is to only support this dialect for OpenAPI 3.1.
1 parent 2215772 commit a4d6dbb

File tree

5 files changed

+37
-9
lines changed

5 files changed

+37
-9
lines changed

lib/openapi3_parser/document.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class Document
3838
# The value of the info field on the OpenAPI document
3939
# @see Node::Openapi#info
4040
# @return [Node::Info]
41+
# @!method jsonSchemaDialect
42+
# The value of the jsonSchemaDialect field on the OpenAPI document
43+
# @see Node::Openapi#json_schema_dialect
44+
# @return [String, nil]
4145
# @!method servers
4246
# The value of the servers field on the OpenAPI document
4347
# @see Node::Openapi#servers
@@ -75,9 +79,9 @@ class Document
7579
# Iterate through the attributes of the root object
7680
# @!method keys
7781
# Access keys of the root object
78-
def_delegators :root, :openapi, :info, :servers, :paths, :components,
79-
:security, :tags, :external_docs, :extension, :[], :each,
80-
:keys
82+
def_delegators :root, :openapi, :info, :json_schema_dialect, :servers,
83+
:paths, :components, :security, :tags, :external_docs,
84+
:extension, :[], :each, :keys
8185

8286
# @param [SourceInput] source_input
8387
# @param [Boolean] emit_warnings Whether to call Kernel.warn when
@@ -175,7 +179,7 @@ def look_up_pointer(pointer, relative_pointer, subject)
175179
end
176180

177181
def add_warning(text)
178-
warn("Warning: #{text}") if emit_warnings
182+
warn("Warning: #{text} - disable these by opening a document with emit_warnings: false") if emit_warnings
179183
@warnings << text
180184
end
181185

lib/openapi3_parser/node/openapi.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ def info
1717
self["info"]
1818
end
1919

20+
# The default jsonSchemaDialect for this document
21+
#
22+
# @return [String, nil]
23+
def json_schema_dialect
24+
self["jsonSchemaDialect"]
25+
end
26+
2027
# @return [Node::Array<Server>]
2128
def servers
2229
self["servers"]

lib/openapi3_parser/node_factory/openapi.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Openapi < NodeFactory::Object
1616
field "jsonSchemaDialect",
1717
default: "https://spec.openapis.org/oas/3.1/dialect/base",
1818
input_type: String,
19+
validate: Validation::InputValidator.new(Validators::Uri),
1920
allowed: ->(context) { context.openapi_version >= "3.1" }
2021
field "servers", factory: :servers_factory
2122
field "paths",

spec/lib/openapi3_parser/node_factory/openapi_spec.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,26 @@
165165
.to have_validation_error("#/")
166166
.with_message("At least one of components, paths and webhooks fields are required")
167167
end
168+
169+
it "accepts a jsonSchemaDialect field" do
170+
factory_context = create_node_factory_context(
171+
minimal_openapi_definition.merge({ "openapi" => "3.1.0",
172+
"jsonSchemaDialect" => "uri://to/dialect" }),
173+
document_input: { "openapi" => "3.1.0" }
174+
)
175+
176+
instance = described_class.new(factory_context)
177+
expect(instance).to be_valid
178+
end
179+
180+
it "defaults to the OAS 3.1 jsonSchemaDialect" do
181+
node = create_node(minimal_openapi_definition.merge({ "openapi" => "3.1.0" }))
182+
expect(node.json_schema_dialect).to eq("https://spec.openapis.org/oas/3.1/dialect/base")
183+
end
168184
end
169185

170186
def create_node(input)
171-
node_factory_context = create_node_factory_context(input)
187+
node_factory_context = create_node_factory_context(input, document_input: { openapi: input["openapi"] })
172188
instance = described_class.new(node_factory_context)
173189
node_context = node_factory_context_to_node_context(node_factory_context)
174190
instance.node(node_context)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ info:
33
title: Examples of changes in OpenAPI 3.1
44
summary: 3.1 introduced a summary field to the Info node
55
version: 1.0.0
6-
# license:
7-
# name: Apache 2.0
8-
# identifier: Apache-2.0
9-
# jsonSchemaDialect: https://spec.openapis.org/oas/3.1/dialect/base
6+
license:
7+
name: Apache 2.0
8+
identifier: Apache-2.0
9+
jsonSchemaDialect: https://spec.openapis.org/oas/3.1/dialect/base
1010
components:
1111
examples:
1212
FullExample:

0 commit comments

Comments
 (0)