Skip to content

WebFlux Multipart File Upload not possible with annotations @RouterOperations and @RequestBody. Enpoints defined as 'RouterFunction' #746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
FrantisekSchneider opened this issue Jun 24, 2020 · 2 comments

Comments

@FrantisekSchneider
Copy link

FrantisekSchneider commented Jun 24, 2020

85558121-bb90d680-b628-11ea-8290-e758474b2efa
For an endpoint using Spring WebFlux to upload a file, the Swagger UI documentation is not generated properly. It doesn't allow to choose a file and send it as a multipart request.

                    method = "POST",
                    operationId = "uploadExcelfile",
                     requestBody = RequestBody(
                        required = true,
                        description = "Excel file",
                        content = [
                            Content(
                                schema = Schema(
                                    description = "Excel file",
                                    type = "object",
                                    name = "excelfile",
                                ),
                                mediaType = MediaType.MULTIPART_FORM_DATA_VALUE,
                                encoding = [Encoding(
                                    name = "excelfile",
                                    contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE
                                )]
                            )
                        ]
                    )```



 "/api/files/smart-matching": {
  "post": {
    "operationId": "uploadExcelfile",
    "requestBody": {
      "content": {
        "multipart/form-data": {
          "encoding": {
            "excelfile": {
              "contentType": "application/octet-stream"
            }
          },
          "schema": {
            "description": "Excel file",
            "type": "object"
          }
        }
      },
      "description": "Excel file",
      "required": true
    },
--- 
* Environment + Language *
Spring Boot : 2.2.4
Kotlin: 1.3.7
org.springdoc:springdoc-openapi-webflux-ui:1.4.1

---
**Possible solution?**
I guess I need the properties support like in issue mentioned in https://stackoverflow.com/questions/14455408/how-to-post-files-in-swagger-openapi
`
 "paramType": "body",
 "dataType": "file",
`
@bnasslahsen
Copy link
Collaborator

@FrantisekSchneider,

This is not directly a springdoc-openapi issue.
But this is related to missing properties from the swagger-annotations: springdoc-openapi is built on top of swagger-core and swagger-annotations.

This is the link to the existing swagger-core issue:

You can log you comments here, for the support, or directly propose a PR on the swagger-core project.

You can correct this behaviour with springdoc-openapi: For example, you can declare minimal informations on using @RouterOperation.

@RouterOperation(method = RequestMethod.POST,
		operation = @Operation(operationId = "uploadExcelfile",
				requestBody = @RequestBody(required = true, description = "Excel file"),
				responses = @ApiResponse()))

And then enhance it using OpenApiCustomiser:

@Bean
OpenApiCustomiser openApiCustomiser() {
	return openApi ->
			openApi.getPaths().values().stream().flatMap(pathItem -> pathItem.readOperations().stream())
					.forEach(operation -> {
						if ("uploadExcelfile".equals(operation.getOperationId())) {
							RequestBody requestBody = operation.getRequestBody();
							requestBody.setContent(new Content().addMediaType(MediaType.APPLICATION_FORM_URLENCODED_VALUE,
									new io.swagger.v3.oas.models.media.MediaType().schema(new ObjectSchema().addProperties("excelfile", new FileSchema()))));
						}
					});
}

@FrantisekSchneider
Copy link
Author

thank you very much -> closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants