typescript - 如何使`[key: string]: string`出现在NestJS Swagger生成中
问题描述
我有以下代表响应的类:
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.
有没有办法手动注释或使用 nest-cli 结构进行注释[key: string]: string
?
解决方案
不幸的是,装饰器似乎不能附加到索引签名上。您也可以使用additionalProperties
(参见此处),但它会生成一个嵌套对象。
class SomeResponse {
@ApiProperty({ type: 'object', additionalProperties: { type: 'string' } })
data: Record<string, string>;
}
这将被提取如下。
{
"data": {
"additionalProp1": "string",
"additionalProp2": "string",
"additionalProp3": "string"
}
}
推荐阅读
- css - yii2中的下拉列表在添加样式属性时失败
- html - 嵌套 XSL 应用模板的问题
- python - 在 Python 3 中使用 PIL 库中的图像模块时出现属性错误
- arrays - Swift Array - 将每个项目转换为一个对象并返回带有转换对象的新数组
- javascript - 为什么我可以在刻度上输入参数?
- sql - SQL Server 索引排序
- java - Kafka 和具有多种消息类型的主题(Avro):消费者(spring cloud spring 由于类路径上缺少类而失败
- ansible - 将 Gitlab CI 变量注入 Ansible
- php - 如何解决在 laravel 中找不到的请求资源
- c# - 调用 await 后 Serilog 不起作用