gpt4 book ai didi

ruby-on-rails - 使用 rswag 关注 JSON Api

转载 作者:行者123 更新时间:2023-12-04 10:38:16 27 4
gpt4 key购买 nike

我正在使用 rswag测试一个相当常见的 RubyOnRails JSON Api应用程序,但我想不出将文件上传到端点的常见情况。

端点需要请求正文中的常用 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 已正确挂接。我看到的请求没有参数,我可以删除 data 参数并且没有任何变化。我已经尝试了一百万种组合,结果总是一样,但我不知道发生了什么。

Controller 期望的paramsdata[attributes][file]

谁能帮帮我?

最佳答案

parameter block 需要指定插入的位置。有一个 example spec在那里我找到了对 :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

关于ruby-on-rails - 使用 rswag 关注 JSON Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60060942/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com