我想创建一个遵循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我想在下面创建代码swagger到json:
data: {
type: articles,
attributes: {
title: { type: :string },
content: { type: :string }
},
}我如何在rswag中做到这一点,在json api规范中有什么技巧可以保留吗?
发布于 2021-06-20 20:40:41
您可以创建一个schema块来验证响应(使用json-schema)语法:
schema type: :object,
properties: {
data: {
type: :object,
properties: {
type: { type: :string },
attributes: {
type: :object,
properties: {
title: { type: :string },
content: { type: :string },
}
}
}
}
}https://stackoverflow.com/questions/58903961
复制相似问题