Skip to content

[BUG] OpenAPI Param names can conflict with Typescript types #10584

@AlexMog

Description

@AlexMog

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • 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

If parameters names generated in OpenAPI spec are the same as a type in typescript, it will throw errors during build.

openapi-generator version

5.3.0-SNAPSHOT, didn't seems to be a regression

OpenAPI declaration file content or url
openapi: 3.0.0
paths:
  /test:
    get:
      operationId: Test
      summary: Test
      parameters:
        - name: from
          required: true
          in: query
          description: |-
            From when to retrieve.
            Format ISO 8601 (example: 2021-07-21T13:16:54.050Z)
          schema:
            format: date-time
            type: string
      responses:
        '200':
          description: ''
          content:
            application/json:
              schema:
                allOf:
                  - properties:
                      nextPageCursor:
                        type: string
                        nullable: true
                      data:
                        type: array
                        items:
                          type: object
      tags:
        - test
      security:
        - bearer: []
info:
  title: Generates error
  description: Generates error
  version: 1.0.0
  contact: {}
tags: []
servers: []
components:
  securitySchemes:
    bearer:
      scheme: bearer
      bearerFormat: JWT
      type: http
Generation Details

docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate -i /local/openapi.yaml -g typescript -o /local/gen --skip-validate-spec

Steps to reproduce
  • Create a file openapi.yaml and compy the content that is found is this issue.
  • execute docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate -i /local/openapi.yaml -g typescript -o /local/gen --skip-validate-spec
  • cd gen && yarn
  • Should generate the following generation error:
  Type 'Date' has no call signatures.

31         let middlewarePreObservable = from<RequestContext>(requestContextPromise);

This is due to the fact that "from" is a function imported by Typescript AND the name of the parameter, as you can see it in the generated code:

    public test(/*here*/ from: Date, _options?: Configuration): Observable<any> {
        const requestContextPromise = this.requestFactory.test(from, _options);

        // build promise chain
        let middlewarePreObservable = /*here*/ from<RequestContext>(requestContextPromise);
Suggest a fix

If the parameter name can conflict with an internal imported type, rename the parameter to avoid conflicts (in this case, a check that the parameter is not 'from' should do the trick)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions