openapi_parser icon indicating copy to clipboard operation
openapi_parser copied to clipboard

support format: binary

Open ota42y opened this issue 4 years ago • 5 comments

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

ota42y avatar May 04 '21 05:05 ota42y

@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)

fmarkwong avatar Dec 29 '22 08:12 fmarkwong

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

ota42y avatar Jan 28 '23 06:01 ota42y

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

pechorin avatar Mar 03 '23 11:03 pechorin

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).

ota42y avatar Mar 12 '23 14:03 ota42y

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

pechorin avatar Mar 12 '23 23:03 pechorin