Skip to content

[BUG] [typescript-nestjs-server] GET apis not parsed properly. Enum imports break and optionality not maintained #22928

@Dagregerino

Description

@Dagregerino

Bug Report Checklist

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

When generating NestJS typescript server stubs, parameters which are optional in the OpenAPI spec become required in the Typescript type created in the .controller.ts and Api.ts files. This is noticeable with GET endpoints specifically, where the request is not included in a model.ts file. Also, when enums are in the parameters, it breaks the import. I am using useSingleRequestParameter=true but it happens even if this is not set.

openapi-generator version

I am calling through the openapi-generator-cli npm package. Looks like 7.17.0 version

{
  "$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "7.17.0"
  }
}
OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Minimal Example API
  version: 1.0.0

paths:
  /items:
    get:
      summary: Get items
      parameters:
        - name: name
          in: query
          required: false
          schema:
            type: string

        - name: description
          in: query
          required: false
          schema:
            type: string

        - name: quantity
          in: query
          required: false
          schema:
            type: integer
            minimum: 0

        - name: type
          in: query
          required: false
          schema:
            type: string
            enum:
              - basic
              - premium
              - enterprise

      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  properties:
                    id:
                      type: string
                    name:
                      type: string
                    type:
                      type: string
Generation Details

Running the following from Node

openapi-generator-cli generate \
  -i ./openapi.yaml \
  -g typescript-nestjs-server \
  -o ./generated \
  --additional-properties=importFileExtension=.js,supportsES6=true,useSingleRequestParameter=true
Steps to reproduce
  • Run the openapi-generator-cli with the example openapi spec. Notice it has a GET endpoint, as well as one parameter being an enum. This will output files, but there are a couple issues

Here is the Default.api.ts file. Notice the issues importing with the enums. Also, all parameters have required:false explicitly in the spec (omitting had the same results), but the TS types do not have optionality included.

// DefaultApi.ts
import { Injectable } from '@nestjs/common';
import { Observable } from 'rxjs';
import { 'basic' | 'premium' | 'enterprise', ItemsGet200ResponseInner,  } from '../models/index.js';

export type ItemsGetRequestParams = {
    name: string
    description: string
    quantity: number
    type: 'basic' | 'premium' | 'enterprise'
}

@Injectable()
export abstract class DefaultApi {
  abstract itemsGet(itemsGetRequestParams: ItemsGetRequestParams, request: Request): Array<ItemsGet200ResponseInner> | Promise<Array<ItemsGet200ResponseInner>> | Observable<Array<ItemsGet200ResponseInner>>;
} 

The .controller file is similar issues.

// DefaultApi.controller.ts
import { Body, Controller, Get, Param, Query, Req } from '@nestjs/common';
import { Observable } from 'rxjs';
import { DefaultApi } from '../api/index.js';
import { &#39;basic&#39; | &#39;premium&#39; | &#39;enterprise&#39;, ItemsGet200ResponseInner,  } from '../models/index.js';

@Controller()
export class DefaultApiController {
  constructor(private readonly defaultApi: DefaultApi) {}

  @Get('/items')
  itemsGet(@Query('name') name: string, @Query('description') description: string, @Query('quantity') quantity: number, @Query('type') type: 'basic' | 'premium' | 'enterprise', @Req() request: Request): Array<ItemsGet200ResponseInner> | Promise<Array<ItemsGet200ResponseInner>> | Observable<Array<ItemsGet200ResponseInner>> {
    return this.defaultApi.itemsGet({ name, description, quantity, type, }, request);
  }

} 
Related issues/PRs

Similar issue in another template/plugin - #18191

Suggest a fix

These mustache templates seem to have bugs, which include not maintaining optional/non-required types

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-nestjs-server/api.mustache#L10C23-L10C31

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-nestjs-server/api.mustache#L10C23-L10C31

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