Skip to content

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

Open
@joao-loureiro-ig

Description

@joao-loureiro-ig

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions