Skip to content

Multipart with JSON and files payload #833

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
blaluc opened this issue Aug 22, 2020 · 2 comments
Closed

Multipart with JSON and files payload #833

blaluc opened this issue Aug 22, 2020 · 2 comments

Comments

@blaluc
Copy link

blaluc commented Aug 22, 2020

I didn't catch if this issue has a solution or if it depends on the standard swagger-ui.
There's one of the test cases:
https://github.com/springdoc/springdoc-openapi/blob/master/springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app119/HelloController.java
that does not work when invoked from the UI.

The error reported in the console is:

Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]

Is it possible to annotate a mulitpart post request in a way that it allows to submit both a json payload and one or more files from the swagger-ui?

@bnasslahsen
Copy link
Collaborator

@blaluc,

The only workaround for sending mutlipart data json and more files, are explained here:

@little-pinecone
Copy link

The workaround from #820 still leaves Swagger UI in a broken state.

A working workaround for Swagger is to provide a custom HttpMessageConverter to deserialize application/octet-stream as shown here: #6462.

The converter:

@Bean
public MappingJackson2HttpMessageConverter octetStreamJsonConverter() {
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setSupportedMediaTypes(List.of(new MediaType("application", "octet-stream")));
    return converter;
}

The endpoint code should look like in the snippet below:

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<Void> saveDocu2ment(
        @RequestPart PersonDTO personDTO,
        @RequestPart(value = "file") MultipartFile file
) {
    …
}

Thanks to that you get a proper OpenAPI specification and working Swagger UI (tested with Spring Boot 2.5.6, Springdoc 1.6.6).

This should also work for #820 and #964.

@springdoc springdoc locked as resolved and limited conversation to collaborators Mar 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants