Nest Swagger generates routes with undefined as response code.
See original GitHub issueBug Report
Current behavior
Adding the following decorator, where status
is optional according to the TS typings, produces an open API spec with an undefined
response code. This causes issues with the generated docs, as well as code generators that consume open API.
@ApiResponse({
description:
'Updates a Cat',
})
The open API spec generated looks something like this:
{
"openapi": "3.0.0",
"paths": {
"/": {
"get": {
"responses": { "undefined": { "description": "Updates a Cat" } }
}
}
}
}
Adding the correct status to the decorator seems the fix the issue.
Input Code
See this for a repro: https://codesandbox.io/s/trusting-wood-4gk83
See this for a live example, where the response “code” is undefined. https://4gk83.sse.codesandbox.io/api/#/default/AppController_getHello
Here is the link to the open API JSON: https://4gk83.sse.codesandbox.io/api-json
Expected behavior
Looking at the typings for ApiResponseOptions
, it basically has the following signature (simplified a bit)
export interface ApiResponseMetadata extends Omit<ResponseObject, 'description'> {
status?: number | 'default';
type?: Type<unknown> | Function | [Function] | string;
isArray?: boolean;
description?: string;
}
Status is marked optional, which I would expect means that the status will be automatically inferred based on the type of decorator used at the controller level, like @Get()
. It seems that in the response, the correct status code is being returned. It’s just the open API spec.
Environment
Nest version: 6.8.0, tested in a project at 7.0.0 as well.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (6 by maintainers)
Top GitHub Comments
@HMilbradt next time, would you mind creating a git repo? Easier to clone and play with 😃
The Swagger Module’s
@ApiResponse()
decorator doesn’t set the response code by default. The usage of@ApiResponse()
is if you want to end up setting all of the values yourself. Otherwise, there’s things like the@ApiOkResponse()
or@ApiCreatedResponse()
decorators that allow you to set up parts of the response’s possible return values. The docs show the usages here. While thestatus
is optional, there is no default value for it. @kamilmysliwiec should we possibly make thestatus
a required value?Let’s track this here https://github.com/nestjs/swagger/pull/1149