openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][JAVA] Content type of response seems to be ignored and always set to application/json

Open TimFaehrtBahn opened this issue 6 years ago • 6 comments

Description

The defined content type within the open api spec seems to be ignored. The generated code always contains "application/json".

openapi-generator version

openapi-generator-cli-4.3.1.jar

OpenAPI declaration file content or url
[...]
 "200": {
            "description": "Success",
            "headers": {
              "Cache-Control": {
                "description": "Controls whether and how long response can be cached by consumers, defaults to 'no-cache, no-store'.",
                "style": "simple",
                "schema": {
                  "type": "string"
                }
              }
            },
            "content": {
              "application/vnd.de.db.ris+json": {
                "schema": {
                  "$ref": "#/components/schemas/BoardPublicArrival"
                }
              }
            }
          }
[...]

##### Command line used for generation

java -jar openapi-generator-cli-4.3.1.jar generate -g java -i swagger.yml --library native --api-package innoapi.api --invoker-package innoapi.invoker --model-package innoapi.model

##### Steps to reproduce
Look in the code and the accept header.
`
localVarRequestBuilder.header("Accept", "application/json");
`

##### Related issues/PRs
/

##### Suggest a fix
Content type has to evaulated for setting the accept header.

TimFaehrtBahn avatar Jun 25 '20 08:06 TimFaehrtBahn

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

auto-labeler avatar Jun 25 '20 08:06 auto-labeler

I face the same issue. Api is defined like this:

 /output/{file}:
    parameters:
      - name: file
        in: path
        schema:
          type: string
        required: true
        description: The filename of the converted file
    get:
      tags:
        - subverter
      operationId: getFile
      summary: Get a file specified by file
      responses:
        200:
          description: The converted file
          content:
            'binary/octet-stream': {}

But the generated Kotlin code insiste on adding application/json to both content-type and accept if not set explicilty: (in my case ApiClient.kt's request() function)

        // take content-type/accept from spec or set to default (application/json) if not defined
        if (requestConfig.headers[ContentType].isNullOrEmpty()) {
            requestConfig.headers[ContentType] = JsonMediaType
        }
        if (requestConfig.headers[Accept].isNullOrEmpty()) {
            requestConfig.headers[Accept] = JsonMediaType
        }

Isn't it possible to fetch the value for Accept by looking at the response."code".content?

emongstad avatar Sep 21 '20 09:09 emongstad

@emongstad I think this belongs in a different issue as this is about the Java native template.

Content-Type and Accept are both having the same issue in Java native: localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json");

jvs64893 avatar Oct 02 '20 13:10 jvs64893

ok, I can agree on that. But I hope that if this is fixed for the java template, it will be fixed for e.g kotlin too?

emongstad avatar Oct 02 '20 13:10 emongstad

I have the same issue, native java client always sets Accept header to application/json no matter the response type in the OpenAPI Spec YML

Sheldor5 avatar Sep 12 '21 17:09 Sheldor5

I ran into this problem today while working on a company project. I managed to develop a fix for this issue. I'm about to make a PR to integrate it. I hope this fix fully resolves the issue.

Paul-Etienne avatar Jan 13 '22 17:01 Paul-Etienne