[英]Unable to add '[]' to @Query name. Tried multiple ways
我在 API 文檔中使用@nestjs/swagger
。 當 swagger 發出請求時,我希望我的 queryParam 是features[]
而不是features
。 Nest 在路由處理程序和文檔中自動搜索@Query
。
SwaggerModule 搜索路由處理程序中的所有@Body()、@Query() 和@Param() 裝飾器以生成 API 文檔。 它還通過利用反射創建相應的 model 定義。 來自: https://docs.nestjs.com/openapi/types-and-parameters
因此,如果我的變量名稱是代碼中的features
,文檔會自動選擇它。 請求失敗,因為features
不是單個元素的數組。 如果我嘗試使用@ApiQuery
將參數單獨添加到 swagger 文檔,我可以選擇命名參數features[]
但它單獨顯示,並且我無法刪除自動添加的features
參數。
@Get('/:uuid/configs')
@ApiOperation({
summary: 'Get configs for user',
})
//manually add param with [] in the name
@ApiQuery({
name: 'features[]',
required: true,
type: [String],
})
async getUserConfigs(
//automatic pickup from here
@Query() queryParams: GetUserConfigQueryParamsDto,
) {
return {
success: true,
} as ResponseBuilder;
}
我還嘗試按需要將查詢名稱作為@Query 中的參數傳遞。
@Get('/:uuid/configs')
@ApiOperation({
summary: 'Get configs for user',
})
//manually add param with [] in the name
@ApiQuery({
name: 'features[]',
required: true,
type: [String],
})
async getUserConfigs(
//Added the desired name as a param
@Query('features[]') queryParams: GetUserConfigQueryParamsDto,
) {
return {
success: true,
} as ResponseBuilder;
}
這肯定會更改 swagger 中的名稱。 但是當我在這種情況下執行請求時, [
由於編碼而轉換為%5B
和%5D
。 所以請求失敗。
我只想要features[]
而不是兩者。 但我無法刪除@Query 或將[] 添加到變量名。 我如何只獲得features[]
?
這是GetUserConfigQueryParamsDto
jic:
export class GetUserConfigQueryParamsDto {
@ApiProperty()
@IsDefined()
@IsArray()
@ArrayNotEmpty()
features: string[];
}
在 GetUserConfigQueryParamsDto class 中 @ApiProperty() 裝飾器上面的功能添加這樣的名稱 @ApiProperty({ name: 'features[]'})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.