-
-
Notifications
You must be signed in to change notification settings - Fork 523
Add Content-Type to multipart/form-data request #964
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
Comments
Instead of using encoding, use encodings. Note that Since version v1.5.0, a functional DSL has been introduced. |
@bnasslahsen Thanks for your reply. Could you explain, what do you mean by telling "use encodings"?
BTW, I did the same documentation using functional DSL. Result is same - no Content-Type for json added to request when you execute it from swagger-ui.
|
You can generate multiple encodings using the encodings attribute withing the Testing the encoding from the swagger-ui is not yet supported in the swagger-ui and already answred here: #820 |
I resolved it for any @RequestPart parameter. @Bean
public OperationCustomizer operationCustomizer(ConversionService conversionService, ObjectProvider<GroupedOpenApi> groupedOpenApis) {
OperationCustomizer customizer = (operation, handlerMethod) -> {
Optional.ofNullable(operation.getRequestBody())
.map(RequestBody::getContent)
.filter(content -> content.containsKey(MediaType.MULTIPART_FORM_DATA_VALUE))
.map(content -> content.get(MediaType.MULTIPART_FORM_DATA_VALUE))
.ifPresent(multipartFormData -> {
for (MethodParameter methodParameter : handlerMethod.getMethodParameters()) {
if (MultipartResolutionDelegate.isMultipartArgument(methodParameter)) {
// ignore MultipartFile parameters
continue;
}
RequestPart requestPart = methodParameter.getParameterAnnotation(RequestPart.class);
if (requestPart == null) {
// ignore parameters without @RequestPart annotation
continue;
}
if (conversionService.canConvert(TypeDescriptor.valueOf(String.class), new TypeDescriptor(methodParameter))) {
// ignore parameters that can be converted from String to a basic type by ObjectToStringHttpMessageConverter
continue;
}
String parameterName = requestPart.name();
if (!StringUtils.hasText(parameterName)) {
parameterName = methodParameter.getParameterName();
}
if (!StringUtils.hasText(parameterName)) {
parameterName = methodParameter.getParameter().getName();
}
if (StringUtils.hasText(parameterName)) {
multipartFormData.addEncoding(parameterName, new Encoding().contentType(MediaType.APPLICATION_JSON_VALUE));
}
}
});
return operation;
};
groupedOpenApis.forEach(groupedOpenApi -> groupedOpenApi.getOperationCustomizers().add(customizer));
return customizer;
} |
Description
I'm making a controller that consumes multipart/form-data request with file and some extra information in json format.
I've made an project under spring-boot 2.3.3 and spriongdoc-openapi 1.5.0
Example of my project is here: https://github.com/shpi0/swagger-ui-test
Example of POST method with multipart data
Current behaviour
Now, when I'm trying to execute query from Swagger-ui, it produces the following query:
Problem
Is it possible to add
Content-Type
fordocument
part in Swagger-ui, as in example below?Screenshots

Here is an example how I make the request in Postman:
The text was updated successfully, but these errors were encountered: