Apipie / apipie-rails

Ruby on Rails API documentation tool
Apache License 2.0
2.47k stars 463 forks source link

Incorrect Representation of Array of Hashes in Swagger Output #905

Closed rahulramm closed 10 months ago

rahulramm commented 10 months ago

I am encountering an issue with the Swagger output when defining an array of hashes inside a hash in the response. The items array defined within a hash in response is being represented as an array of strings instead of an array of hashes.

Here is the Apipie definition I am using:

def_param_group :api_client_fields do
    property :id, Integer
    property :name, String
    property :created_at, String, desc: "Timestamp in ISO 8601 format"
    property :updated_at, String, desc: "Timestamp in ISO 8601 format"
  end

# return response defined in the GET method
returns code: 200, desc: "Success reply" do
    property :result, Hash do
      property :items, Array, of: Hash do
        param_group :api_client_fields
      end
      property :count, Integer, desc: "Total number of items"
      property :page, Integer, desc: "Current page number"
      property :per_page, Integer, desc: "Number of items per page"
    end
  end

Expected Behavior:

The 'items' array should be displayed as an array of hashes with the proper schema. Here is a screenshot of the expected output:

Screenshot 2023-12-24 at 6 51 19 PM

Actual behavior:

The Swagger output is incorrectly representing the items array as an array of string. Here is a screenshot of the actual output:

Screenshot 2023-12-24 at 6 54 21 PM

Contents in swagger file

"200": {
            "description": "Success reply",
            "schema": {
              "type": "object",
              "properties": {
                "result": {
                  "type": "object",
                  "properties": {
                    "items": {
                      "type": "array",
                      "items": {
                        "type": "string"
                      },
                      "required": true
                    },
                    "count": { ... },
                    "page": { ... },
                    "per_page": { ... }
                  },
                  ...
                }
              },
              ...
            }
          }
rahulramm commented 10 months ago

Apologies. This works correctly when array_of: Hash is used instead of Array, of: Hash.

mathieujobin commented 10 months ago

Thanks for the report and adding the resolution.

Lyubomyr commented 10 months ago

Having the same issue. And array_of: Hash doesn't work for requests, only for responses. So the issue is still relevant.