有没有办法在models部分定义HashMap或Generic Object类型? 我有一个返回产品的REST服务,这些产品可以有不同的选项。 options属性基本上是一个HashMap,其中id是选项名称,其值是选项值。
答案 0 :(得分:10)
是的,这是可能的。
在OpenAPI(fka.Swagger)2.0和3.0中,hashmap始终是<string, something>
地图:
additionalProperties
定义。假设您要描述像这样的<string, string>
哈希映射:
{
"key1": "value1",
"key2": "value2"
}
相应的OpenAPI 2.0定义将是:
definitions:
StringStringMap:
type: object
additionalProperties:
type: string
在OpenAPI 3.0中,定义为:
components:
schemas:
StringStringMap:
type: object
additionalProperties:
type: string
像这样的<string, object>
哈希映射
{
"key1": {"someData": "data", "someOtherData": true},
"key2": {"someData": "data2", "someOtherData": false}
}
将在OpenAPI 2.0中以这种方式定义:
definitions:
ComplexObject:
type: object
properties:
someData:
type: string
someOtherData:
type: boolean
StringObjectMap:
type: object
additionalProperties:
$ref: "#/definitions/ComplexObject"
并在OpenAPI 3.0中:
components:
schemas:
ComplexObject:
type: object
properties:
someData:
type: string
someOtherData:
type: boolean
StringObjectMap:
type: object
additionalProperties:
$ref: "#/definitions/ComplexObject"
我刚刚在part 4 of my OpenAPI (fka Swagger tutorial)中详细介绍了这一点。
OpenAPI (fka. Swagger) specification explains briefly this too。
答案 1 :(得分:0)
这对我有用。没有任何值类型限制
definitions:
dynamicFields:
type: object
additionalProperties: {}
示例:您可以在此处传递任何类型的值
{
"dynamicFields" : {
"accountNo": "aaaaaa3455",
"customerNo": 123455,
"isApplicable": true,
"phoneNo": [
"38859379384",
3948399448,
true
]
}
}