Add a macro for complexes multiparts

This will allow to describe multipart in a short way, like JSON payload:

Before:

    put 'Creates a blog with thumbnail' do
      consumes 'multipart/form-data'
      parameter name: :title, in: :formData, type: :string, required: true
      parameter name: :content, in: :formData, type: :string, required: true
      parameter name: :file, in: :formData, type: :file, required: true

      let(:blog) { FactoryBot.build(:blog) }
      let(:title) { blog.title }
      let(:content) { blog.content }
      let(:file) { blog.file }

      ...
    end

After:

    put 'Creates a blog with thumbnail' do
      consumes 'multipart/form-data'
      parameter name: :blog, in: :formData, schema: { '$ref' => '#/definitions/blog' }

      let(:blog) { FactoryBot.attributes_for(:blog) }

      ...
    end

Your mileage may vary but you can always choose the best option.
This commit is contained in:
Gabriel Sobrinho
2020-04-16 22:00:55 -03:00
parent 7ceedab4cb
commit f8dbd98bbc
7 changed files with 110 additions and 1 deletions

View File

@@ -185,8 +185,22 @@ module Rswag
# Rather that serializing with the appropriate encoding (e.g. multipart/form-data),
# Rails test infrastructure allows us to send the values directly as a hash
# PROS: simple to implement, CONS: serialization/deserialization is bypassed in test
smart_payload = build_smart_form_payload(parameters, example)
raw_payload = build_raw_form_payload(parameters, example)
smart_payload.merge(raw_payload)
end
def build_smart_form_payload(parameters, example)
smart_tuples = parameters
.select { |p| p[:in] == :formData && p[:schema] }
.map { |p| example.send(p[:name]) }
.reduce({}, :merge)
end
def build_raw_form_payload(parameters, example)
tuples = parameters
.select { |p| p[:in] == :formData }
.select { |p| p[:in] == :formData && !p[:schema] }
.map { |p| [p[:name], example.send(p[:name])] }
Hash[tuples]
end