Skip to content

Commit eba6cb2

Browse files
committed
Set valid consumes if file parameter
1 parent 1eb9fe6 commit eba6cb2

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### Features
44

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

78
#### Fixes

lib/grape-swagger/doc_methods.rb

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
require 'grape-swagger/doc_methods/headers'
1515
require 'grape-swagger/doc_methods/build_model_definition'
1616
require 'grape-swagger/doc_methods/version'
17+
require 'grape-swagger/doc_methods/file_params'
1718

1819
module GrapeSwagger
1920
module DocMethods
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module GrapeSwagger
4+
module DocMethods
5+
class FileParams
6+
class << self
7+
def includes_file_param?(params)
8+
return params.any? { |x| x[:type] == 'file' }
9+
end
10+
11+
end
12+
13+
def to_formdata(params)
14+
params.each { |x| x[:in] = 'formData' if x[:in] == 'body' }
15+
end
16+
end
17+
end
18+
end
19+
end
20+

lib/grape-swagger/endpoint.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ def method_object(route, options, path)
119119
method[:produces] = produces_object(route, options[:produces] || options[:format])
120120
method[:consumes] = consumes_object(route, options[:format])
121121
method[:parameters] = params_object(route, options, path)
122+
# if any parameters are file type, automatically set consumes
123+
if method[:parameters].present? &&
124+
GrapeSwagger::DocMethods::FileParams.includes_file_param?(method[:parameters]) &&
125+
['application/x-www-form-urlencoded', 'multipart/form-data'].none? do |consume|
126+
method[:consumes].include?(consume)
127+
end
128+
method[:consumes] = ['application/x-www-form-urlencoded', 'multipart/form-data']
129+
end
122130
method[:security] = security_object(route)
123131
method[:responses] = response_object(route, options)
124132
method[:tags] = route.options.fetch(:tags, tag_object(route, path))
@@ -189,7 +197,9 @@ def params_object(route, options, path)
189197
memo << GrapeSwagger::DocMethods::ParseParams.call(param, value, path, route, @definitions)
190198
end
191199

192-
if GrapeSwagger::DocMethods::MoveParams.can_be_moved?(route.request_method, parameters)
200+
if GrapeSwagger::DocMethods::FileParams.includes_file_param?(parameters)
201+
parameters = GrapeSwagger::DocMethods::FileParams.to_formdata(parameters)
202+
elsif GrapeSwagger::DocMethods::MoveParams.can_be_moved?(route.request_method, parameters)
193203
parameters = GrapeSwagger::DocMethods::MoveParams.to_definition(path, parameters, route, @definitions)
194204
end
195205

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe '#881 handle file params' do
6+
let(:app) do
7+
Class.new(Grape::API) do
8+
namespace :issue_881 do
9+
params do
10+
requires :upload, type: File
11+
end
12+
13+
post do
14+
present params
15+
end
16+
end
17+
18+
add_swagger_documentation format: :json
19+
end
20+
end
21+
22+
subject do
23+
get '/swagger_doc'
24+
JSON.parse(last_response.body)
25+
end
26+
27+
let(:consumes) { subject['paths']['/issue_881']['post']['consumes'] }
28+
let(:parameters) { subject['paths']['/issue_881']['post']['parameters'] }
29+
30+
specify do
31+
expect(consumes).to eql(
32+
["application/x-www-form-urlencoded", "multipart/form-data"]
33+
)
34+
expect(parameters).to eql(
35+
[{"in"=>"formData", "name"=>"upload", "required"=>true, "type"=>"file"}]
36+
)
37+
end
38+
end

0 commit comments

Comments
 (0)