3

我正在使用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参数并且没有任何变化。我已经尝试了一百万种组合,总是相同的结果,我不知道发生了什么。

params控制器期望的是data[attributes][file].

谁能帮我?

4

2 回答 2

4

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
于 2020-02-06T15:28:54.130 回答
0

如果您需要发送带有文件的对象数组,您可以执行以下操作:

parameter name: 'documents', in: :formData, type: :array, required: true

let(:documents) do
  [
    {
     file: file_1,
     name: '...' 
    },
    {
     file: file_2,
     name: '...'
    }
  ]
end
于 2021-09-09T07:55:28.983 回答