的映射我需要使用Swagger爲使用字符串鍵索引的對象映射使用(既作爲輸入又作爲輸出)的API文檔。Swagger:<string,Object>
例子:
{
"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」和「酒吧」可以是任何字符串鍵,但他們應該是一組鍵中是唯一的。
我知道,有揚鞭,我可以定義對象的數組,但是這給不同的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"
}
]
}
我看了'Open API Specification' - 'Add support for Map data types #38'頁面。據我瞭解,它建議使用additionalProperties,但它似乎不回答我的需要(或它不適用於我使用的Swagger UI 2.1.4)。 我錯過了什麼嗎?
到目前爲止,我已經找到了以下變通(在揚鞭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
}
}
}
}
}
這幾乎做工作,但讀者必須明白「鍵」可以是任何字符串,可以重複多次。
有沒有更好的方法來實現我所需要的?
個人情況下,我花了一些時間瞭解*爲什麼''additionalProperties'是正確答案:http://stackoverflow.com/a/41240118/110488 –