Skip to content

Commit f340966

Browse files
committed
Add definitions for if, then and else schema fields
I didn't think I'd be able to use these words as method names as they are Ruby reserved keywords. However as I understand it is ok because Ruby can work with them contextual (i.e. they're always prefixed by an object e.g self.if) Spec used: https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-10.2.2.1
1 parent 3b18566 commit f340966

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

json-schema-for-3.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ not - schema - in 3.0
7979
if - single schema
8080
then - single schema
8181
else - single schema
82+
dependentSchemas - map of schemas - somewhat complex, is it related to dependentRequired ?
8283

8384
prefixItems: schema
8485
items: schema - in 3.0

lib/openapi3_parser/node/schema/v3_1.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ def content_media_type
5454
def content_schema
5555
self["contentSchema"]
5656
end
57+
58+
# @return [Schema, nil]
59+
def if
60+
self["if"]
61+
end
62+
63+
# @return [Schema, nil]
64+
def then
65+
self["then"]
66+
end
67+
68+
# @return [Schema, nil]
69+
def else
70+
self["else"]
71+
end
5772
end
5873
end
5974
end

lib/openapi3_parser/node_factory/schema/v3_1.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class V3_1 < NodeFactory::Object # rubocop:disable Naming/ClassAndModuleCamelCas
2929
input_type: String,
3030
validate: Validation::InputValidator.new(Validators::MediaType)
3131
field "contentSchema", factory: :referenceable_schema
32+
field "if", factory: :referenceable_schema
33+
field "then", factory: :referenceable_schema
34+
field "else", factory: :referenceable_schema
3235

3336
def build_node(data, node_context)
3437
Node::Schema::V3_1.new(data, node_context)

spec/lib/openapi3_parser/node/schema/v3_1_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,31 @@
22

33
RSpec.describe Openapi3Parser::Node::Schema::V3_1 do
44
it_behaves_like "schema node", openapi_version: "3.1.0"
5+
6+
%i[if then else].each do |method_name|
7+
describe method_name.to_s do
8+
it "supports a Ruby reserved word as a method name" do
9+
factory_context = create_node_factory_context(
10+
{ method_name.to_s => { "type" => "string" } },
11+
document_input: {
12+
"openapi" => "3.1.0",
13+
"info" => {
14+
"title" => "Minimal Openapi definition",
15+
"version" => "1.0.0"
16+
}
17+
}
18+
)
19+
20+
instance = Openapi3Parser::NodeFactory::Schema::V3_1
21+
.new(factory_context)
22+
.node(node_factory_context_to_node_context(factory_context))
23+
24+
# check we're testing the right class
25+
expect(instance).to be_an_instance_of(described_class)
26+
27+
expect(instance.public_send(method_name))
28+
.to be_an_instance_of(described_class)
29+
end
30+
end
31+
end
532
end

0 commit comments

Comments
 (0)