OpenAPITools / OpenAPITools/openapi-generator
[BUG][JAVA] Different media types per HTTP status code cause misleading generation of @ApiResponse
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
I was trying to define an RFC 9457 compliant Problem object that uses the "application/problem+json" media type. In this scenario, all 4xx or 5xx HTTP status codes would lead to a predefined Object as "application/problem+json" while 2xx would return the actual payload object as "application/json".
The example below reproduces the issue with a simple GET /test that yields 200 and 404.
The generated code suggests, that both HTTP status codes would yield both "application/json" and "application/problem+json".
openapi-generator version
This has been reproduced with 7.4.0 as well as with the current HEAD (f357be4)
OpenAPI declaration file content or url
This is simplest specification to reproduce the issue (referred to as spec.yaml)
openapi: 3.1.0
info:
title: Demo
version: 1.0.0
paths:
/test:
get:
responses:
'200':
description: Success
content:
application/json:
schema:
type: string
'400':
description: Error
content:
application/problem+json:
schema:
type: string
Generation Details
- Check out and build project as described here (https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-to-test-with-the-latest-master-of-openapi-generator)
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g spring -i spec.yaml -o generated
Steps to reproduce
- Generate code as described above
generated\src\main\java\org\openapitools\api\TestApi.java
Generated code:
// ...
@Operation(
operationId = "testGet",
responses = {
@ApiResponse(responseCode = "200", description = "Success", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = String.class)),
@Content(mediaType = "application/problem+json", schema = @Schema(implementation = String.class))
}),
@ApiResponse(responseCode = "400", description = "Some error", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = String.class)),
@Content(mediaType = "application/problem+json", schema = @Schema(implementation = String.class))
})
}
)
// ...
Expected code:
// ...
@Operation(
operationId = "testGet",
responses = {
@ApiResponse(responseCode = "200", description = "Success", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = String.class)),
}),
@ApiResponse(responseCode = "400", description = "Some error", content = {
@Content(mediaType = "application/problem+json", schema = @Schema(implementation = String.class))
})
}
)
// ...
Related issues/PRs
Possible, I didn't find any
Suggest a fix
This is more of an observation than a fix: JavaSpring/api.mustache uses the produces list to generate @Content annotations with @ApiResonse. This list (templateData > operations.0.produces) contains all media types within the operation.
Debugging into org.openapitools.codegen.DefaultGenerator#processTemplateToFile suggests that (templateData > operations.0.responses.[].content) contains the actual media type for this response.
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 JavaSpring/api.mustache and inspect how templateData > operations.0.responses[].content is populated in DefaultGenerator#processTemplateToFile. Reproduce the issue with spec.yaml and inspect generated/src/main/java/org/openapitools/api/TestApi.java. Done means each @ApiResponse contains only the media type defined for its HTTP status code.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100