swagger-api / swagger-api/swagger-codegen

[typescript-fetch] Bug: For an API method which "produces" : [ "text/plain" ], response.json() is called in the generated client code

Open
#9,258 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

For a swagger operation which produces a plain text response body (MIME type text/plain), defined as "produces" : [ "text/plain" ], the generated client code calls the json-function of the response object, which tries to decode the response body as JSON.

                    if (response.status >= 200 && response.status < 300) {
                        return response.json();
                    } else {
                        throw response;
                    }

This fails at runtime with an SyntaxError: Unexpected token x in JSON at position 0

Swagger-codegen version

2.4.2

Swagger declaration file content or url
{
  "swagger" : "2.0",
  "info" : {
    "version" : "1.0",
    "title" : "API"
  },
  "host" : "localhost:8888",
  "tags" : [ {
    "name" : "Test"
  } ],
  "schemes" : [ "http" ],
  "paths" : {
    "/test" : {
      "get" : {
        "tags" : [ "test" ],
        "summary" : "plain text test",
        "operationId" : "getPlainText",
        "produces" : [ "text/plain" ],
        "parameters" : [ {
          "name" : "key",
          "in" : "query",
          "required" : true,
          "type" : "string"
        } ],
        "responses" : {
          "200" : {
            "description" : "successful operation",
            "schema" : {
              "type" : "string"
            }
          }
        }
      }
    }
  }
}
Command line used for generation

swagger-codegen-maven-plugin:2.4.2:generate (default)
using <language>typescript-fetch</language>

Steps to reproduce

Generate typescript-fetch code from the swagger declaration file (see above)

Suggest a fix/enhancement

The response callback must either respect the MIME types specified in the produces section of the operation, or alternatively the Content-Type header of the response can be evaluated at runtime.

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 by running swagger-codegen-maven-plugin:2.4.2:generate with language typescript-fetch against the supplied Swagger declaration and inspect the generated getPlainText method. Trace how its successful response is selected and verify completion by generating a client that handles the text/plain string response without the reported JSON decode failure.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.