kin-openapi icon indicating copy to clipboard operation
kin-openapi copied to clipboard

multipart/form-data - validation fails

Open adabuleanu opened this issue 1 year ago • 1 comments

For the following openapi 3 multi-part:

openapi: 3.0.3
paths:
  /endpoint:
    post:
      operationId: create
      requestBody:
        content:
          multipart/form-data:
            schema:
              $ref: "#/components/schemas/Request"
components:
  schemas:
    Request:
      type: object
      properties:
        mystring:
          type: string
        myint:
          type: integer
        myobjj:
          type: object

And using the following curl command:

 curl -X 'POST' 'http://localhost/endpoint'  \
        -H 'accept: application/json' \
        -H 'Content-Type: multipart/form-data'   \
        -F 'mystring=test' \
        -F 'myint=0'
        -F 'myobj={"key1":"val1"}'  

The validation for the myint and myobj fails with

request body has an error: doesn't match schema #/components/schemas/Request: Error at "/myint": value must be an integer
request body has an error: doesn't match schema #/components/schemas/Request: Error at "/myobj": value must be an object

The failures comes from comparing these types with string because all the values are decoded as strings:

interface {}(map[string]interface {}) ["mystring": "...", "myint": "...", "myobj": "..." ]

Is this a valid bug or am I doing something wrong? The only thing I could find was these tickets: https://github.com/getkin/kin-openapi/issues/793 and https://github.com/deepmap/oapi-codegen/issues/1169 which are closed without a fix or a workaround.

I am using github.com/deepmap/oapi-codegen/v2 v2.1.0 and github.com/getkin/kin-openapi v0.122.0

adabuleanu avatar May 15 '24 14:05 adabuleanu

same issue, any workarounds?

Abdullah-AlAttar avatar Feb 18 '25 14:02 Abdullah-AlAttar