How to create different element types within Swagger 2.0 editor

安稳与你 提交于 2019-11-26 22:26:40

问题


I trying to map below JSON to Swagger YAML in swagger editor 2.0 and I am not sure how to set mixed array types into my schema

{
  "obj1": [
     "string data",
     1
    ]
}

Now, my swagger definition has,

schema:
  object1:
    type: array
    items:
      type: string

回答1:


The answer depends on which version of the OpenAPI Specification you use.

OpenAPI 2.0

OpenAPI 2.0 (Swagger 2.0) does not really support mixed-type array and parameters. The most you can do is to use a typeless schema {} for items, which means the items can be anything (except null) – numbers, objects, strings, etc. You cannot specify the exact types for items, but you can add an example of an array with different item types.

# swagger: '2.0'

obj1:
  type: array
  items: {}  # <--- means "any type" (except null)
  example:
    - string data
    - 1

Note: Typeless schema {} can only be used in body parameters and response schemas. Path, header and form parameters require a primitive type for array items.

OpenAPI 3.0

Mixed types are supported in the next version, OpenAPI 3.0, using anyOf, oneOf, and optionally nullable: true to also allow nulls.

# openapi: 3.0.1

obj1:
  type: array
  items:
    oneOf:
      - type: string
      - type: integer
    nullable: true  # If nulls are allowed


来源:https://stackoverflow.com/questions/38690802/how-to-create-different-element-types-within-swagger-2-0-editor

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