Skip to content

Commit

Permalink
Merge pull request #109 from simoleone/support-typeless-schemas
Browse files Browse the repository at this point in the history
Support empty schema as any type
  • Loading branch information
ota42y authored May 23, 2021
2 parents 2d2f57b + d836438 commit 91a455a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
7 changes: 6 additions & 1 deletion lib/openapi_parser/schema_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require_relative 'schema_validators/all_of_validator'
require_relative 'schema_validators/one_of_validator'
require_relative 'schema_validators/nil_validator'
require_relative 'schema_validators/unspecified_type_validator'

class OpenAPIParser::SchemaValidator
# validate value by schema
Expand Down Expand Up @@ -113,7 +114,7 @@ def validator(value, schema)
when 'array'
array_validator
else
nil
unspecified_type_validator
end
end

Expand Down Expand Up @@ -156,4 +157,8 @@ def one_of_validator
def nil_validator
@nil_validator ||= OpenAPIParser::SchemaValidator::NilValidator.new(self, @coerce_value)
end

def unspecified_type_validator
@unspecified_type_validator ||= OpenAPIParser::SchemaValidator::UnspecifiedTypeValidator.new(self, @coerce_value)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class OpenAPIParser::SchemaValidator
class UnspecifiedTypeValidator < Base
# @param [Object] value
def coerce_and_validate(value, _schema, **_keyword_args)
value
end
end
end
1 change: 1 addition & 0 deletions spec/data/normal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ paths:
anyOf:
- type: string
- type: boolean
unspecified_type: {}
enum_string:
type: string
enum:
Expand Down
21 changes: 7 additions & 14 deletions spec/openapi_parser/schema_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@
end
end
end

context 'unspecified_type' do
it do
expect(request_operation.validate_request_body(content_type, { 'unspecified_type' => "foo" })).
to eq({ 'unspecified_type' => "foo" })
end
end
end

describe 'object' do
Expand Down Expand Up @@ -426,20 +433,6 @@
expect(e.message).to end_with("does not define properties: unknown")
end
end

describe 'invalid type' do
let(:root) { OpenAPIParser.parse(invalid_schema, config) }
let(:invalid_schema) do
data = normal_schema
data['paths']['/validate']['post']['requestBody']['content']['application/json']['schema'].delete 'type'
data
end

it do
params = { 'string' => 'str' }
expect { request_operation.validate_request_body(content_type, params) }.to raise_error(OpenAPIParser::ValidateError)
end
end
end

describe 'coerce' do
Expand Down

0 comments on commit 91a455a

Please sign in to comment.