Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require discriminator instance to be an object #206

Merged
merged 1 commit into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/json_schemer/openapi31/vocab/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def subschemas_by_property_value
end

def validate(instance, instance_location, keyword_location, context)
return result(instance, instance_location, keyword_location, true) unless instance.is_a?(Hash)
return result(instance, instance_location, keyword_location, false) unless instance.is_a?(Hash)

property_name = value.fetch('propertyName')

Expand Down
102 changes: 100 additions & 2 deletions test/open_api_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,107 @@ def test_non_json_pointer_discriminator
end

def test_discriminator_non_object_and_missing_property_name
schemer = JSONSchemer.schema({ 'discriminator' => { 'propertyName' => 'x' } }, :meta_schema => JSONSchemer.openapi31)
assert(schemer.valid?(1))
schemer = JSONSchemer.schema(
{
'anyOf' => [{ '$ref' => '#/components/schemas/z' }],
'discriminator' => { 'propertyName' => 'x' },
'components' => { 'schemas' => { 'z' => true } },
},
:meta_schema => JSONSchemer.openapi31
)
refute(schemer.valid?(1))
refute(schemer.valid?({ 'y' => 'z' }))
assert(schemer.valid?({ 'x' => 'z' }))
end

def test_discriminator_nullable
cat = { id: 1, type: 'cat', meow: 'Meow' }
dog = { id: 1, type: 'dog', bark: 'Woof' }
junk = { id: 1, type: 'cat', junk: 'junk' }

refs = {
URI('json-schemer://schema/cat') => {
type: 'object',
properties: {
id: {
type: 'integer',
},
type: {
type: 'string',
const: 'cat',
},
meow: {
type: 'string',
},
},
required: %w[id type meow],
},
URI('json-schemer://schema/dog') => {
type: 'object',
properties: {
id: {
type: 'integer',
},
type: {
type: 'string',
const: 'dog',
},
bark: {
type: 'string',
},
},
required: %w[id type bark],
},
}

nullable_union_schema = {
oneOf: [
{ type: 'null' },
{
oneOf: [{ '$ref': 'cat' }, { '$ref': 'dog' }],
discriminator: {
propertyName: 'type',
mapping: {
cat: 'cat',
dog: 'dog',
},
},
},
],
}

nullable_union_schemer = JSONSchemer.schema(
nullable_union_schema,
meta_schema: 'https://spec.openapis.org/oas/3.1/dialect/base',
ref_resolver: refs.to_proc,
)

assert(nullable_union_schemer.valid?(cat))
assert(nullable_union_schemer.valid?(dog))
refute(nullable_union_schemer.valid?(junk))
assert(nullable_union_schemer.valid?(nil))

non_nullable_union_schema = {
oneOf: [{ '$ref': 'cat' }, { '$ref': 'dog' }],
discriminator: {
propertyName: 'type',
mapping: {
cat: 'cat',
dog: 'dog',
},
},
}

non_nullable_union_schemer = JSONSchemer.schema(
non_nullable_union_schema,
meta_schema: 'https://spec.openapis.org/oas/3.1/dialect/base',
ref_resolver: refs.to_proc,
)

assert(non_nullable_union_schemer.valid?(cat))
assert(non_nullable_union_schemer.valid?(dog))
refute(non_nullable_union_schemer.valid?(junk))
refute(non_nullable_union_schemer.valid?(nil))
end

def test_openapi31_formats
Expand Down
Loading