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

Skip validation if the format is binary, even if the value is not an actual string. #180

Merged
merged 1 commit into from
Feb 27, 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
13 changes: 12 additions & 1 deletion lib/openapi_parser/schema_validator/string_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ def initialize(validator, coerce_value, datetime_coerce_class)
end

def coerce_and_validate(value, schema, **_keyword_args)
return OpenAPIParser::ValidateError.build_error_result(value, schema) unless value.kind_of?(String)
unless value.kind_of?(String)
# Skip validation if the format is `binary`, even if the value is not an actual string.
# ref: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#data-types
if schema.format == 'binary'
# TODO:
# It would be better to check whether the value is an instance of `Rack::Multipart::UploadFile`,
# `ActionDispatch::Http::UploadedFile`, or another similar class.
return [value, nil]
end

return OpenAPIParser::ValidateError.build_error_result(value, schema)
end

value, err = check_enum_include(value, schema)
return [nil, err] if err
Expand Down
20 changes: 20 additions & 0 deletions spec/openapi_parser/schema_validator/string_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,24 @@
end
end
end

describe 'validate binary format' do
subject { OpenAPIParser::SchemaValidator.validate(params, target_schema, options) }

let(:replace_schema) do
{
binary: {
type: 'string',
format: 'binary',
},
}
end

context 'correct' do
context 'arbitrary object except string' do
let(:params) { { 'binary' => ['b', 'i', 'n', 'a', 'r', 'y'] } }
it { expect(subject).to eq({ 'binary' => ['b', 'i', 'n', 'a', 'r', 'y'] }) }
end
end
end
end