richhollis / swagger-docs

Generates swagger-ui json files for Rails APIs with a simple DSL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

thornbird opened this issue · comments

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?

commented

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

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

Already find the trick, close issue.

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