Skip to content

Commit

Permalink
Add methods for boolean schemas
Browse files Browse the repository at this point in the history
I wasn't sure whether to add methods to the existing class or to create
a new class. I'll see how this pans out.
  • Loading branch information
kevindew committed Jan 30, 2025
1 parent 07dabbf commit f43d88e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
36 changes: 36 additions & 0 deletions lib/openapi3_parser/node/schema/v3_1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,42 @@ class Schema < Node::Object
# these complexities and focuses on the core schema as defined in:
# https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-01
class V3_1 < Schema # rubocop:disable Naming/ClassAndModuleCamelCase
# Whether this is a schema that is just a boolean value rather
# than a schema object
#
# @return [Boolean]
def boolean?
!boolean.nil?
end

# Returns a boolean for a boolean schema [1] and nil for one based
# on an object
#
# [1]: https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-4.3.2
#
# @return [Boolean, nil]
def boolean
self["boolean"]
end

# Returns true when this is a boolean schema that has a true value,
# returns false for booleans schemas that have a false value or schemas
# that are objects.
#
# @return [Boolean]
def true?
boolean == true
end

# Returns false when this is a boolean schema that has a false value,
# returns false for booleans schemas that have a true value or schemas
# that are objects.
#
# @return [Boolean]
def false?
boolean == false
end

# @return [String, Node::Array<String>, nil]
def type
self["type"]
Expand Down
45 changes: 44 additions & 1 deletion spec/lib/openapi3_parser/node/schema/v3_1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,51 @@
RSpec.describe Openapi3Parser::Node::Schema::V3_1 do
it_behaves_like "schema node", openapi_version: "3.1.0"

describe "boolean methods" do
let(:instance) do
factory_context = create_node_factory_context(input)

Openapi3Parser::NodeFactory::Schema::V3_1
.new(factory_context)
.node(node_factory_context_to_node_context(factory_context))
end

context "when given a boolean schema with a true value" do
let(:input) { true }

it "identifies as a boolean" do
expect(instance.boolean?).to be(true)
expect(instance.boolean).to be(true)
expect(instance.true?).to be(true)
expect(instance.false?).to be(false)
end
end

context "when given a boolean schema with a false value" do
let(:input) { false }

it "identifies as a boolean" do
expect(instance.boolean?).to be(true)
expect(instance.boolean).to be(false)
expect(instance.true?).to be(false)
expect(instance.false?).to be(true)
end
end

context "when given an object schema" do
let(:input) { { "type" => "string" } }

it "does not identify as a boolean" do
expect(instance.boolean?).to be(false)
expect(instance.boolean).to be_nil
expect(instance.true?).to be(false)
expect(instance.false?).to be(false)
end
end
end

%i[if then else].each do |method_name|
describe method_name.to_s do
describe "##{method_name}" do
it "supports a Ruby reserved word as a method name" do
factory_context = create_node_factory_context(
{ method_name.to_s => { "type" => "string" } },
Expand Down

0 comments on commit f43d88e

Please sign in to comment.