Skip to content

[BUG] Angular-typescript generator generates invalid code #9259

@EmmanuelRoux

Description

@EmmanuelRoux
Description

Using angular-typescript generator leads to invalid code throwing following error at runtime :
ERROR TypeError: Cannot read property 'apiKey' of undefined

Looking at generated source code, see Configuration class in configuration.ts does not correctly handle undefined config parameters :

    constructor(configurationParameters: ConfigurationParameters = {}) {
        this.apiKeys = configurationParameters.apiKeys; // <-- THIS MAY BE UNDEFINED
        // [...] 
        if (configurationParameters.credentials) {
            this.credentials = configurationParameters.credentials;
        }
        else {
            this.credentials = {};
        }

        // init default apiKey credential
        if (!this.credentials['apiKey']) {
            this.credentials['apiKey'] = () => {
                return this.apiKeys['apiKey'] || this.apiKeys['apiKey']; // <-- THIS IS THROWING THE ERROR
            };
        }
    }
openapi-generator version

Version 5.1.0

(npm package @openapitools/openapi-generator-cli version 2.2.5)

OpenAPI declaration file content or url
# api.yml
openapi: 3.0.0
info:
  version: '1.0'
  title: Example API
security:
  - oauthSecurity: []
  - apiKey: []
paths:
  /endpointX:
    get:
      responses:
        default:
          description: successful operation
components:
  securitySchemes:
    apiKey:
      type: apiKey
      description: API Key
      name: apiKey
      in: header
Generation Details

openapi-generator-cli generate -g typescript-angular -i api.yml

Related issues/PRs

This problem also lead to compilation errors when using strict type checking, as described in #8980 and #8983.

Suggest a fix

The fix suggested in #8980 may work

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