【问题标题】:How create nested object in the rswag?如何在 rswag 中创建嵌套对象?
【发布时间】:2021-06-20 20:40:41
【问题描述】:

我想按照 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 规范中是否有任何技巧?

【问题讨论】:

    标签: ruby-on-rails api swagger rswag


    【解决方案1】:

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

    schema type: :object,
      properties: {
        data: {
          type: :object,
          properties: {
            type: { type: :string },
            attributes: {
              type: :object,
              properties: {
                title: { type: :string },
                content: { type: :string },
              }
            }
          }
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2018-11-25
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 2021-06-18
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 2017-01-06
      相关资源
      最近更新 更多