我需要使用 Swagger 记录一个 API,该 API 使用对象映射作为输入和输出,并通过字符串键进行索引。
Example:
{
"a_property": {
"foo": {
"property_1": "a string 1",
"property_2": "a string 2"
},
"bar": {
"property_1": "a string 3",
"property_2": "a string 4"
}
}
}
“foo”和“bar”可以是任何字符串键,但它们在键集中应该是唯一的。
我知道,使用 Swagger,我可以定义一个对象数组,但这提供了不同的 API,因为这样我们就会得到如下内容:
{
"a_property": [
{
"key": "foo"
"property_1": "a string 1",
"property_2": "a string 2"
},
{
"key": "bar"
"property_1": "a string 3",
"property_2": "a string 4"
}
]
}
我已阅读“开放 API 规范”-“添加对地图数据类型 #38 的支持”页。据我了解,建议使用extraProperties,但它似乎不能满足我的需求(或者它不适用于我使用的 Swagger UI 2.1.4)。我错过了什么?
到目前为止,我找到了以下解决方法(在 Swagger JSON 中):
a_property: {
description: "This is a map that can contain several objects indexed by different keys.",
type: object,
properties: {
key: {
description: "map item",
type: "object",
properties: {
property_1: {
description: "first property",
type: string
},
property_2: {
description: "second property",
type: string
}
}
}
}
}
这几乎完成了工作,但是读者必须理解“key”可以是任何字符串,并且可以重复多次。
有更好的方法来实现我所需要的吗?