如何在rswag中创建嵌套对象?

时间:2019-11-17 18:46:52

标签: ruby-on-rails api swagger rswag

我想按照json api规范创建带有大张旗鼓的文档。

我的代码:

   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规范中还有其他技巧吗?

1 个答案:

答案 0 :(得分: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 },
          }
        }
      }
    }
  }