通过Swagger/OpenAPI为AdditionalProperty指定多种类型 [英] Specifying multiple types for additionalProperties through Swagger/OpenAPI
问题描述
我希望在OpenAPI中表示以下JSON对象:
I am looking to represent the following JSON Object in OpenAPI:
{
"name": "Bob",
"age": 4,
...
}
属性的数量和属性名称不是完全预先确定的,因此我希望使用AdditionalProperties.但是,我不太确定如何通过OpenAPI/Swagger 2.0表示它.我试过了:
The number of properties and the property names are not fully predetermined, so I look to use additionalProperties. However, I'm not too certain how it would be represented through OpenAPI/Swagger 2.0. I tried this:
Person:
type: object
additionalProperties:
type:
- int
- string
或等效的JSON:
{
"Person": {
"type": "object",
"additionalProperties": {
"type": ["int", "string"]
}
}
}
但是那不是很有效.有什么方法可以保留我要表示的JSON对象的结构,特别是字符串和整数,而不是任意对象类型?
but that didn't quite work. Is there any way to keep the structure of the JSON Object I want to represent, for specifically strings and integers, and not arbitrary object types?
推荐答案
OpenAPI/Swagger 2.0不支持多类型值.您最多可以使用无类型架构,这意味着附加属性可以是任何内容-字符串,数字,布尔值,依此类推-但是您无法指定确切的类型.
OpenAPI/Swagger 2.0 does not support multi-type values. The most you can do is use the typeless schema, which means the additional properties can be anything - strings, numbers, booleans, and so on - but you can't specify the exact types.
Person:
type: object
additionalProperties: {}
这等效于:
Person:
type: object
您可能需要切换到支持oneOf
的OpenAPI 3.0,因此可以使用:
You may want to switch to OpenAPI 3.0, which supports oneOf
so you can use:
Person:
type: object
additionalProperties:
oneOf:
- type: string
- type: integer
这篇关于通过Swagger/OpenAPI为AdditionalProperty指定多种类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!