我正尝试在我的RoR应用程序中为我的rswag文档创建一个带有api的show action。在运行rake rswag:specs:swaggerize之后,我在动态执行文档时遇到了问题。我的问题是,因为我不能获得模型对象的id或slug,所以我不能简单地在文档中输入它。以下是github文档中的一个示例。
path '/blogs/{id}' do
get 'Retrieves a blog' do
tags 'Blogs'
produces 'application/json', 'application/xml'
parameter name: :id, in: :path, type: :string
response '200', 'blog found' do
schema type: :object,
properties: {
id: { type: :integer },
title: { type: :string },
content: { type: :string }
},
required: [ 'id', 'title', 'content' ]
let(:id) { Blog.create(title: 'foo', content: 'bar').id }
run_test!
end上面的例子很简单,我们创建了一个Blog实例,并将id传递给参数path。但是,当我看到生成的html并尝试为我的情况传入id/slug时,我总是得到404错误,因为它找不到id。
下面是我的swagger文档的图片。

正如您在图片中看到的,我不能输入有效的slug,因为我不知道它。为了让它工作,我需要事先知道url段塞。所以我的问题是我该如何解决这个问题。最后,下面是我的代码,其中包含一个index操作和一个show操作。顺便说一句,我的api不支持post创建操作。
describe 'Companies API' do
path '/api/v1/ipo-index' do
get 'Retrieves an index of ipo company listings' do
tags 'Companies'
consumes 'application/json'
produces 'application/json'
response '200', 'ok' do
let(:companies) do
3.times do
create(:company)
end
end
run_test!
end
end
end
path '/api/v1/ipo/companies/{slug}' do
get 'Retrieves a specific company listing' do
tags 'Companies'
consumes 'application/json'
produces 'application/json'
parameter name: :slug, in: :path, type: :string, description: 'Company url slug'
let(:company) do
create(:company)
end
response '200', 'Company found' do
let(:slug) { company.slug } #### What is the slug?
run_test!
end
response '404', 'Record not found' do
let(:slug) { 'invalid-slug' }
run_test!
end
end
end
end我是swagger文档的新手,所以任何帮助都将非常感谢。谢谢。
发布于 2020-12-28 21:47:42
Swagger的try it out特性向您在swagger_helper中声明的服务器发出真正的请求。它必须知道slug名称才能发出正确的请求,并且它必须是声明的服务器数据库中存在的记录。
https://stackoverflow.com/questions/64506950
复制相似问题