Model response containing array of different objec

2019-07-18 11:35发布

问题:

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" }
  ]
}


标签: swagger