oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

I'm getting an error when File upload

Open delgec opened this issue 3 years ago • 6 comments

Hello everyone

I'm getting an error when File upload. I read the documents but I didn't fixed.

How can I fix it?

Thanks

Error message request body has an error: failed to decode request body: unsupported schema of request body

Source code https://github.com/delgec/restapi-sample

delgec avatar Jan 31 '22 11:01 delgec

Help me pls :(

delgec avatar Mar 07 '22 07:03 delgec

I'm having trouble with the same. It looks to me like the encoding is being ignored.

tschaub avatar Apr 14 '22 19:04 tschaub

i meet the same error

jesson1 avatar May 21 '22 03:05 jesson1

I found that I was able to make things work as long as I use application/octet-stream as the content type.

Here is a snippet from the schema:

  /upload:
    post:
      requestBody:
        required: true
        content:
          application/octet-stream:
            schema:
              type: string
              format: binary

In my case, I'm supporting upload of application/pdf and other types. I have to force the client to use application/octet-stream as the content type. And then I switch to the proper content type when I push the data to cloud storage.

tschaub avatar May 22 '22 21:05 tschaub

I also bumped into this issue. The workaround I found is to register the image type when your app starts:

import (
	"github.com/getkin/kin-openapi/openapi3filter"
	...
)
...
func init() {
	openapi3filter.RegisterBodyDecoder("image/jpeg", openapi3filter.FileBodyDecoder)
}
...

adyatlov avatar Jul 11 '22 02:07 adyatlov

What's the correct way to use multipart/form-data as the Content-Type? oapi-coden returns an error here: https://github.com/getkin/kin-openapi/blob/32445850faa0551385d614e844b344006c0a8f22/openapi3filter/req_resp_decoder.go#L984-L986

What are the conditions for schema.Value.Type to have the value "object" ?

It seems that there is no way to use the content type multipart/form-data at all, or at the very least it is unclear how it is supposed to be used.

edit: Solved the problem

openapi3filter.RegisterBodyDecoder("multipart/form-data", openapi3filter.FileBodyDecoder)

No clue what the multipartBodyDecoder is for since it is not working (?) or at the very least not required to handle multipart requests.

edit2: I finally figured out how to use multipart/form-data without overriding the default decoder. The problem is that my schema wasn't of type object. https://swagger.io/docs/specification/describing-request-body/multipart-requests/

RobertB4 avatar Jul 13 '22 07:07 RobertB4

openapi3filter.RegisterBodyDecoder("image/jpeg", openapi3filter.FileBodyDecoder)

Thank you to @adyatlov. This solved the problem. It wasn't enough to use type: object on the swagger definition.

Here was how I got it working: The swagger endpoint request body is defined like so:

      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                image:
                  type: string
                  format: binary

On application load, before I set up the validator:

openapi3filter.RegisterBodyDecoder("image/jpeg", openapi3filter.FileBodyDecoder)

Insomnia outputs a client request like this, so you can repro:

const form = new FormData();
form.append("image", "C:\\path\\to\\file.jpg");

const options = {
  method: 'POST',
  headers: {
    'Content-Type': 'multipart/form-data; boundary=---011000010111000001101001',
  }
};

options.body = form;

fetch('http://localhost:8080/images', options)
  .then(response => response.json())
  .then(response => console.log(response))
  .catch(err => console.error(err));

I'm using go-gin, and the corresponding code to parse the file looks like:

file, err := c.FormFile("image")
	if err != nil {
		//... handle error
	}

	err = c.SaveUploadedFile(file, "uploads/"+file.Filename)
	if err != nil {
		//... handle error
	}
//... respond success

codypotter avatar Jun 28 '23 20:06 codypotter