Documentating an API GET request with rswag in ruby on rails 6

1.4k Views Asked by At

i'm working on a team project and we are all new to rswag, we are using JWT token as authentication and mostly basic crud-like controllers. I'm having a very hard time figuring out rswag, and what should i be documenting and what not, we have already done tests files with rspec. I have an /api/activities endpoint in which a GET request would return me a JSON response with an array of activities, my problem is, i want my rswag "Try it out" option to return me 5 activities or something, and i have tried to use factoryBot for this but with no avail(i have already done this in rspec files, i just don't really know how to interact with rswag correctly, where to put the Creation commands, whether it creates data in the test database or in my development database, does it display data from my development database or test database?)

Anyways, if some of you more experienced folks could maybe provide a good guide for me, i would really appreciate it, and also help me return an array of activities with the "Try it out" option. This is what i have so far.

require 'swagger_helper'

RSpec.describe '../integration/api/activities', type: :request do
  describe 'Activities' do
    path '/api/activities' do
      get 'Lists all activities' do
        tags 'activities'
        produces 'application/json'

        let(:create_activities) { create_list(:activity, 10) }
        response '200', 'lists activities' do
          let(:id) { Activity.first.id }
          schema type: :object,
                 properties: {
                   activities: {
                     type: :array,
                     items: {
                       properties: {
                         id: { type: :integer },
                         name: { type: :string },
                         content: { type: :string },
                         image: { type: :string }
                       },
                       required: %w[id name content]
                     }
                   }
                 }
          run_test!
        end
      end
    end
  end
end

How do you recommend i approach this problem? How would you do this? there's no real "fail" Scenario here, you either get an array full of activities or you get an empty array, this endpoint does not use authorization. My controller has a basic

@activities = Activity.all
render json: @activities

Also, this is my swagger_helper.rb as of now.

require 'rails_helper'

RSpec.configure do |config|
  config.swagger_root = Rails.root.join('swagger').to_s

  config.swagger_docs = {
    'v1/swagger.yaml' => {
      openapi: '3.0.1',
      info: {
        title: 'API V1',
        version: 'v1'
      },
      paths: {},
      servers: [
        {
          url: 'http://localhost:3000',
          variables: {
            defaultHost: {
              default: 'www.example.com'
            }
          }
        }
      ]
    }
  }
  config.swagger_format = :yaml
end
0

There are 0 best solutions below