swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

Multipart data (files) not rendered correctly in Swagger UI unless an extra parameter is present

Open senn opened this issue 3 years ago • 1 comments

With method signature

@PostMapping(value = "/scan", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "Success"),
        @ApiResponse(responseCode = "400", description = "Something went wrong")})
public MyResponse doSomething(
        @Parameter(name = "files",
                description = "A list of files", style = ParameterStyle.FORM, content = {
                @Content(mediaType = "*/*", schema = @Schema(type = "string", format = "binary"))
        })
        @RequestParam(value = "files") List<MultipartFile> files)

Swagger UI renders the files part incorrectly as a text area: image

The service can't be tested this way. However, everything works perfectly when using another client like Postman.

However, by simply adding a dummy parameter swaggerUiHack like this:

@PostMapping(value = "/scan", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ApiResponses(value = {
      @ApiResponse(responseCode = "200", description = "Success"),
      @ApiResponse(responseCode = "400", description = "Something went wrong")})
public MyResponse doSomething(
            @Parameter(description = "This is a hack to make Swagger UI render correctly")
            @RequestPart(required = false) String swaggerUiHack,
            @Parameter(name = "files",
                    description = "A list of files", style = ParameterStyle.FORM, content = {
                    @Content(mediaType = "application/octet-stream", schema = @Schema(type = "string", format = "binary"))
            })
            @RequestPart(value = "files") List<MultipartFile> files)

The Swagger UI becomes image

Although the descriptions are not rendered correctly: files description at top and no description of my swaggerUiHack field, the multipart files param can now be selected correctly (although only one file can be selected).

EDIT: using swagger-annotations-2.1.1.jar

senn avatar Jun 15 '22 07:06 senn

Also tested on new version 2.2.1 and same result.

senn avatar Jun 16 '22 09:06 senn