Skip to content

[BUG] typescript-nestjs configuration invalid if using apiKey security #17706

@simhnna

Description

@simhnna

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?
Description

Configuration.apiKeys is optional but referenced without a undefined check in the service classes.

Turns out the included code is also wrong
https://github.com/OpenAPITools/openapi-generator/blob/master/samples/client/petstore/typescript-nestjs-v6-provided-in-root/builds/default/configuration.ts#L13
and
https://github.com/OpenAPITools/openapi-generator/blob/master/samples/client/petstore/typescript-nestjs-v6-provided-in-root/builds/default/api/pet.service.ts#L258

openapi-generator version

7.2.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Swagger Petstore - OpenAPI 3.0
  version: v0
servers:
  - url: https://petstore3.swagger.io/api/v3
security:
  - api_key: []
tags:
  - name: pet
    description: Everything about your Pets
    externalDocs:
      description: Find out more
      url: http://swagger.io
paths:
  /pet:
    put:
      tags:
        - pet
      summary: Update an existing pet
      description: Update an existing pet by Id
      operationId: updatePet
      requestBody:
        description: Update an existent pet in the store
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Pet'
          application/xml:
            schema:
              $ref: '#/components/schemas/Pet'
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/Pet'
        required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'          
            application/xml:
              schema:
                $ref: '#/components/schemas/Pet'
        '400':
          description: Invalid ID supplied
        '404':
          description: Pet not found
        '422':
          description: Validation exception
components:
  schemas:
    Pet:
      required:
        - name
      type: object
      properties:
        id:
          type: integer
          format: int64
          example: 10
        name:
          type: string
          example: doggie
      xml:
        name: pet
  securitySchemes:
    api_key:
      type: apiKey
      name: api_key
      in: header
Generation Details

openapi-generator-cli generate -g typescript-nestjs -i ./spec2.yaml -o petshop --skip-validate-spec --additional-properties=fileNaming=kebab-case

Steps to reproduce
export class PetService {
   //....

  public updatePet(pet: Pet, ): Observable<any> {
      // ...
       // authentication (api_key) required
        if (this.configuration.apiKeys["api_key"]) {
            headers['api_key'] = this.configuration.apiKeys["api_key"];
        }
  }
}

export class Configuration {
    apiKeys?: {[ key: string ]: string};
    username?: string;
    password?: string;
    accessToken?: string | (() => string);
    basePath?: string;
    withCredentials?: boolean;

    constructor(configurationParameters: ConfigurationParameters = {}) {
        this.apiKeys = configurationParameters.apiKeys;
        this.username = configurationParameters.username;
        this.password = configurationParameters.password;
        this.accessToken = configurationParameters.accessToken;
        this.basePath = configurationParameters.basePath;
        this.withCredentials = configurationParameters.withCredentials;
    }
}

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