OpenAPITools / OpenAPITools/openapi-generator

[BUG][TypeScript] Description

Open
#7,044 1 comment 1 reaction 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 (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

My webservice can return 'text/csv' in case of success or 'application/problem+json' in case of failure. The generated http request only keeps the json Accept header so that the response cannot be interpreted when csvc is returned in case of success.
Therefore the requests results in a "SyntaxError: Unexpected token T in JSON at position 0 at JSON.parse ()" since it is not JSON.
As a workaround, I will wrap my csv in a json.

openapi-generator version

4.3.1
not tested on previous versions

OpenAPI declaration file content or url
    responses:
        "200":
          content:
            text/csv:
              schema:
                type: string
        '404':
          content:
            "application/problem+json":
              schema:
                $ref: "#/components/schemas/Problem"
Command line used for generation

openapi-generator generate -i ./src/apis/contracts/swagger_0.4.yaml -g typescript-angular -o ./src/apis/name --api-package api --model-package model --additional-properties="ngVersion=7.2.11" --additional-properties="withInterfaces=true"

Steps to reproduce
  • creates a WS that can return both csv or json
  • call the service so it returns csv
Related issues/PRs

not found

Suggest a fix

I guess there is a reason why, but in the generated code, only the json accept header is selected by this.configuration.selectHeaderAccept(httpHeaderAccepts) instead of both :

        if (httpHeaderAcceptSelected === undefined) {
            // to determine the Accept header
            const httpHeaderAccepts: string[] = [
                'text/csv',
                'application/problem+json'
            ];
            httpHeaderAcceptSelected = this.configuration.selectHeaderAccept(httpHeaderAccepts);
        }
        if (httpHeaderAcceptSelected !== undefined) {
            headers = headers.set('Accept', httpHeaderAcceptSelected);
        }

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

Generate the provided OpenAPI example with the typescript-angular command, then inspect the generated request around configuration.selectHeaderAccept and its response parsing. Locate the corresponding generator template and verify that the generated client handles the declared text/csv success response and application/problem+json failure response without a JSON.parse error.

Written by the indexing model from the issue text.

Assessment

Tech stack
angular, openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.