From 1158cfda15a30ff200b1afbe42f717b75a049877 Mon Sep 17 00:00:00 2001 From: Kevin Dew Date: Wed, 29 Jan 2025 10:16:53 +0000 Subject: [PATCH] Add dependentRequired field for 3.1 schema Based on https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00 --- lib/openapi3_parser/node/schema/v3_1.rb | 5 +++++ lib/openapi3_parser/node_factory/schema/v3_1.rb | 13 ++++++++++++- spec/integration/open_v3.1_examples_spec.rb | 13 +++++++++++-- spec/support/examples/v3.1/changes.yaml | 15 +++++++++++++++ 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/lib/openapi3_parser/node/schema/v3_1.rb b/lib/openapi3_parser/node/schema/v3_1.rb index b8886308..c285669a 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 dc16a0cd..1412630f 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 649c47d1..38cb6cdb 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 456b8064..e6490e03 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