[英]Map strings to Array on Open API and NestJS
I aim to generate the swagger spec through a NestJS project, and It's all working until now.我的目标是通过 NestJS 项目生成 swagger 规范,直到现在一切正常。 The problem arises when I want to specify that return data is a Dict that maps a key to an array of custom objects on runtime.
当我想指定返回数据是一个在运行时将键映射到自定义对象数组的 Dict 时,问题就出现了。
I read the documentation herehttps://swagger.io/docs/specification/data-models/dictionaries/ and some related content and got that I can create a map of strings to any object, but I couldn't relate to typed Arrays.我在这里阅读了文档https://swagger.io/docs/specification/data-models/dictionaries/和一些相关内容,并了解到我可以为任何 object 创建一个 map 的字符串,但我无法与键入的 Arrays 相关联.
It's important to note that I don't know what the keys would be and how many objects would be contained in each Array, I just know that the value would be an Array of a specific type.重要的是要注意,我不知道键是什么以及每个数组中将包含多少对象,我只知道该值将是特定类型的数组。
This is an example of my data:这是我的数据示例:
{
"1":[CustomObjectType, CustomObjectType, CustomObjectType],
"5":[CustomObjectType, CustomObjectType]
}
So I think on Swagger it should be something like:所以我认为在 Swagger 上它应该是这样的:
components:
schemas:
CustomMap:
type: object
additionalProperties:
$ref: '#/components/schemas/CustomObjectType'
isArray: true
CustomObjectType:
type: object
...
But I can't get it correctly.但我无法正确理解。 If I discover how I do it in Swagger, I can figure out how to pass it to NestJS.
如果我在 Swagger 中发现我是如何做的,我就能弄清楚如何将它传递给 NestJS。
I tried to use a class as type, but it doesn't map properties:我尝试使用 class 作为类型,但它没有 map 属性:
export class CustomObjectDTO {
[x: string]: Array<CustomObjectType>;
}
My last attempt was by using @ApiResponse我最后一次尝试是使用@ApiResponse
@ApiResponse({
status: 200,
schema: {
type: 'object',
additionalProperties: {
type: getSchemaPath(CustomObjectType),
},
},
})
and it was almost what I need, but it misses that the value is actually and Array of CustomObjectType这几乎是我所需要的,但它忽略了值实际上是 CustomObjectType 的数组
Solution moved from @lia 's question post.解决方案从@lia的问题帖子中移出。
Found a solution by using value type as Array and defining the item's type as my CustomObjectType.
通过使用值类型作为数组并将项目的类型定义为我的 CustomObjectType 找到了解决方案。 Example:
例子:
components: schemas: CustomMap: type: object additionalProperties: type: array items: $ref: '#/components/schemas/CustomObjectType' CustomObjectType: type: object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.