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

validate request with multipart makes body unretrievable

Open aligator opened this issue 1 year ago • 0 comments

with this route

  /api/import/7z:
    post:
      tags:
        - Import
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  format: binary
      responses:
        "200":
          description: OK

The body is read by the ValidateRequestBody function. After that function the body is read, and I can no longer read it. The value in openapi3filter/validate_request.go#L275 is actually never used because the defaultsSet is false.

I wouldn't expect the validator to read the full body in this case, as it is completely unnecessary. Before I used the validator I had the route to "stream" the multipart body to avoid loading the whole file in RAM.

This makes no sense with a validator that reads the body anyway...

So two problems:

  • I didn't expect the validator to read the body in this route, as there is nothing to validate. Is there a way to provide a NoOp BodyDecoder which just does nothing and leaves the body reader as it is?
  • Reading the body by the validator without "reseting" the reader afterwards makes it impossible to retrieve the file.

aligator avatar Apr 09 '24 13:04 aligator