Skip to content

Commit e219776

Browse files
author
Tim Connor
committed
ensure complete declared params structure is present
1 parent d40d697 commit e219776

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

lib/grape/dsl/inside_route.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@ def declared_param_is_array?(params_nested_path)
9999
end
100100

101101
def should_be_empty_hash?(passed_children_params, params_nested_path)
102-
passed_children_params.empty? && declared_param_is_hash?(params_nested_path)
102+
passed_children_params.empty? && declared_param_is_empty_hash?(params_nested_path)
103103
end
104104

105-
def declared_param_is_hash?(params_nested_path)
105+
def declared_param_is_empty_hash?(params_nested_path)
106106
key = route_options_params_key(params_nested_path)
107-
route_options_params[key] && route_options_params[key][:type] == 'Hash'
107+
has_children = route_options_params.keys.any? { |k| k != key && k.start_with?(key) }
108+
109+
route_options_params[key] && route_options_params[key][:type] == 'Hash' && !has_children
108110
end
109111

110112
def route_options_params

spec/grape/endpoint_spec.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,11 @@ def app
393393

394394
get '/declared?first=present&nested[fourth]=1'
395395
expect(last_response.status).to eq(200)
396-
expect(JSON.parse(last_response.body)['nested'].keys.size).to eq 4
396+
397+
body = JSON.parse(last_response.body)
398+
expect(body['nested'].keys).to eq(%w[fourth fifth nested_two nested_arr])
399+
expect(body['nested']['nested_two'].keys).to eq(%w[sixth nested_three])
400+
expect(body['nested']['nested_two']['nested_three'].keys).to eq(%w[seventh])
397401
end
398402

399403
it 'builds nested params when given array' do
@@ -424,7 +428,7 @@ def app
424428

425429
get '/declared?first=present'
426430
expect(last_response.status).to eq(200)
427-
expect(JSON.parse(last_response.body)['nested']).to eq({})
431+
expect(JSON.parse(last_response.body)['nested']).to be_a(Hash)
428432
end
429433

430434
it 'to be an array when include_missing is true' do

0 commit comments

Comments
 (0)