1

我想按照 json api 规范创建一个带有 swagger 和 rails 的文档。

我的代码:

   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

我想在下面为 json 创建代码:

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

我如何在 rswag 中执行此操作,在 json api 规范中是否有任何技巧?

4

1 回答 1

0

您可以创建一个schema块来验证响应(使用json-schema)语法:

schema type: :object,
  properties: {
    data: {
      type: :object,
      properties: {
        type: { type: :string },
        attributes: {
          type: :object,
          properties: {
            title: { type: :string },
            content: { type: :string },
          }
        }
      }
    }
  }
于 2021-06-20T12:40:41.443 回答