0

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
GuidoMedina
  • 117
  • 1
  • 9
  • Have you tried using `let!(:create_activities) { create_list(:activity, 10) }`? `let!` is not lazy loading so it will create the records even if you don't reference the method. – max Jul 01 '21 at 19:19
  • hey max, yes i have tried that, it still returns me an empty array, which is weird... I just want to understand a little more about how documenting with this gem works – GuidoMedina Jul 01 '21 at 19:45
  • Could you, should add `type: object` to below of `items:`. you could found a solution in this issue. https://github.com/rswag/rswag/issues/132 – smapira Sep 24 '21 at 05:46
  • @smapira thank you for your input, this was actually just a misunderstanding of me on how RSwag works. Later i understood that RSwag works with your actual database (which is why you only have it set up at the development/staging stage, and not in production). Thanks! – GuidoMedina Sep 25 '21 at 00:47

0 Answers0