通过 Swagger/OpenAPI 为附加属性指定多种类型 [英] Specifying multiple types for additionalProperties through Swagger/OpenAPI

查看:28
本文介绍了通过 Swagger/OpenAPI 为附加属性指定多种类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在 OpenAPI 中表示以下 JSON 对象:

I am looking to represent the following JSON Object in OpenAPI:

{
  "name": "Bob",
  "age": 4,
  ...
}

属性的数量和属性名称没有完全预先确定,所以我希望使用附加属性.但是,我不太确定它将如何通过 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 3.0

OpenAPI 3.0 支持 oneOf 所以你可以使用:

Person:
  type: object
  additionalProperties:
    oneOf:
      - type: string
      - type: integer

OpenAPI 2.0

OpenAPI 2.0 不支持多类型值.您最多可以使用 无类型模式,这意味着附加属性可以是任何东西 - 字符串、数字、布尔值、等等 - 但你不能指定确切的类型.

OpenAPI 2.0

OpenAPI 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

这篇关于通过 Swagger/OpenAPI 为附加属性指定多种类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆