Skip to content

Commit

Permalink
Allow DELETE to have a params schema definition (#923)
Browse files Browse the repository at this point in the history
  • Loading branch information
numbata authored Apr 25, 2024
1 parent e2b50b3 commit b75ce7f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

* Your contribution here.
* [#922](https://github.com/ruby-grape/grape-swagger/pull/922): Force request body to be an schema object - [@numbata](https://github.com/numbata)
* [#923](https://github.com/ruby-grape/grape-swagger/pull/923): Enabled schema definitions for body parameters in DELETE requests - [@numbata](https://github.com/numbata)

### 2.0.2 (Februar 2, 2024)

Expand Down
2 changes: 1 addition & 1 deletion lib/grape-swagger/doc_methods/move_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def deletable?(param)
end

def move_methods
[:post, :put, :patch, 'POST', 'PUT', 'PATCH']
[:delete, :post, :put, :patch, 'DELETE', 'POST', 'PUT', 'PATCH']
end

def includes_body_param?(params)
Expand Down
55 changes: 55 additions & 0 deletions spec/issues/923_params_schema_definition_for_delete_action_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require 'spec_helper'

describe '#923 Body params for DELETE action' do
let(:app) do
Class.new(Grape::API) do
params do
requires :post_id, type: Integer
requires :query, type: String, documentation: { type: 'string', param_type: 'body' }
end
delete '/posts/:post_id/comments' do
{ 'declared_params' => declared(params) }
end
add_swagger_documentation format: :json
end
end

describe 'retrieves the documentation for delete parameters as a schema defintion' do
subject do
get '/swagger_doc'
JSON.parse(last_response.body)
end

specify do
expect(subject['paths']['/posts/{post_id}/comments']['delete']['parameters']).to match(
[
{
'format' => 'int32',
'in' => 'path',
'name' => 'post_id',
'type' => 'integer',
'required' => true
},
{
'name' => 'deletePostsPostIdComments',
'in' => 'body',
'required' => true,
'schema' => { '$ref' => '#/definitions/deletePostsPostIdComments' }
}
]
)

expect(subject['definitions']['deletePostsPostIdComments']).to match(
'type' => 'object',
'properties' => {
'query' => {
'type' => 'string'
}
},
'required' => ['query']
)
end
end
end
4 changes: 2 additions & 2 deletions spec/lib/move_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
end

let(:allowed_verbs) do
[:post, :put, :patch, 'POST', 'PUT', 'PATCH']
[:post, :put, :patch, :delete, 'POST', 'PUT', 'PATCH', 'DELETE']
end

let(:not_allowed_verbs) do
[:get, :delete, 'GET', 'DELETE']
[:get, 'GET']
end

describe 'movable params' do
Expand Down
2 changes: 2 additions & 0 deletions spec/support/mock_parser.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'ostruct'

module GrapeSwagger
class MockParser
attr_reader :model, :endpoint
Expand Down

0 comments on commit b75ce7f

Please sign in to comment.