OpenAPITools / OpenAPITools/openapi-generator

[BUG][Java] Response content type always set to "application/problem+json" when multiple json types exist

Open
#9,543 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

When the OpenAPI spec contains two json types (application/json and application/problem+json), the generated code always chooses "application/problem+json" as the content type.

openapi-generator version

org.openapi.generator version "5.1.1"

OpenAPI declaration file content or url
"503": {
  "description": "Service Unavailable",
  "content": {
    "application/problem+json": {
      "schema": {
        "$ref": "#/components/schemas/RFC7807Response"
      }
    }
  }
},
"default": {
  "description": "default response",
  "content": {
    "application/json": {
      "schema": {
        "$ref": "#/components/schemas/DeviceSessionModel"
      }
    }
  }
}
Command line used for generation
java -jar openapi-generator-cli-5.0.0.jar generate -g java -i swagger.json
Steps to reproduce

Regardless of the ordering in the OpenAPI declaration file, application/problem+json appears first in the array & is set as the content type. Look at these lines in the generated api:

        final String[] localVarAccepts = { 
            "application/problem+json", "application/json"
        };
        final List<MediaType> localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/6779

Suggest a fix/enhancement

Not specifying "application/json" as a content type causes us trouble when trying to identify a RESTful request.

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

Reproduce the issue with the supplied OpenAPI declaration and Java generation command, then trace how the Java generator produces the generated API's localVarAccepts array. Verify the content-type ordering and selection when application/json and application/problem+json are both present, including reversed declaration order; done means the generated client no longer always prioritizes application/problem+json and regression coverage confirms the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.