add formData support

This commit is contained in:
ali.q
2017-04-15 01:34:05 +04:30
committed by ali
parent 25d8adaf8b
commit 182ee093f4
12 changed files with 154 additions and 18 deletions

View File

@@ -27,6 +27,7 @@ module Rswag
end
def build_body(example)
return build_form_data(example) if parameters_in(:formData).present?
body_parameter = parameters_in(:body).first
body_parameter.nil? ? '' : example.send(body_parameter[:name]).to_json
end
@@ -93,6 +94,10 @@ module Rswag
"#{name}=#{value.join(',')}" # csv is default
end
end
def build_form_data(example)
Hash[parameters_in(:formData).map { |p| [p[:name][/^\w+[^\[]/, 0], example.send(p[:name][/^\w+[^\[]/, 0])] }]
end
end
end
end

View File

@@ -158,6 +158,17 @@ module Rswag
end
end
context "'formData' parameter" do
before do
api_metadata[:operation][:parameters] << { name: 'comment', in: :formData, type: 'string' }
allow(example).to receive(:comment).and_return('Some comment')
end
it 'returns the example value as a hash' do
expect(body).to eq({"comment" => "Some comment"})
end
end
context "referenced 'body' parameter" do
before do
api_metadata[:operation][:parameters] << { '$ref' => '#/parameters/comment' }
@@ -171,6 +182,20 @@ module Rswag
expect(body).to eq("{\"text\":\"Some comment\"}")
end
end
context "referenced 'formData' parameter" do
before do
api_metadata[:operation][:parameters] << { '$ref' => '#/parameters/comment' }
global_metadata[:parameters] = {
'comment' => { name: 'comment', in: :formData, type: 'string' }
}
allow(example).to receive(:comment).and_return('Some comment')
end
it 'returns the example value as a json string' do
expect(body).to eq({"comment" => "Some comment"})
end
end
end
describe '#build_headers' do