diff --git a/CHANGELOG.md b/CHANGELOG.md index 43712b58..112fe55c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/lib/grape-swagger.rb b/lib/grape-swagger.rb index 84065fcb..7bf00863 100644 --- a/lib/grape-swagger.rb +++ b/lib/grape-swagger.rb @@ -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' diff --git a/spec/grape-swagger_helper_spec.rb b/spec/grape-swagger_helper_spec.rb index c010177b..5d563f56 100644 --- a/spec/grape-swagger_helper_spec.rb +++ b/spec/grape-swagger_helper_spec.rb @@ -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 diff --git a/test/api.rb b/test/api.rb index c75a06d9..ac4f8d7a 100644 --- a/test/api.rb +++ b/test/api.rb @@ -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