Skip to content

Commit

Permalink
Set valid consumes if file parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
spaceraccoon committed Nov 15, 2022
1 parent 1eb9fe6 commit eba6cb2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### Features

* [#874](https://github.com/ruby-grape/grape-swagger/pull/874): Add support for wildcard segments path parameters - [@spaceraccoon](https://github.com/spaceraccoon)
* Your contribution here.

#### Fixes
Expand Down
1 change: 1 addition & 0 deletions lib/grape-swagger/doc_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require 'grape-swagger/doc_methods/headers'
require 'grape-swagger/doc_methods/build_model_definition'
require 'grape-swagger/doc_methods/version'
require 'grape-swagger/doc_methods/file_params'

module GrapeSwagger
module DocMethods
Expand Down
20 changes: 20 additions & 0 deletions lib/grape-swagger/doc_methods/file_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module GrapeSwagger
module DocMethods
class FileParams
class << self
def includes_file_param?(params)
return params.any? { |x| x[:type] == 'file' }
end

end

def to_formdata(params)
params.each { |x| x[:in] = 'formData' if x[:in] == 'body' }
end
end
end
end
end

12 changes: 11 additions & 1 deletion lib/grape-swagger/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ def method_object(route, options, path)
method[:produces] = produces_object(route, options[:produces] || options[:format])
method[:consumes] = consumes_object(route, options[:format])
method[:parameters] = params_object(route, options, path)
# if any parameters are file type, automatically set consumes
if method[:parameters].present? &&
GrapeSwagger::DocMethods::FileParams.includes_file_param?(method[:parameters]) &&
['application/x-www-form-urlencoded', 'multipart/form-data'].none? do |consume|
method[:consumes].include?(consume)
end
method[:consumes] = ['application/x-www-form-urlencoded', 'multipart/form-data']
end
method[:security] = security_object(route)
method[:responses] = response_object(route, options)
method[:tags] = route.options.fetch(:tags, tag_object(route, path))
Expand Down Expand Up @@ -189,7 +197,9 @@ def params_object(route, options, path)
memo << GrapeSwagger::DocMethods::ParseParams.call(param, value, path, route, @definitions)
end

if GrapeSwagger::DocMethods::MoveParams.can_be_moved?(route.request_method, parameters)
if GrapeSwagger::DocMethods::FileParams.includes_file_param?(parameters)
parameters = GrapeSwagger::DocMethods::FileParams.to_formdata(parameters)
elsif GrapeSwagger::DocMethods::MoveParams.can_be_moved?(route.request_method, parameters)
parameters = GrapeSwagger::DocMethods::MoveParams.to_definition(path, parameters, route, @definitions)
end

Expand Down
38 changes: 38 additions & 0 deletions spec/issues/881_handle_file_params_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require 'spec_helper'

describe '#881 handle file params' do
let(:app) do
Class.new(Grape::API) do
namespace :issue_881 do
params do
requires :upload, type: File
end

post do
present params
end
end

add_swagger_documentation format: :json
end
end

subject do
get '/swagger_doc'
JSON.parse(last_response.body)
end

let(:consumes) { subject['paths']['/issue_881']['post']['consumes'] }
let(:parameters) { subject['paths']['/issue_881']['post']['parameters'] }

specify do
expect(consumes).to eql(
["application/x-www-form-urlencoded", "multipart/form-data"]
)
expect(parameters).to eql(
[{"in"=>"formData", "name"=>"upload", "required"=>true, "type"=>"file"}]
)
end
end

0 comments on commit eba6cb2

Please sign in to comment.