Model response containing array of different object types in swagger

旧巷老猫 提交于 2019-12-12 21:47:45

问题


I want to model a response object containing an array of different types of objects in swagger, something like this:

{
   "table": [
        {
          "user" : []
        },
        {
          "customer": []
        },
        {
           "employee": []
        }
    ]
}

I have tried a solution below but it wraps all the properties in a single object { [ { "user": [], "customer": [] } ] }.

  responses:
    200:
      schema:
        type: array
        items:
          type: object
          properties:
            user:
              type: array
              items:
                $ref: '#/definitions/User'
            customer:
              type: array
              items:
                $ref: '#/definitions/Customer'
            employee:
              type: array
              items:
                $ref: '#/definitions/Employee' 

回答1:


That will be supported in the next release of OpenAPI spec (3.0) and here is the related discussion about this feature:

https://github.com/OAI/OpenAPI-Specification/issues/57

Here is an example (provided in the URL above):

{
  "oneOf": [
    { "$ref": "Cat" },
    { "$ref": "Dog" }
  ]
}


来源:https://stackoverflow.com/questions/41129403/model-response-containing-array-of-different-object-types-in-swagger

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