Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for #47 - upload button doesnt work on rails; #139

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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): Fix for not functioning upload button when using rails; by [@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
4 changes: 3 additions & 1 deletion lib/grape-swagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,11 @@ def as_markdown(description)
end

def parse_params(params, path, method)
accepted_file_classes = ['Rack::Multipart::UploadedFile', 'Hash'] # 1.st for Rack, 2nd for Rails
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a CONSTANT.


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) && accepted_file_classes.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