Skip to content

Commit

Permalink
Add some dubious hacks to get appropriate errors
Browse files Browse the repository at this point in the history
These build on the smell of hacks from the previous commit to make this
class a bit smelly.

This adds some work arounds to get appropriate error messages for nodes
that could be one of two types. Should we need more usage of this we
probably want to refactor aspects of the ObjectFactory code to be able
to handle dual types and the TypeChecker.
  • Loading branch information
kevindew committed Jan 29, 2025
1 parent 6d16da6 commit 134b16d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
32 changes: 28 additions & 4 deletions lib/openapi3_parser/node_factory/schema/v3_1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,37 @@ def boolean_input?
end

def errors
@errors ||= boolean_input? ? [] : super
# It's a bit janky that we do this method overloading here to handle
# the dual types of a 3.1 Schema. However this is the only node we
# have this dual type behaviour. We should do something more clever
# in the factories if there is further precedent.
@errors ||= if boolean_input?
Validation::ErrorCollection.new
elsif raw_input && !raw_input.is_a?(::Hash)
error = Validation::Error.new(
"Invalid type. Expected Object or Boolean",
context,
self.class
)
Validation::ErrorCollection.new([error])
else
super
end
end

def node(node_context)
return super unless boolean_input?

Node::Schema::V3_1.new({ "boolean" => resolved_input }, node_context)
# as per #errors above, this is a bit of a nasty hack to handle
# dual type handling and should be refactored should there be
# other nodes with the same needs
if boolean_input?
Node::Schema::V3_1.new({ "boolean" => resolved_input }, node_context)
elsif raw_input && !raw_input.is_a?(::Hash)
raise Error::InvalidType,
"Invalid type for #{context.location_summary}: " \
"Expected Object or Boolean"
else
super
end
end

def build_node(data, node_context)
Expand Down
18 changes: 18 additions & 0 deletions spec/lib/openapi3_parser/node_factory/schema/v3_1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,24 @@

it_behaves_like "schema factory"

describe "type validation" do
it "rejects a non object or boolean input with an appropriate explanation" do
instance = described_class.new(create_node_factory_context(15))

expect(instance).to have_validation_error("#/").with_message("Invalid type. Expected Object or Boolean")
end

it "raises the appropriate error when a non object or boolean input is built" do
node_factory_context = create_node_factory_context("blah")
instance = described_class.new(node_factory_context)
node_context = node_factory_context_to_node_context(node_factory_context)

expect { instance.node(node_context) }
.to raise_error(Openapi3Parser::Error::InvalidType,
"Invalid type for #/: Expected Object or Boolean")
end
end

describe "boolean input" do
it "is valid for a boolean input" do
instance = described_class.new(create_node_factory_context(false))
Expand Down

0 comments on commit 134b16d

Please sign in to comment.