mirror of
https://github.com/ditkrg/rswag.git
synced 2026-01-25 07:16:40 +00:00
Merge pull request #300 from sobrinho/master
Add a macro for complexes multiparts
This commit is contained in:
@@ -186,8 +186,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
|
||||
|
||||
@@ -178,6 +178,20 @@ module Rswag
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'smart form payload' do
|
||||
before do
|
||||
metadata[:operation][:consumes] = ['multipart/form-data']
|
||||
metadata[:operation][:parameters] = [{ name: 'comment', in: :formData, schema: { type: 'object' } }]
|
||||
allow(example).to receive(:comment).and_return(text: 'Some comment')
|
||||
end
|
||||
|
||||
it 'sets payload to hash of names and example values' do
|
||||
expect(request[:payload]).to eq(
|
||||
:text => 'Some comment'
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'produces content' do
|
||||
|
||||
Reference in New Issue
Block a user