Ruby on rails 使用rswag跟踪JSON Api
我正在使用它来测试一个相当常见的RubyOnRails应用程序,但我无法找出一个相当常见的情况,即我将文件上载到端点 端点需要请求正文中通常的Ruby on rails 使用rswag跟踪JSON Api,ruby-on-rails,rspec,json-api,rswag,Ruby On Rails,Rspec,Json Api,Rswag,我正在使用它来测试一个相当常见的RubyOnRails应用程序,但我无法找出一个相当常见的情况,即我将文件上载到端点 端点需要请求正文中通常的data属性和包含二进制文件的file属性,我无法让rswag生成具有正确参数的请求 path '/documents' do post 'Creates a document' do tags 'Documents' consumes 'multipart/form-data' produces 'appl
data
属性和包含二进制文件的file
属性,我无法让rswag生成具有正确参数的请求
path '/documents' do
post 'Creates a document' do
tags 'Documents'
consumes 'multipart/form-data'
produces 'application/vnd.api+json'
parameter name: 'data',
description: 'Whatever',
attributes: {
schema: {
type: :object,
properties: {
file: { type: :binary },
},
},
}
response '201', 'success' do
let(:data) do
{
attributes: {
file: Rack::Test::UploadedFile.new($REALARGS),
},
}
end
schema "$ref": '#/definitions/post_document_responses/201'
run_test! do |_example|
# ACTUAL TEST
end
end
end
end
还有一些更简单的参数(例如授权)被正确生成,我确信rswag
被正确钩住了。
我看到的请求没有参数,我可以删除数据
参数,并且没有任何更改。我尝试了一百万种组合,结果总是一样的,我不知道发生了什么
控制器期望的参数是数据[属性][文件]
有人能帮我吗?参数
块需要指定插入位置。我在其中找到了一个对:formData
的引用,结果证明必须设置它(即,您不能使用:body
,它只发送一个空请求)
经过一番诡辩之后,我成功地让您的示例使用表单数据中的嵌套属性:
path '/documents' do
post 'Creates a document' do
tags 'Documents'
consumes 'multipart/form-data'
produces 'application/vnd.api+json'
parameter name: 'data[attributes][file]',
description: 'Whatever',
in: :formData,
attributes: {
schema: {
type: :object,
properties: {
file: { type: :binary },
},
},
}
response '201', 'success' do
let(:"data[attributes][file]") { Rack::Test::UploadedFile.new(Rails.root.join("spec/requests/api/v1/documents_post_spec.rb")) }
end
schema "$ref": '#/definitions/post_document_responses/201'
run_test! do |_example|
# ACTUAL TEST
end
end
end
end