Specifying multiple types for additionalProperties through Swagger/OpenAPI

眉间皱痕 提交于 2019-12-06 06:00:44

问题


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

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

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

or the JSON equivalent:

{
  "Person": {
    "type": "object",
    "additionalProperties": {
      "type": ["int", "string"]
    }
  }
}

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?


回答1:


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: {}

This is equivalent to:

Person:
  type: object


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


来源:https://stackoverflow.com/questions/46472543/specifying-multiple-types-for-additionalproperties-through-swagger-openapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!