NestJS の OpenAPI 定義で `required: false` なクエリパラメータを指定する方法
Published: 2021/9/5
問題
NestJS は、デコレーターベースで自身の openapi 仕様を定義できるが、@Query
を付与したパラメータは、そのままだと required: true
になってしまう。
解決
Automatically detect optional @Query · Issue #30 · nestjs/swagger
Am I right in thinking that you could look for a ? on the query function parameter to denote that it is optional, and pick this up to set the required value on the swagger document? It would be nic...
github.com
を見ていると、 @ApiImplicitQuery
のメソッドデコレーターを利用する必要があることが分かり、また、
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Progamming), FP (Functional Programming), and FRP (Functional Reactive Programming).
docs.nestjs.com

を見ると、このデコレーターは、今現在は @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
関連記事
NestJS における global モジュールについて
2023/8/12
OpenAPI では rails のクエリパラメータの完全サポートはできない
2021/10/29