Closed ajeans closed 3 years ago
As a sidenote, for now I workaround this by doing the following bash voodoo after the generation.
find . -name "*ControllerApi.java" | xargs sed -i 's/{"application\/problem+json", "application\/json"}/{"application\/json", "application\/problem+json"}/g'
The standard JSON mediatype will come first and be selected.
@wing328 I just opened the same bug as this for the webclient generator (https://github.com/OpenAPITools/openapi-generator/issues/9543) & was planning on submitting a PR when I came across this one. Is there a reason this fix wasn't ported to the other client generators?
@pgadura that was my first PR and I decided to limit the scope of the PR. You're right it would make sense in other clients.
@ajeans Makes sense! I ended up making a similar PR for webclient
Bug Report Checklist
Description
So we have created a REST server based on standard spring framework conventions (RestController), the openapi spec is generated using springdoc-openapi from the server, and I managed to use the gradle openapi-generator plugin to create a "java/resttemplate" client. This is with the 4.3.1 openapi-generator plugin. Using an integration test, this worked fine so :+1:
But then we decided to make the server handle errors as described in RFC-7807 https://tools.ietf.org/html/rfc7807 As soon as I regenerated the openapi spec and the client from it, the test started failing.
Debugging this a bit, it seems that:
Adding the RFC-7807 for the error cases is properly reflected in the openapi spec, HTTP 200 declares a application/json content whereas HTTP 500 declares a application/problem+json content
The client is properly generated using the gradle plugin, the main change is that each *ControllerApi now knows that it should accept the two media types
apiClient.selectHeaderAccept
transforms{"application/problem+json", "application/json"}
into a singleMediaType.APPLICATION_PROBLEM_JSON
because of its generated implementation.As
application/problem+json
comes first,isJsonMime
returnstrue
and this is the only media type that is returned.Accept: application/problem+json
which is rejected by the server as being an unexpected media type.openapi-generator version
This is with 4.3.1. I didn't test the 5.0.0 beta as the mustache template has the same
selectHeaderAccept
implementation (cf. https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache)OpenAPI declaration file content or url
Generation Details
The gradle configuration is as follows
Steps to reproduce
I can provide a simplified gradle project if necessary.
Related issues/PRs
440 seems to be in the same group of "how do I deal with multiple mime types"
Suggest a fix
Naively, I would implement another method called
isJsonProblemMime
and only return early if the media type being tested isisJsonMime(mediaType) && !isJsonProblemMime(mediaType)
Happy to write a PR in that direction (or another) if that helps.