[BUG][JAVA][webclient] UnsupportedMediaTypeException if multiple consumes media type and unknown type in first position
Bug Report Checklist
- [x] Have you provided a full/minimal spec to reproduce the issue?
- [ ] Have you validated the input using an OpenAPI validator (example)?
- [x] Have you tested with the latest master to confirm the issue still exists?
- [x] Have you searched for related issues/PRs?
- [ ] What's the actual output vs expected output?
- [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
Template : Java/webclient
We generated client side code in Java/webclient mode for an api which define multiples media types in 'consumes' part. The swagger file is like the following (I omitted the unuseful parts) :
{
"swagger": "2.0",
"paths": {
"/mypath": {
"post": {
"operationId": "post",
"consumes": [
"application/json-patch+json",
"application/json",
"text/json",
"application/*+json"
],
"produces": [
"application/json"
]
}
}
}
}
The important thing is the order of the consumes list. The first value in the example is "application/json-patch+json". In this case, the execution of the generated webclient code throws the following error :
org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/json-patch+json' not supported for bodyType=MyObject
at org.springframework.web.reactive.function.BodyInserters.unsupportedError(BodyInserters.java:391)
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
If the first value is "application/json", everything works fine.
openapi-generator version
5.0.0
OpenAPI declaration file content or url
Generation Details
below our maven configuration to generate the java webclient code :
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>5.0.0</version>
<executions>
<execution>
<id>swagger</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<invokerPackage>${utilPackage}</invokerPackage>
<generateModelTests>false</generateModelTests>
<generateApiTests>false</generateApiTests>
<generateSupportingFiles>true</generateSupportingFiles>
<modelPackage>${modelPackage}</modelPackage>
<apiPackage>${clientPackage}</apiPackage>
<generatorName>java</generatorName>
<inputSpec>${project.basedir}/src/main/swagger/swagger.json</inputSpec>
<generateModelDocumentation>false</generateModelDocumentation>
<configOptions>
<library>webclient</library>
<serializableModel>true</serializableModel>
<interfaceOnly>true</interfaceOnly>
<java8>true</java8>
<dateLibrary>java8</dateLibrary>
<performBeanValidation>true</performBeanValidation>
<useBeanValidation>true</useBeanValidation>
<serializationLibrary>jackson</serializationLibrary>
<serializableModel>true</serializableModel>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Steps to reproduce
Related issues/PRs
Suggest a fix
In client-side, if a content-type is unknown, doesn't throw an exception and try the next one.
I had the same issue. Modified ApiClient.Mustache to add this
clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder(mapper, MediaType.APPLICATION_JSON, MediaType.APPLICATION_NDJSON));
clientDefaultCodecsConfigurer.defaultCodecs().jackson2JsonDecoder(new Jackson2JsonDecoder(mapper, MediaType.APPLICATION_JSON, MediaType.APPLICATION_NDJSON));
I had a similar issue. you need a different implementation for isJsonMime in ApiClient https://github.com/OpenAPITools/openapi-generator/blob/53873ff6d89340e452126207722e1aa32618550f/modules/openapi-generator/src/main/resources/Java/ApiClient.mustache#L581
basically you have 2 options either extend generated ApiClient at point you create its instance or to make your own template with updated re-ex template
here is how you do first approach
ApiClient apiClient = new ApiClient() {
@Override
public boolean isJsonMime(String mime) {
return "application/json".equals(mime); // here you write more loose condition to pass other possible JsonMimeType
}
}
in your own generator you correct the regex to ignore unwanted context-types: https://github.com/OpenAPITools/openapi-generator/blob/53873ff6d89340e452126207722e1aa32618550f/modules/openapi-generator/src/main/resources/Java/ApiClient.mustache#L582