Swagger-Editor model array with distinct types

这一生的挚爱 提交于 2019-11-27 19:34:14

问题


I have a return type which is an array of two elements. The first element is an integer and the second element is an array of dictionary with keys of sql_ident and name. The sql_ident value is an integer and the name value is a string.

I can't figure out how to model this in my response object. So it'd be like:

[
   12,
   [
      {
        "sql_ident" : 17,
        "name" : "Billy Bob"
      },
      {
        "sql_ident" : 935,
        "name" : "Tina Turner"
      } 
   ]
] 

回答1:


In OpenAPI/Swagger 2.0, array items must be of the same type, so there is no way to precisely model your response. The most you can do is to use a typeless schema for items, which means the items can be anything - numbers, objects, strings, etc. - but you can't specify the exact types for items.

definitions:
  MyResponse:
    type: array
    items: {}

In OpenAPI 3.0, multi-type arrays can be described using oneOf and anyOf:

components:
  schemas:
    MyResponse:
      type: array
      items:
        oneOf:
          - type: integer
          - type: array
            items:
              $ref: "#/components/schemas/MyDictionary"

    MyDictionary:
      type: object
      properties:
        sql_ident:
          type: string
        name:
          type: string


来源:https://stackoverflow.com/questions/41904148/swagger-editor-model-array-with-distinct-types

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