ota42y / openapi_parser

validate and coerce parameter using OpenAPI3 definition
MIT License
122 stars 87 forks source link

support format: binary #108

Open ota42y opened 3 years ago

ota42y commented 3 years ago

Plan

OpenAPI 3.0.3 support fomat: binary so we should support it. https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#data-types

Rack set file as Rack::Multipart::UploadedFile so we should check it in string validator. https://github.com/ota42y/openapi_parser/blob/master/lib/openapi_parser/schema_validators/string_validator.rb#L10

Other

related https://github.com/interagent/committee/issues/255#issuecomment-699821755

fmarkwong commented 1 year ago

@ota42y wanted to ask what the status on this is. Is it in production already? I still get the same "expected string but received Hash" validation error for file uploads (with type: string and format: binary)

ota42y commented 1 year ago

Sorry, we are not currently implementing it, and we have no plans to do so for the time being.

pechorin commented 1 year ago

Sorry for question: are there any workaround for this? For now it's impossible to validate submitted via form data file :/

ota42y commented 1 year ago

At the moment there are none. However, if there is an error, we would like to have some workaround (no verification, but no error either).

pechorin commented 1 year ago

This code/patch works for me:

module StringValidatorPatch
  def coerce_and_validate(value, schema, **_keyword_args)
    # Diff:
    # :- return OpenAPIParser::ValidateError.build_error_result(value, schema) unless value.kind_of?(String)
    #
    # :+
    if !value.is_a?(String)
      if schema.format == 'binary'
        # Fix: in tests rack uploaded file not converts automatically (committee bug)
        if value[:tempfile]
          return [ActionDispatch::Http::UploadedFile.new(value), nil]
        else
          return [value, nil]
        end
      else
        return OpenAPIParser::ValidateError.build_error_result(value, schema)
      end
    end
    # End of diff

    value, err = check_enum_include(value, schema)
    return [nil, err] if err

    value, err = pattern_validate(value, schema)
    return [nil, err] if err

    value, err = validate_max_min_length(value, schema)
    return [nil, err] if err

    value, err = validate_email_format(value, schema)
    return [nil, err] if err

    value, err = validate_uuid_format(value, schema)
    return [nil, err] if err

    value, err = validate_date_format(value, schema)
    return [nil, err] if err

    value, err = validate_datetime_format(value, schema)
    return [nil, err] if err

    [value, nil]
  end
end

class OpenAPIParser::SchemaValidator::StringValidator
  prepend StringValidatorPatch
end