带有rswag的JSON Api之后

问题描述 投票:1回答:1

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

控制器期望的paramsdata[attributes][file]

有人可以帮我吗?

ruby-on-rails rspec json-api rswag
1个回答
0
投票

parameter块需要指定要插入的位置。在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
© www.soinside.com 2019 - 2024. All rights reserved.