NestJS の OpenAPI 定義で `required: false` なクエリパラメータを指定する方法

Published: 2021/9/5


問題

NestJS は、デコレーターベースで自身の openapi 仕様を定義できるが、@Query を付与したパラメータは、そのままだと required: true になってしまう。

解決

を見ていると、 @ApiImplicitQuery のメソッドデコレーターを利用する必要があることが分かり、また、

を見ると、このデコレーターは、今現在は @ApiQuery であることが分かる。

なので、

@Controller("transactions")
export class TransactionsController {

    @ApiQuery({
        name: "limit",
        description: "The maximum number of transactions to return",
        required: false,
        type: Number
    })
    @Get("recent")
    getRecentTransactions(@Query("limit", new ParseIntPipe()) limit: Number = 10) {
        ...
    }
}

が、 required: false なクエリパラメータの定義方法。


Tags: nestjsopenapi

関連記事