Skip to content

Commit

Permalink
Allow body parameter name to be specified
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Perkins committed May 5, 2017
1 parent 2f3ec04 commit 17e47d8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Features

* Your contribution here.
* [#607](https://github.com/ruby-grape/grape-swagger/pull/607): Allow body parameter name to be specified - [@tjwp](https://github.com/tjwp).

#### Fixes

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,20 @@ end
```


#### Overriding the name of the body parameter

By default, body parameters have a generated name based on the operation. For
deeply nested resources, this name can get very long. To override the name of
body parameter add `body_name: 'post_body'` after the description.

```ruby
namespace 'order' do
desc 'Create an order', body_name: 'post_body'
post do
...
end
end
```

#### Defining an endpoint as an array <a name="array" />

Expand Down
8 changes: 4 additions & 4 deletions lib/grape-swagger/doc_methods/move_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def parent_definition_of_params(params, path, route)

move_params_to_new(definition, params)

definition[:description] = route.description if route.respond_to?(:description)
definition[:description] = route.description if route.try(:description)

build_body_parameter(referenced_definition, definition_name)
build_body_parameter(referenced_definition, definition_name, route.options)
end

def move_params_to_new(definition, params)
Expand Down Expand Up @@ -150,9 +150,9 @@ def add_to_required(definition, value)
definition[:required].push(*value)
end

def build_body_parameter(reference, name)
def build_body_parameter(reference, name, options)
{}.tap do |x|
x[:name] = name
x[:name] = options[:body_name] || name
x[:in] = 'body'
x[:required] = true
x[:schema] = { '$ref' => "#/definitions/#{reference}" }
Expand Down
34 changes: 17 additions & 17 deletions spec/lib/move_params_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,13 @@

describe 'parent_definition_of_params' do
let(:path) { '/in_body' }
let(:route_options) { { requirements: {} } }
describe 'POST' do
let(:params) { paths[path][:post][:parameters] }
let(:options) do
{
method: 'POST'
}
end
let(:env) { Rack::MockRequest.env_for(path, options) }
let(:request) { Grape::Request.new(env) }
let(:route) { Grape::Router::Route.new('POST', path.dup, route_options) }

specify do
subject.to_definition(path, params, request, definitions)
subject.to_definition(path, params, route, definitions)
expect(params).to eql(
[
{ name: 'InBody', in: 'body', required: true, schema: { '$ref' => '#/definitions/postInBody' } }
Expand All @@ -118,16 +113,10 @@

describe 'POST' do
let(:params) { paths['/in_body/{key}'][:put][:parameters] }
let(:options) do
{
method: 'PUT'
}
end
let(:env) { Rack::MockRequest.env_for(path, options) }
let(:request) { Grape::Request.new(env) }
let(:route) { Grape::Router::Route.new('PUT', path.dup, route_options) }

specify do
subject.to_definition(path, params, request, definitions)
subject.to_definition(path, params, route, definitions)
expect(params).to eql(
[
{ in: 'path', name: 'key', description: nil, type: 'integer', format: 'int32', required: true },
Expand Down Expand Up @@ -214,9 +203,20 @@
{ name: name, in: 'body', required: true, schema: { '$ref' => "#/definitions/#{reference}" } }
end
specify do
parameter = subject.send(:build_body_parameter, reference, name)
parameter = subject.send(:build_body_parameter, reference, name, {})
expect(parameter).to eql expected_param
end

describe 'body_name option specified' do
let(:route_options) { { body_name: 'body' } }
let(:expected_param) do
{ name: route_options[:body_name], in: 'body', required: true, schema: { '$ref' => "#/definitions/#{reference}" } }
end
specify do
parameter = subject.send(:build_body_parameter, reference, name, route_options)
expect(parameter).to eql expected_param
end
end
end
end

Expand Down

0 comments on commit 17e47d8

Please sign in to comment.