【问题标题】:How to make `[key: string]: string` appear in NestJS Swagger generation如何使`[key: string]: string`出现在NestJS Swagger生成中
【发布时间】:2021-09-25 05:28:47
【问题描述】:

我有以下代表响应的类:

class SomeResponse {
  property1?: string;
  [key: string]: string
}

我想使用 nest-cli 为这个响应生成正确的 swagger 文档。 它适用于我的所有其他simpler 属性,即property1 在招摇中正确显示。 我也试过直接注释:

class SomeResponse {
  property1?: string;
  @ApiProperty()
  [key: string]: string
}

但这是不允许的,因为它给出:error TS1206: Decorators are not valid here.

有没有办法手动注释或使用像[key: string]: string 这样的nest-cli 结构?

【问题讨论】:

    标签: typescript swagger nestjs


    【解决方案1】:

    不幸的是,装饰器似乎不能附加到索引签名上。您也可以使用additionalProperties(参见here),但它会生成一个嵌套对象。

    class SomeResponse {
      @ApiProperty({ type: 'object', additionalProperties: { type: 'string' } })
      data: Record<string, string>;
    }
    

    这将被提取如下。

    {
      "data": {
        "additionalProp1": "string",
        "additionalProp2": "string",
        "additionalProp3": "string"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      相关资源
      最近更新 更多