首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在rswag中创建嵌套对象?

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

Stack Overflow用户
提问于 2019-11-18 02:46:53
回答 1查看 371关注 0票数 1

我想创建一个遵循json api规范的swagger和rails文档。

我的代码:

代码语言:javascript
复制
   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

我想在下面创建代码swagger到json:

代码语言:javascript
复制
data: {
    type: articles,
    attributes: {
      title: { type: :string },
      content: { type: :string }
    },
  }

我如何在rswag中做到这一点,在json api规范中有什么技巧可以保留吗?

EN

回答 1

Stack Overflow用户

发布于 2021-06-20 20:40:41

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

代码语言:javascript
复制
schema type: :object,
  properties: {
    data: {
      type: :object,
      properties: {
        type: { type: :string },
        attributes: {
          type: :object,
          properties: {
            title: { type: :string },
            content: { type: :string },
          }
        }
      }
    }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58903961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档