1

I wanted to create a documentation with swagger and rails following the json api specification.

My code:

   require 'swagger_helper'

    describe 'Blogs API' do

      path '/blogs' do

        post 'Creates a blog' do
          tags 'Blogs'
          consumes 'application/json', 'application/xml'
          parameter name: :blog, in: :body, schema: {
            properties: {
              data: {
                type: :object,
                items: {
                  properties: {
                    title: { type: :string },
                    content: { type: :string }
                  },
                  required: ['data','title']
                }
              },
            },
          }
          response '201', 'blog created' do
            let(:blog) { { title: 'foo', content: 'bar' } }
            run_test!
          end
      response '422', 'invalid request' do
        let(:blog) { { title: 'foo' } }
        run_test!
      end
    end
  end 
end

I want create code swagger to json below:

data: {
    type: articles,
    attributes: {
      title: { type: :string },
      content: { type: :string }
    },
  }

How do I do this in rswag, is there any trick to leave in the json api specification?

Davi Luis
  • 396
  • 3
  • 16

1 Answers1

1

You can create a schema block to validate the response (using json-schema) syntax:

schema type: :object,
  properties: {
    data: {
      type: :object,
      properties: {
        type: { type: :string },
        attributes: {
          type: :object,
          properties: {
            title: { type: :string },
            content: { type: :string },
          }
        }
      }
    }
  }
bugged
  • 63
  • 1
  • 7