我正在使用罗塞格来测试一个相当常见的RubyOnRails 阿皮应用程序,但我无法找到一个非常常见的情况,即将文件上传到端点。
端点期望在请求体中使用包含二进制文件的data属性,并且我无法使用正确的参数来生成一个请求。
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是正确的。我看到的请求没有参数,我可以删除data参数,没有任何更改。我尝试了上百万次组合,结果总是一样,我不知道发生了什么。
控制器期望的params是data[attributes][file]。
有谁可以帮我?
发布于 2020-02-06 15:28:54
parameter块需要指定插入的位置。有一个示例规范,在这里我找到了一个对: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发布于 2021-09-09 07:55:28
如果您需要发送一个带有文件的对象数组,您可以这样做:
parameter name: 'documents', in: :formData, type: :array, required: true
let(:documents) do
[
{
file: file_1,
name: '...'
},
{
file: file_2,
name: '...'
}
]
endhttps://stackoverflow.com/questions/60060942
复制相似问题