7

I didn't find any examples of how to use rswag to generate documentation according to json api.

spec/integration/pets_spec.rb

require 'swagger_helper'

It is possible to change the code to generate the format of json api?

describe 'Pets API' do

  path '/api/v1/pets' do

    post 'Creates a pet' do
      tags 'Pets'
      consumes 'application/json', 'application/xml'
      parameter name: :pet, in: :body, schema: {
        type: :object,
        properties: {
          name: { type: :string },
          photo_url: { type: :string },
          status: { type: :string }
        },
        required: [ 'name', 'status' ]
      }

      response '201', 'pet created' do
        let(:pet) { { name: 'Dodo', status: 'available' } }
        run_test!
      end

      response '422', 'invalid request' do
        let(:pet) { { name: 'foo' } }
        run_test!
      end
    end
  end

  path '/api/v1/pets/{id}' do

    get 'Retrieves a pet' do
      tags 'Pets'
      produces 'application/json', 'application/xml'
      parameter name: :id, :in => :path, :type => :string

      response '200', 'name found' do
        schema type: :object,
          properties: {
            id: { type: :integer, },
            name: { type: :string },
            photo_url: { type: :string },
            status: { type: :string }
          },
          required: [ 'id', 'name', 'status' ]

        let(:id) { Pet.create(name: 'foo', status: 'bar', photo_url: 'http://example.com/avatar.jpg').id }
        run_test!
      end

      response '404', 'pet not found' do
        let(:id) { 'invalid' }
        run_test!
      end
    end
  end
end

If rswag what tips would you give me?

Davi Luis
  • 396
  • 3
  • 16

1 Answers1

-2

You need to change the swagger_helper.rb file. Change the file format to JSON. I have attached the screenshot for that one below:

https://i.stack.imgur.com/MzOR9.png

  • this may be a misunderstanding: the question is about testing + documenting and API that follows the json.api specification found here: https://jsonapi.org/, not about the swagger.yml or swagger.json file – bjelli Mar 22 '20 at 06:04