diff --git a/CHANGELOG.md b/CHANGELOG.md index c84d381b..5bc90d50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/grape-swagger/doc_methods.rb b/lib/grape-swagger/doc_methods.rb index efb850e3..a0076df7 100644 --- a/lib/grape-swagger/doc_methods.rb +++ b/lib/grape-swagger/doc_methods.rb @@ -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 diff --git a/lib/grape-swagger/doc_methods/file_params.rb b/lib/grape-swagger/doc_methods/file_params.rb new file mode 100644 index 00000000..f8a2409d --- /dev/null +++ b/lib/grape-swagger/doc_methods/file_params.rb @@ -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 + \ No newline at end of file diff --git a/lib/grape-swagger/endpoint.rb b/lib/grape-swagger/endpoint.rb index 96817317..8d19d1dd 100644 --- a/lib/grape-swagger/endpoint.rb +++ b/lib/grape-swagger/endpoint.rb @@ -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)) @@ -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 diff --git a/spec/issues/881_handle_file_params_spec.rb b/spec/issues/881_handle_file_params_spec.rb new file mode 100644 index 00000000..09ee234b --- /dev/null +++ b/spec/issues/881_handle_file_params_spec.rb @@ -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