nestjs / swagger

OpenAPI (Swagger) module for Nest framework (node.js) :earth_americas:

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow setting default type to response types

bddvlpr opened this issue · comments

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

Currently, any @ApiResponse decorator has a never type by default. This can be very vexatious in case of using type-safe OpenAPI clients.

@ApiOkResponse({description: "Something happened"})
@ApiBadRequestResponse({description: "Something went wrong"})
@Get()
someTest() {
    // ...
    throw new HttpException('Some test', 400);
    // ...
}

Will result in the following types being generated:

{
    responses: {
        200: // ...
        400: never
    }
}

Describe the solution you'd like

Perhaps implementing a "default exception type" or a way of globally setting the default return type would help.

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

This allows the swagger to auto-generate (primarily) exceptions more streamlined to default HttpException results (see examples below).

A generic HttpException response.

{
    message: string,
    statusCode: number
}

Or, for example, a class-validator exception.

{
    message: string[],
    error: string,
    statusCode: number
}

+1