python-openapi / openapi-core

Openapi-core is a Python library that adds client-side and server-side support for the OpenAPI v3.0 and OpenAPI v3.1 specification.
BSD 3-Clause "New" or "Revised" License
299 stars 132 forks source link

[Bug]: RequestBodyValidationError when sending image for multipart form data #761

Closed seththoburn-cc closed 8 months ago

seththoburn-cc commented 8 months ago

Actual Behavior

This seems to be the same as #630, but I'm getting a RequestBodyValidationError. It's thrown by a MediaTypeDeserializeError further up. Here are the relevant parts of the stacktrace:

  File "openapi_core/deserializing/media_types/util.py", line 24, in <dictcomp>
    part.get_param("name", header="content-disposition"): part.get_payload(
AttributeError: 'str' object has no attribute 'get_param'

  File "openapi_core/deserializing/media_types/deserializers.py", line 31, in deserialize
    raise MediaTypeDeserializeError(self.mimetype, value)
openapi_core.deserializing.media_types.exceptions.MediaTypeDeserializeError: Failed to deserialize value with multipart/form-data mimetype: --------------------------5ccec95762968d0b
Content-Disposition: form-data; name="file"; filename="Rectangle 40044.png"
Content-Type: image/png

  File "openapi_core/validation/decorators.py", line 58, in _raise_error
    raise init(**kw) from exc
openapi_core.validation.request.exceptions.RequestBodyValidationError: Request body validation error

Expected Behavior

The body is validated

Steps to Reproduce

Following schema:

"/uploads": {
      "post": {
        "requestBody": {
          "content": {
            "multipart/form-data": {
              "schema": {
                "properties": {
                  "file": { "format": "binary", "type": "string" }
                },
                "required": ["file"],
                "type": "object"
              }
            }
          }
        },
        "responses": {
          "201": {
            "content": {
              "application/json": {
                "schema": {
                  "properties": { "id": { "format": "uuid", "type": "string" } }
                }
              }
            },
            "description": "Upload created successfully"
          },
        },
        "security": [{ "bearerAuth": [] }],
        "summary": "Create an upload",
        "tags": ["upload"]
      }
    }

Request:

curl -X POST \
 -H  "Content-type: multipart/form-data" \
 -H "Authorization: Bearer TOKEN" \
 -F "file=@/Users/seththoburn/Downloads/Rectangle 40044.png" \
 https://localhost:3000/api/v1/uploads

OpenAPI Core Version

0.18.2

OpenAPI Core Integration

flask

Affected Area(s)

unmarshalling

References

630

Anything else we need to know?

No response

Would you like to implement a fix?

None

seththoburn-cc commented 8 months ago

Turns out this is fixed in 0.19.0a1, I had thought it was supposed to be fixed in 0.18.2.