Skip to content

Commit 1d14091

Browse files
anakinjLeFnord
authored andcommitted
consumes support for PATCH (#654)
1 parent 3a93218 commit 1d14091

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* [#642](https://github.com/ruby-grape/grape-swagger/pull/642): Fix examples link in readme - [@iBublik](https://github.com/iBublik).
1414
* [#641](https://github.com/ruby-grape/grape-swagger/pull/641): Exclude default success code if http_codes define one already - [@anakinj](https://github.com/anakinj).
1515
* [#651](https://github.com/ruby-grape/grape-swagger/pull/651): Apply `values` and `default` of array params to its items - [@yewton](https://github.com/yewton).
16+
* [#654](https://github.com/ruby-grape/grape-swagger/pull/654): Allow setting the consumes for PATCH methods - [@anakinj](https://github.com/anakinj).
1617

1718
* Your contribution here.
1819

lib/grape-swagger/endpoint.rb

+4-7
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,11 @@ def produces_object(route, format)
161161
route_mime_types.present? ? route_mime_types : mime_types
162162
end
163163

164-
def consumes_object(route, format)
165-
method = route.request_method.downcase.to_sym
166-
if route.settings.dig(:description, :consumes)
167-
format = route.settings[:description][:consumes]
168-
end
169-
mime_types = GrapeSwagger::DocMethods::ProducesConsumes.call(format) if %i[post put].include?(method)
164+
SUPPORTS_CONSUMES = %i[post put patch].freeze
170165

171-
mime_types
166+
def consumes_object(route, format)
167+
return unless SUPPORTS_CONSUMES.include?(route.request_method.downcase.to_sym)
168+
GrapeSwagger::DocMethods::ProducesConsumes.call(route.settings.dig(:description, :consumes) || format)
172169
end
173170

174171
def params_object(route, path)

spec/swagger_v2/api_swagger_v2_format-content_type_spec.rb

+9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ class ProducesApi < Grape::API
4949
{ 'declared_params' => declared(params) }
5050
end
5151

52+
desc 'This uses consumes for consumes',
53+
failure: [{ code: 400, model: Entities::ApiError }],
54+
consumes: ['application/www_url_encoded'],
55+
entity: Entities::UseResponse
56+
patch '/use_consumes' do
57+
{ 'declared_params' => declared(params) }
58+
end
59+
5260
add_swagger_documentation
5361
end
5462
end
@@ -123,6 +131,7 @@ def app
123131
specify do
124132
expect(subject['paths']['/use_consumes']['post']).to include('consumes')
125133
expect(subject['paths']['/use_consumes']['post']['consumes']).to eql ['application/www_url_encoded']
134+
expect(subject['paths']['/use_consumes']['patch']['consumes']).to eql ['application/www_url_encoded']
126135
end
127136
end
128137
end

0 commit comments

Comments
 (0)