为什么 additionalProperties 是 Swagger/OpenAPI 2.0 中表示 Dictionary/Map 的方式
Why `additionalProperties` is the way to represent Dictionary/Map in Swagger/OpenAPI 2.0
虽然我看过OpenAPI spec中的例子:
type: object
additionalProperties:
$ref: '#/definitions/ComplexModel'
我不明白 为什么 使用 additionalProperties
是 Map/Dictionary.
的正确架构
规范必须说明的关于 additionalProperties
的唯一具体内容也无济于事:
The following properties are taken from the JSON Schema definition but their definitions were adjusted to the Swagger Specification. Their definition is the same as the one from JSON Schema, only where the original definition references the JSON Schema definition, the Schema Object definition is used instead.
- items
- allOf
- properties
- additionalProperties
首先,我找到了 better explanation for additionalProperties
:
For an object, if this is given, in addition to the properties defined in properties
all other property names are allowed. Their values must each match the schema object given here. If this is not given, no other properties than those defined in properties
are allowed.
所以这就是我最终理解的方式:
使用 properties
,我们可以定义类似于 Python's namedtuple, however if we wish to have something more like Python's dict 或任何其他 hash/map 的已知属性集,其中我们无法指定有多少个键或它们是什么提前,我们应该使用additionalProperties
.
additionalProperties
将匹配任何 属性 名称(它将充当 dict
的键,而 $ref
或 type
将是dict
值的模式,并且由于每个给定对象的同名属性不应该超过一个,我们将强制执行唯一键。
请注意,与 Python 的 dict
不同,它接受任何不可变值作为键,因为这里的键本质上是 属性 名称,它们必须是字符串。 (谢谢Ted Epstein for that clarification). This limitation can be tracked down to pair := string : value
in the json specification.
陈,我觉得是对的
一些可能有用的进一步背景知识:
在 JavaScript 中,这是 JSON 的原始上下文,对象就像字符串到值的哈希映射,其中一些值是数据,其他值是函数。您可以将每个名称-值对视为 属性。但是 JavaScript 没有 类,所以 属性 名称不是预定义的,每个对象都可以有自己独立的属性集。
JSON Schema 使用properties
关键字来验证预先已知的名称-值对;并使用 additionalProperties
(或 patternProperties
,在 OpenAPI 2.0 中不受支持)来验证未知的属性。
为清楚起见:
- 地图中的属性 名称或"keys" 必须是字符串。它们不能是数字或任何其他值。
- 如您所说,属性 名称 应该 是唯一的。不幸的是,JSON 规范并不严格要求唯一性,但建议使用唯一性,并且大多数 JSON 实现都期望唯一性。更多背景 here.
properties
和additionalProperties
可以单独使用,也可以组合使用。当 additionalProperties 单独使用时,没有属性,对象本质上充当 map<string, T>
,其中 T 是 additionalProperties 子模式中描述的类型。也许这有助于回答您原来的问题。
- 根据单个模式评估对象时,如果 属性 名称与
properties
中指定的名称匹配,其值只需要针对为该[提供的子模式有效=56=]。 additionalProperties
子模式(如果提供)将仅用于验证 未 包含在 properties
映射中的属性。
- 在 Swagger 的核心 Java 库中实现的
additionalProperties
存在一些限制。我已经记录了这些限制 here.
虽然我看过OpenAPI spec中的例子:
type: object additionalProperties: $ref: '#/definitions/ComplexModel'
我不明白 为什么 使用 additionalProperties
是 Map/Dictionary.
规范必须说明的关于 additionalProperties
的唯一具体内容也无济于事:
The following properties are taken from the JSON Schema definition but their definitions were adjusted to the Swagger Specification. Their definition is the same as the one from JSON Schema, only where the original definition references the JSON Schema definition, the Schema Object definition is used instead.
- items
- allOf
- properties
- additionalProperties
首先,我找到了 better explanation for additionalProperties
:
For an object, if this is given, in addition to the properties defined in
properties
all other property names are allowed. Their values must each match the schema object given here. If this is not given, no other properties than those defined inproperties
are allowed.
所以这就是我最终理解的方式:
使用 properties
,我们可以定义类似于 Python's namedtuple, however if we wish to have something more like Python's dict 或任何其他 hash/map 的已知属性集,其中我们无法指定有多少个键或它们是什么提前,我们应该使用additionalProperties
.
additionalProperties
将匹配任何 属性 名称(它将充当 dict
的键,而 $ref
或 type
将是dict
值的模式,并且由于每个给定对象的同名属性不应该超过一个,我们将强制执行唯一键。
请注意,与 Python 的 dict
不同,它接受任何不可变值作为键,因为这里的键本质上是 属性 名称,它们必须是字符串。 (谢谢Ted Epstein for that clarification). This limitation can be tracked down to pair := string : value
in the json specification.
陈,我觉得
一些可能有用的进一步背景知识:
在 JavaScript 中,这是 JSON 的原始上下文,对象就像字符串到值的哈希映射,其中一些值是数据,其他值是函数。您可以将每个名称-值对视为 属性。但是 JavaScript 没有 类,所以 属性 名称不是预定义的,每个对象都可以有自己独立的属性集。
JSON Schema 使用properties
关键字来验证预先已知的名称-值对;并使用 additionalProperties
(或 patternProperties
,在 OpenAPI 2.0 中不受支持)来验证未知的属性。
为清楚起见:
- 地图中的属性 名称或"keys" 必须是字符串。它们不能是数字或任何其他值。
- 如您所说,属性 名称 应该 是唯一的。不幸的是,JSON 规范并不严格要求唯一性,但建议使用唯一性,并且大多数 JSON 实现都期望唯一性。更多背景 here.
properties
和additionalProperties
可以单独使用,也可以组合使用。当 additionalProperties 单独使用时,没有属性,对象本质上充当map<string, T>
,其中 T 是 additionalProperties 子模式中描述的类型。也许这有助于回答您原来的问题。- 根据单个模式评估对象时,如果 属性 名称与
properties
中指定的名称匹配,其值只需要针对为该[提供的子模式有效=56=]。additionalProperties
子模式(如果提供)将仅用于验证 未 包含在properties
映射中的属性。 - 在 Swagger 的核心 Java 库中实现的
additionalProperties
存在一些限制。我已经记录了这些限制 here.