Skip to content
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

[BUG][JAVA][SPRING] Generated code shouldn't list application/problem+json content type #20742

Open
5 of 6 tasks
pvdbosch opened this issue Feb 26, 2025 · 0 comments
Open
5 of 6 tasks

Comments

@pvdbosch
Copy link

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

#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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant