OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA][SPRING] Generated code shouldn't list application/problem+json content type
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- actual output:
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
If an OpenAPI specifies application/problem+json responses for 4xx or 5xx status codes, the generated interfaces will support JSON for the normal response, instead of only for error responses, even if the OpenAPI specifies only other media types for the normal 2xx response.
I tested this both for spring-boot and spring-http-interface, and it leads to some unwanted side-effects:
- when a client calls the API with "Accept: application/problem+json", a service (using the generated classes) will attempt to create a JSON response even if the normal response is non-JSON, e.g. "image/png"
- a client using the generated spring-http-interface, will specify "application/problem+json" in its Accept request header as preferred response content type, e.g. for a generated
accept = { "application/problem+json", "image/png" }
openapi-generator version
Tested on 7.11, 7.12-SNAPSHOT and 8.0.0-SNAPSHOT
OpenAPI declaration file content or url
https://gist.github.com/pvdbosch/3b420281eaac355151370d30f7bfd192#file-openapi-yaml
The gist also contains the generated Java interfaces.
Generation Details
https://gist.github.com/pvdbosch/3b420281eaac355151370d30f7bfd192#file-pom-xml
Steps to reproduce
Generate code using provided OpenAPI.
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/issues/7141 : Code generation for Java client stubs using RestTemplate or WebClient was updated to exclude "application/problem+json" from "Accept" header during creation of requests.
The same solution isn't possible for spring-http-interface because it only has an interface, and the caller code is generated at runtime as a dynamic proxy.
Suggest a fix
For spring-http-interface, and for spring-boot with singleContentTypes: true, exclude application/problem+json from the list of content types in x-accepts in AbstractJavaCodegen.java#L1945.
For spring-boot with singleContentTypes: false, the produces variable would need to be filtered, but it is set in DefaultCodegen, so that might have a larger impact.
On a side note: setting the singleContentTypes to true doesn't seem to be effective: all content types are still included in the generated code; only sometimes in a different order.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with AbstractJavaCodegen.java around line 1945 and compare the generated spring-boot and spring-http-interface outputs from the linked OpenAPI declaration. Trace how x-accepts and the produces variable are populated, including the DefaultCodegen path. Done means application/problem+json is excluded from normal response content types while remaining available for error responses.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi, spring, spring-boot
- Domain
- api, backend, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100