OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] 204 should be a valid response for `text/plain`

Open
#22,055 2 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

I have the following endpoint public String getGlobalConfigurationValueByKey(@QueryParam("key") String key)
which returns a String with Content-Type text/plain as an HTTP 200 response.

But if there is no such entry, it returns HTTP 204 and does not add a Content-Type header.

The generated code, however, can't cope with this, complaining:

Error! The response Content-Type is supposed to be text/plain but it's not: (GET https://someh/api/v1/config/global?key=mandanten%2Ffarb) 204

which is located in

https://github.com/OpenAPITools/openapi-generator/blob/ddb15d4b9d6fa0f04090b5db3d64ea1b8a6d0ee6/modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache#L449

openapi-generator version
			<groupId>org.openapitools</groupId>
			<artifactId>openapi-generator-maven-plugin</artifactId>
			<!-- RELEASE_VERSION -->
			<version>7.16.0</version>
OpenAPI declaration file content or url
 /api/v1/config/global:
    put:
      operationId: setGlobalConfigurationValueByKey
      tags:
      - user
      - config
      parameters:
      - name: key
        in: query
        schema:
          type: string
      requestBody:
        content:
          text/plain:
            schema:
              type: string
        required: true
      responses:
        "200":
          description: OK
          content:
            text/plain:
              schema:
                type: string
        "401":
          description: Not Authorized
        "403":
          description: Not Allowed
      summary: Set Global Configuration Value By Key
      security:
      - ElexisEnvironment:
        - api-access
        - user
      description: "🔓 **Roles Required:** api-access,user<br>"
    get:
      operationId: getGlobalConfigurationValueByKey
      tags:
      - user
      - config
      parameters:
      - name: key
        in: query
        schema:
          type: string
      responses:
        "200":
          description: OK
          content:
            text/plain:
              schema:
                type:
                - string
        "401":
          description: Not Authorized
        "403":
          description: Not Allowed
      summary: Get Global Configuration Value By Key
      security:
      - ElexisEnvironment:
        - api-access
        - user
      description: "🔓 **Roles Required:** api-access,user<br>"
Suggest a fix

I propose accepting a 204, and adding the following code wich seems legit, and works for me as a solution.

        if(204==localVarResponse.statusCode()) {
        	return new ApiResponse<String>(
                    localVarResponse.statusCode(),
                    localVarResponse.headers().map(),
                    null
            );
        }

If anyone is willing to take it, I'll prepara a pull 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

Start at modules/openapi-generator/src/main/resources/Java/libraries/native/api.mustache around line 449, using the provided OpenAPI declaration and generated Java client scenario. Verify the behavior for a 204 response with text/plain, then add coverage for the case and confirm the generated client accepts the response and returns the expected empty value.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.