Bug Report Checklist
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 { 'basic' | 'premium' | 'enterprise', 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
Bug Report Checklist
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.tsandApi.tsfiles. This is noticeable with GET endpoints specifically, where the request is not included in amodel.tsfile. Also, when enums are in the parameters, it breaks the import. I am usinguseSingleRequestParameter=truebut it happens even if this is not set.openapi-generator version
I am calling through the
openapi-generator-clinpm 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
Generation Details
Running the following from Node
Steps to reproduce
Here is the
Default.api.tsfile. Notice the issues importing with the enums. Also, all parameters haverequired:falseexplicitly in the spec (omitting had the same results), but the TS types do not have optionality included.The .controller file is similar issues.
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