Swagger-Editor model array with distinct types

前端 未结 1 1464
盖世英雄少女心
盖世英雄少女心 2020-12-12 05:20

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

相关标签:
1条回答
  • 2020-12-12 05:26

    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
    
    0 讨论(0)
提交回复
热议问题