Skip to content

Commit

Permalink
Add dependentRequired field for 3.1 schema
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindew committed Jan 29, 2025
1 parent 8733278 commit 1158cfd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/openapi3_parser/node/schema/v3_1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def examples
self["examples"]
end

# @return [Node::Map<String, Node::Array<String>>]
def dependent_required
self["dependentRequired"]
end

# @return [String, nil]
def content_encoding
self["contentEncoding"]
Expand Down
13 changes: 12 additions & 1 deletion lib/openapi3_parser/node_factory/schema/v3_1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 11 additions & 2 deletions spec/integration/open_v3.1_examples_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions spec/support/examples/v3.1/changes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 1158cfd

Please sign in to comment.