-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Description
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: stringGeneration 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=trueSteps 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