springfox / springfox-demos

Springfox demo applications
http://springfox.io
Apache License 2.0
737 stars 353 forks source link

FileUpload not working with JSON data #40

Open dheerajuppalapati opened 7 years ago

dheerajuppalapati commented 7 years ago

I tried the example https://github.com/springfox/springfox-demos/blob/be4118bb031c8dcded514c33d74cae52fc51ff08/boot-swagger/src/main/java/springfoxdemo/boot/swagger/web/FileUploadController.java

Just changed the @RequestPart String description to @RequestBody Component component which should be a JSON input.

Here is the code sample @RequestMapping(value = "/v1/component/fileupload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public Component fileComplaint( @RequestBody Component component, @RequestPart MultipartFile file){ ........................... }

Here is what the swagger-ui looks like image

Here is the request details image image

Here is the response details image

Here are my questions:

  1. I'm not sure why does say image in swagger-ui. It should say "application/json" instead. How do i fix this?

  2. If you request at the request paramaters, the data i'm putting in the "component" parameter is not being sent to the back-end. It only sends the file details.

  3. How do i fix org.springframework.web.HttpMediaTypeNotSupportedException??

Any kind of help will be appreciated. I already checked out the suggestions in https://github.com/springfox/springfox/issues/134 and it was of no help

Thanks, Dheeraj

dilipkrish commented 7 years ago

Swagger-ui does not support multipart file upload

dheerajuppalapati commented 7 years ago

I'm guessing this is not the right place to ask questions about springfox?? Every-time I ask questions I get answers from you which do not make sense.

What does this mean "Swagger-ui does not support multipart file upload"?? Springfox is build on swagger and example you posted for sprinfox here https://github.com/springfox/springfox-demos/blob/be4118bb031c8dcded514c33d74cae52fc51ff08/boot-swagger/src/main/java/springfoxdemo/boot/swagger/web/FileUploadController.java is for Multipart file upload.

I asked a simple question and I keep getting ridiculous answer.

NiteshMehta commented 6 years ago

I am also facing similar problem with https://github.com/springfox/springfox-demos/blob/be4118bb031c8dcded514c33d74cae52fc51ff08/boot-swagger/src/main/java/springfoxdemo/boot/swagger/web/FileUploadController.java

When I modify @RequestPart String description to @RequestPart RequestObject requestObject

I am getting error org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported

Even the curl generated by swagger-ui produces same error, but once I add type=application/json to body problem is fixed curl -X POST --header 'Content-Type: multipart/form-data' --header 'Accept: application/json' -F 'requestObject=@/req.json;type=application/json' 'http://localhost:8080/MyService/api/upload'

looking at https://github.com/swagger-api/swagger-core/issues/636, it looks like swagger-ui have support to provide content-type for RequestPart.

springfox version 2.7.0 springboot Version 1.5.4.RELEASE

dilipkrish commented 6 years ago

Request parts, and multipart requests in general are not supported by swagger-ui. Atleast as of version 2.0 of the spec.

NiteshMehta commented 6 years ago

Hi Dilip, I am confused here, isn't FileUploadController.java multipart request with two request parts ?

dilipkrish commented 6 years ago

It is but swagger-ui and OpenAPI spec in general doesnt support multipart requests well. FileUploadController is just to demonstrate that springfox tries to generate the right description

NiteshMehta commented 6 years ago

Thanks Dilip FileUploadController.java actually works, but its just that when we change @RequestPart String description to @RequestPart RequestObject requestObject it produces following error org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported

so does this means that it is partially supported?

rajademo commented 6 years ago

Dilip / Nitesh, any update on this issue. I'm also facing the same issue. When i change @RequestPart String to @RequestPart Object, i'm getting the error.

dilipkrish commented 6 years ago

Multipart requests are not supported very well in Swagger-ui unfortunately

ahmeterdem commented 4 years ago

"Multipart requests are not supported very well in Swagger-ui unfortunately" then for file upload instead of multipart file which data type can we use?