OpenAPITools / OpenAPITools/openapi-generator

[BUG][Typescript-Node] Duplicate 'accessToken' setters for oauth2 and bearer

Open
#6,223 3 comments 4 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 (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

When both Oauth2 and Token are described in the security requirements of an endpoint, the Typescript-Node version generates both as set accessToken() methods that result in duplicate identifier exception from typescript.

openapi-generator version

4.3.1

OpenAPI declaration file content or url

If you have:

/api/endpoint/{id}:
    put:
      tags:
      - Endpoint
      summary: Some description
      operationId: doFoo
      parameters:
      - name: id
        in: path
        required: true
        schema:
          type: integer
          format: int32
      responses:
        default:
          description: default response
          content:
            application/json: {}
      security:
      - token: []
      - oauth2:
        - write:foo

Then you get the following in the generated API:

    set accessToken(token: string) {
        this.authentications.oauth2.accessToken = token;
    }

    set accessToken(accessToken: string | (() => string)) {
        this.authentications.token.accessToken = accessToken;
    }

which is not valid.


This is due to

{{#isBasicBearer}}

    set accessToken(accessToken: string | (() => string)) {
        this.authentications.{{name}}.accessToken = accessToken;
    }
{{/isBasicBearer}}
{{#isOAuth}}

    set accessToken(token: string) {
        this.authentications.{{name}}.accessToken = token;
    }
{{/isOAuth}}

In modules/openapi-generator/src/main/resources/typescript-node/api-single.mustache.

Note that this is not possible to have both in the same TS class.

Command line used for generation
openapi-generator generate -i api.yaml -g typescript-node -o src/generated/ --additional-properties=supportsES6=true
Suggest a fix

Set distinct names for oauth and token setters. E.g., accessToken and accessTokenOauth.

An alternative would be to just allow favouring one over the other (e.g., always bearer, or always oauth).

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 in modules/openapi-generator/src/main/resources/typescript-node/api-single.mustache and reproduce the issue with the supplied OpenAPI declaration and typescript-node generation command. Check how the isBasicBearer and isOAuth sections render when both security requirements are present. Done means the generated TypeScript class no longer contains duplicate accessToken setters and remains valid TypeScript.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.