OpenAPITools / OpenAPITools/openapi-generator

[BUG] [java spring (server)] Incorrect media types generated in @ApiResponse if operation specifies different responses with different media types

Open
#21,385 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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?
  • 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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

If an OpenAPI operation has two responses with different media types, then the generated Java code is incorrect in terms of the @io.swagger.v3.oas.annotations.responses.ApiResponse's content attribute - more specifically, the declared media types.

To give an example, here is an excerpt of my OpenAPI spec:

responses:
  200:
    description: successful pet lookup
    content:
      application/json:
        schema:
          $ref: '#/components/schemas/PetDetailsResponse'
  404:
    description: pet not found
    content:
      application/problem+json:
        schema:
          $ref: '#/components/schemas/ProblemDetails'

Here is the actual corresponding Java code excerpt:

responses = {
    @ApiResponse(responseCode = "200", description = "successful pet lookup", content = {
        @Content(mediaType = "application/json", schema = @Schema(implementation = PetDetailsResponse.class)),
        @Content(mediaType = "application/problem+json", schema = @Schema(implementation = PetDetailsResponse.class))
    }),
    @ApiResponse(responseCode = "404", description = "pet not found", content = {
        @Content(mediaType = "application/json", schema = @Schema(implementation = ProblemDetails.class)),
        @Content(mediaType = "application/problem+json", schema = @Schema(implementation = ProblemDetails.class))
    })

As you can see, the generator incorrectly annotates both responses with both media types. In reality, the 200 response should only produce application/json and the 404 response should only produce application/problem+json. Instead, the generator appears to take the union of all the possible media types produced by the operation.

Here is the expected corresponding Java:

responses = {
    @ApiResponse(responseCode = "200", description = "successful pet lookup", content = {
        @Content(mediaType = "application/json", schema = @Schema(implementation = PetDetailsResponse.class))
    }),
    @ApiResponse(responseCode = "404", description = "pet not found", content = {
        @Content(mediaType = "application/problem+json", schema = @Schema(implementation = ProblemDetails.class))
    })
openapi-generator version

openapi-generator:7.13.0

No regression as far as I am aware - I have observed this behaviour in multiple versions.

OpenAPI declaration file content or url
openapi: "3.0.2"
info:
  title: API for Pets
  version: "1.0"
paths:
  /pets/{petId}:
    get:
      operationId: getPetById
      summary: Pet information
      description: Provides detailed pet information.
      parameters:
        - $ref: '#/components/parameters/petId'
      responses:
        200:
          description: successful pet lookup
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PetDetailsResponse'
        404:
          description: pet not found
          content:
            application/problem+json:
              schema:
                $ref: '#/components/schemas/ProblemDetails'
Generation Details
generatorName: spring
generateApis: true
generateModels: true
generateModelTests: false
generateSupportingFiles: false
skipOperationExample: true
strictSpec: true
additionalProperties:
  interfaceOnly: "true"
  useBeanValidation: "false"
  useJakartaEe: "true"
  generateBuilders: "true"
Steps to reproduce

Run the java spring (server) generator against the minimal spec that has been provided.
In general, any operation with >=2 responses and >=2 distinct media types across the responses should reproduce the issue.

Related issues/PRs

Other issues around the handling of multiple media types have been reported before, however they are not the same issue as this. For example, issue #13156 is concerned with a response with multiple media types. In this case, different responses have a single media type, but the generated Java code erroneously assigns all possible media types to all the responses.

Suggest a fix

The specification unambiguously assigns a single media type to a single response, so, presumably, it should be possible for the generator to also assign a single media type to each response in Java.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the Java Spring (server) generator with the minimal OpenAPI spec and the listed generation settings. Trace how response content is converted into @ApiResponse annotations, then verify that each response retains only its own media type and schema in the generated Java output.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.