Back

How to describe a response containing an array with Rswag
24 December 2020

On a project I work on we use swagger to document api using the rswag gem. I noticed the responses were never described, in part because most of them return an array.

I poke around trying to find how to describe it using rswag. After lots of trying and errors here is how to do it.

Rspec code

path '/api/v1/posts/{id}' do
  let(:post) { create :post }
  let(:id) { post.id }

  get 'show' do
    parameter name: :id, in: :path, type: :string, description: "post id", required: true
    response '200', 'show post with comments'do
      schema type: :object,
            properties: {
              title: { type: :string },
              content: { type: :string },
              comments: {
                type: :array,
                items: {
                  type: :object,
                  properties: {
                    content: { type: :string }
                  }
                }
              }
            }
      run_test!
    end
  end
end

Produces this

Back