Skip to content

Commit 18341f1

Browse files
anakinjLeFnord
authored andcommitted
Fixed move_params_to_new to allow name to be defined as a Symbol (ruby-grape#476)
* Fixed move_params_to_new to allow name to be defined as a Symbol * Fixes for parameter type handling * All param names as symbols
1 parent 585432e commit 18341f1

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Metrics/AbcSize:
2828
# Offense count: 3
2929
# Configuration parameters: CountComments.
3030
Metrics/ClassLength:
31-
Max: 220
31+
Max: 222
3232

3333
# Offense count: 10
3434
Metrics/CyclomaticComplexity:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#### Fixes
88

9+
* [#476](https://github.com/ruby-grape/grape-swagger/pull/476): Fixes for handling the parameter type when body parameters are defined inside desc block - [@anakinj](https://github.com/anakinj).
910
* Your contribution here.
1011

1112
### 0.22.0 (July 12, 2016)

lib/grape-swagger/doc_methods/move_params.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def parent_definition_of_params(params, route)
3333
end
3434

3535
def move_params_to_new(definition, params)
36-
params, nested_params = params.partition { |x| !x[:name].include?('[') }
36+
params, nested_params = params.partition { |x| !x[:name].to_s.include?('[') }
3737

3838
unless params.blank?
3939
properties, required = build_properties(params)

lib/grape-swagger/endpoint.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,11 @@ def parse_request_params(params)
231231
array_key = nil
232232
params.select { |param| public_parameter?(param) }.each_with_object({}) do |param, memo|
233233
name, options = *param
234-
array_key = name.to_s if param_type_is_array?(options[:type])
234+
param_type = options[:type]
235+
param_type = param_type.to_s unless param_type.nil?
236+
array_key = name.to_s if param_type_is_array?(param_type)
235237
options[:is_array] = true if array_key && name.start_with?(array_key)
236-
memo[param.first] = options unless (options[:type] == 'Hash' || options[:type] == 'Array') && !options.key?(:documentation)
238+
memo[name] = options unless %w(Hash Array).include?(param_type) && !options.key?(:documentation)
237239
end
238240
end
239241

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require 'spec_helper'
2+
3+
describe 'body parameter definitions' do
4+
before :all do
5+
module TheBodyApi
6+
class Endpoint < Grape::API
7+
resource :endpoint do
8+
desc 'The endpoint' do
9+
headers XAuthToken: {
10+
description: 'Valdates your identity',
11+
required: true
12+
}
13+
params body_param: { type: 'String', desc: 'param', documentation: { in: 'body' } },
14+
body_type_as_const_param: { type: String, desc: 'string_param', documentation: { in: 'body' } }
15+
end
16+
17+
post do
18+
{ 'declared_params' => declared(params) }
19+
end
20+
end
21+
22+
add_swagger_documentation
23+
end
24+
end
25+
end
26+
27+
def app
28+
TheBodyApi::Endpoint
29+
end
30+
31+
subject do
32+
get '/swagger_doc'
33+
JSON.parse(last_response.body)
34+
end
35+
36+
context 'a definition is generated for the endpoints parameters defined within the desc block' do
37+
specify do
38+
expect(subject['definitions']['postEndpoint']['properties']).to eql(
39+
'body_param' => { 'type' => 'string', 'description' => 'param' },
40+
'body_type_as_const_param' => { 'type' => 'string', 'description' => 'string_param' }
41+
)
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)