Skip to content

Commit

Permalink
Added support for Rack::Multipart::UploadedFile parameters, fixes rub…
Browse files Browse the repository at this point in the history
  • Loading branch information
timgluz authored and dblock committed Aug 17, 2014
1 parent a19a63e commit 04566fc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Next Release

* [#139](https://github.com/tim-vandecasteele/grape-swagger/pull/139): Added support for `Rack::Multipart::UploadedFile` parameters - [@timgluz](https://github.com/timgluz).
* [#136](https://github.com/tim-vandecasteele/grape-swagger/pull/136), [#94](https://github.com/tim-vandecasteele/grape-swagger/pull/94): Recurse combination of namespaces when using mounted apps - [@renier](https://github.com/renier).
* [#100](https://github.com/tim-vandecasteele/grape-swagger/pull/100): Added ability to specify a nickname for an endpoint - [@lhorne](https://github.com/lhorne).
* [#94](https://github.com/tim-vandecasteele/grape-swagger/pull/94): Added support for namespace descriptions - [@renier](https://github.com/renier).
Expand Down
2 changes: 1 addition & 1 deletion lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def as_markdown(description)
def parse_params(params, path, method)
params ||= []
params.map do |param, value|
value[:type] = 'File' if value.is_a?(Hash) && value[:type] == 'Rack::Multipart::UploadedFile'
value[:type] = 'File' if value.is_a?(Hash) && ['Rack::Multipart::UploadedFile', 'Hash'].include?(value[:type])
items = {}

raw_data_type = value.is_a?(Hash) ? (value[:type] || 'string').to_s : 'string'
Expand Down
38 changes: 38 additions & 0 deletions spec/grape-swagger_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,44 @@ class HelperTestAPI < Grape::API
]
end

it 'parses file param' do
params = {
rack: {
type: 'Rack::Multipart::UploadedFile',
desc: 'rack file',
datafile: 'content',
required: true
},
rails: {
type: 'Hash',
desc: 'rails file',
datafile: 'content',
required: true
}
}
path = '/coolness'
method = 'POST'
expect(subject.parse_params(params, path, method)).to eq [
{
paramType: 'body',
name: :rack,
description: 'rack file',
type: 'File',
required: true,
allowMultiple: false
},
{
paramType: 'body',
name: :rails,
description: 'rails file',
type: 'File',
required: true,
allowMultiple: false
}
]

end

context 'custom type' do
before :all do
class CustomType
Expand Down
11 changes: 11 additions & 0 deletions test/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ class Api < Grape::API
get do
@@splines.values
end

# TEST api for testing uploading
# curl --form file=@splines.png http://localhost:9292/splines/upload
desc 'Update image'
post 'upload' do
filename = params[:file][:filename]
content_type 'application/octet-stream'
env['api.format'] = :binary # there's no formatter for :binary, data will be returned "as is"
header 'Content-Disposition', "attachment; filename*=UTF-8''#{URI.escape(filename)}"
params[:file][:tempfile].read
end
end

add_swagger_documentation
Expand Down

0 comments on commit 04566fc

Please sign in to comment.