Fun*_*hor 8 enums dictionary swagger
我需要定义一张地图。但我也想限制可能的键。
这是我尝试做的。
type: object
description: Key Value Map with user data. Possible values for the keys ("KEY_1", "KEY_2", "KEY_3")
additionalProperties:
type: object
Run Code Online (Sandbox Code Playgroud)
是否可以使用枚举来定义键?(映射返回 int String, Object。但这不是问题的一部分)
感谢您的帮助。
当您使用时,additionalPropertie您无法定义接受的密钥集。但如果我的理解是正确的,这个 hashmap 作为 JSON 的两个示例可能是:
JSON 示例 1:
{
"KEY_1": { ... },
"KEY_2": { ... },
"KEY_3": { ... }
}
Run Code Online (Sandbox Code Playgroud)
JSON 示例 2:
{
"KEY_1": { ... },
"KEY_3": { ... }
}
Run Code Online (Sandbox Code Playgroud)
这个具有定义的键集的哈希图基本上是一个对象,其中包含类型对象的可选(即非必需)属性,因此可以像任何其他对象一样定义:
type: object
properties:
KEY_1:
type: object
KEY_2:
type: object
KEY_3:
type: object
Run Code Online (Sandbox Code Playgroud)
注意:最好使用 $ref 来避免为每个 KEY 属性定义对象:
type: object
properties:
KEY_1:
$ref: '#/definitions/TheObjectDefinition'
KEY_2:
$ref: '#/definitions/TheObjectDefinition'
KEY_3:
$ref: '#/definitions/TheObjectDefinition'
Run Code Online (Sandbox Code Playgroud)