是的,这是可能的。
在 OpenAPI (fka. Swagger) 2.0 和 3.0 中,hashmap 始终是 <string, something> 映射:
- 键始终是字符串,不需要定义。
- 值类型是你想要的,用
additionalProperties定义。
假设你想描述一个像这样的<string, string> hashmap:
{
"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> hashmap
{
"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。