richhollis / swagger-docs

Generates swagger-ui json files for Rails APIs with a simple DSL.
MIT License
750 stars 150 forks source link

Response with array of models, not hash including property array of models #154

Closed thornbird closed 8 years ago

thornbird commented 8 years ago

In swagger-docs, the response only takes a code, message and model. But model is with property and value, that means, it will present as a hash json. so there is no way for a response is an array json.

Let me find example. Previously in issue #112, jstoup111 expressed below how to represent response which is array of models.

swagger_model :User do
  property :id, :integer, :required, 'Id of the user'
  property :email, :string, :required, 'email of the user'
end

swagger_model :Users do
  property :users, :array, :required, 'List of user Objects', items: { '$ref' => :User }
end

swagger_api :index do
  summary 'Returns the users'
  response :ok, nil, :Users
  response :unauthorized
end

The result would be like:

{
  users: [
    {
      id: 1,
      email: 'test@test.com'
    },
    {
      id: 2,
      email: 'test2@test.com'
    }
  ]
}

But how to express in the response includes an array of User, like below:

[
    {
      id: 1,
      email: 'test@test.com'
    },
    {
      id: 2,
      email: 'test2@test.com'
    }
  ]

In OAI/OpenAPI-Specification, the array of model response could be like below:

{
  "/pets": {
    "get": {
      "description": "Returns all pets from the system that the user has access to",
      "produces": [
        "application/json"
      ],
      "responses": {
        "200": {
          "description": "A list of pets.",
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/definitions/pet"
            }
          }
        }
      }
    }
  }
}

but swagger-docs cannot generate such response. Any way?

zhengxu001 commented 8 years ago

It seems that you want to get responses that the root JSON is itself an array, you can try:

swagger_model :User do
  property :id, :integer, :required, 'Id of the user'
  property :email, :string, :required, 'email of the user'
end

swagger_api :index do
  summary 'Returns the users'
  response :ok
  response :unauthorized
  type "array[User]" #or 'type "array"; items "$ref" => "User"'
end
thornbird commented 8 years ago

Thanks @zhengxu001 This trick or solution should be written in the ReadMe so that it won't cause more confusion.

thornbird commented 8 years ago

Already find the trick, close issue.

amukherj commented 7 years ago

Which version of swagger-docs provides support for the above syntax?