簡體   English   中英

如何使 `[key: string]: string` 出現在 NestJS Swagger 生成中

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM