你的例子是正确的。
如何记录对象中“密钥”的限制?理想情况下,我想说“这不仅仅是任意字符串,而是对应于子字符串的i d”。这有什么支持吗?
openapi假设键是字符串,但是目前(从openapi 3.0开始)没有办法限制键的内容/格式。您可以在模式中口头记录任何限制和细节
description
. 添加模式示例可以帮助说明字典/映射的外观。
"Parent": {
"additionalProperties": {
"$ref": "#/components/schemas/Child"
},
"description": "A map of `Child` schemas, where the keys are IDs that correspond to the child",
"example": {
"child1": { ... bunch of stuff ... },
"child2": { ... bunch of stuff ... },
}
有个建议要补充
support for
patternProperties
在模式中,这将允许定义密钥格式。但从openapi 3.0.2开始,它仍处于提议阶段。
如果已知可能的键名称(例如,它们是枚举的一部分),则可以将字典定义为常规对象,将键定义为单个对象属性:
// Keys can be: key1, key2, key3
"Parent": {
"key1": { "$ref": "#/components/schemas/Child" },
"key2": { "$ref": "#/components/schemas/Child" },
"key3": { "$ref": "#/components/schemas/Child" }
}