diff --git a/lib/openapi3_parser/node/schema/v3_1.rb b/lib/openapi3_parser/node/schema/v3_1.rb index b888630..c285669 100644 --- a/lib/openapi3_parser/node/schema/v3_1.rb +++ b/lib/openapi3_parser/node/schema/v3_1.rb @@ -50,6 +50,11 @@ def examples self["examples"] end + # @return [Node::Map>] + def dependent_required + self["dependentRequired"] + end + # @return [String, nil] def content_encoding self["contentEncoding"] diff --git a/lib/openapi3_parser/node_factory/schema/v3_1.rb b/lib/openapi3_parser/node_factory/schema/v3_1.rb index dc16a0c..1412630 100644 --- a/lib/openapi3_parser/node_factory/schema/v3_1.rb +++ b/lib/openapi3_parser/node_factory/schema/v3_1.rb @@ -24,8 +24,8 @@ class V3_1 < NodeFactory::Object # rubocop:disable Naming/ClassAndModuleCamelCas field "exclusiveMinimum", input_type: Numeric field "maxContains", input_type: Integer field "minContains", input_type: Integer, default: 1 - # dependentRequired - map with basic validation rules field "examples", factory: NodeFactory::Array + field "dependentRequired", factory: :dependent_required_factory field "contentEncoding", input_type: String field "contentMediaType", input_type: String, @@ -83,6 +83,17 @@ def validate_type(validatable) end end + def dependent_required_factory(context) + value_factory = lambda do |value_context| + NodeFactory::Array.new(value_context, value_input_type: String) + end + + NodeFactory::Map.new( + context, + value_factory: + ) + end + def prefix_items_factory(context) NodeFactory::Array.new( context, diff --git a/spec/integration/open_v3.1_examples_spec.rb b/spec/integration/open_v3.1_examples_spec.rb index 649c47d..38cb6cd 100644 --- a/spec/integration/open_v3.1_examples_spec.rb +++ b/spec/integration/open_v3.1_examples_spec.rb @@ -45,10 +45,19 @@ end it "can access a referenced schema" do - expect(document.components.schemas["DoubleReferencedSchema"]["required"]) + expect(document.components.schemas["DoubleReferencedSchema"].required) .to match_array(%w[id name]) - expect(document.components.schemas["DoubleReferencedSchema"]["description"]) + expect(document.components.schemas["DoubleReferencedSchema"].description) .to eq("My double referenced schema") end + + it "can parse and navigate a dependentRequired field" do + schema = document.components.schemas["DependentRequired"] + + expect(schema.dependent_required).to be_a(Openapi3Parser::Node::Map) + expect(schema.dependent_required.keys).to match_array(%w[credit_card]) + expect(schema.dependent_required["credit_card"]).to be_a(Openapi3Parser::Node::Array) + expect(schema.dependent_required["credit_card"]).to match_array(%w[billing_address]) + end end end diff --git a/spec/support/examples/v3.1/changes.yaml b/spec/support/examples/v3.1/changes.yaml index 456b806..e6490e0 100644 --- a/spec/support/examples/v3.1/changes.yaml +++ b/spec/support/examples/v3.1/changes.yaml @@ -79,6 +79,20 @@ components: type: string exp: type: string + DependentRequired: + type: object + properties: + name: + type: string + credit_card: + type: number + billing_address: + type: string + required: + - name + dependentRequired: + credit_card: + - billing_address Number: type: integer multipleOf: 5 @@ -112,6 +126,7 @@ components: patternProperties: /^test/: type: string + # Boolean: true # Add content types # Add $ref usage (plain, merged, defs) # Add compound things: anyOf, oneOf, not, if, then, else