[英]Boolean in swagger sent as string instead of boolean in NestJS
[英]How to make `[key: string]: string` appear in NestJS Swagger generation
我有以下代表響應的類:
class SomeResponse {
property1?: string;
[key: string]: string
}
我想使用 nest-cli 為這個響應生成正確的 swagger 文檔。 它適用於我所有其他simpler
屬性,即property1
在 swagger 中正確顯示。 我也嘗試直接注釋:
class SomeResponse {
property1?: string;
@ApiProperty()
[key: string]: string
}
但這是不允許的,因為這會給出: error TS1206: Decorators are not valid here.
有沒有辦法手動注釋或使用像[key: string]: string
這樣的 nest-cli 結構?
不幸的是,似乎裝飾器不能附加到索引簽名。 您也可以使用additionalProperties
(參見此處),但它會生成嵌套對象。
class SomeResponse {
@ApiProperty({ type: 'object', additionalProperties: { type: 'string' } })
data: Record<string, string>;
}
這將被提取如下。
{
"data": {
"additionalProp1": "string",
"additionalProp2": "string",
"additionalProp3": "string"
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.