Skip to content

Commit

Permalink
ensure complete declared params structure is present
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Connor committed Sep 17, 2020
1 parent d40d697 commit e219776
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ def declared_param_is_array?(params_nested_path)
end

def should_be_empty_hash?(passed_children_params, params_nested_path)
passed_children_params.empty? && declared_param_is_hash?(params_nested_path)
passed_children_params.empty? && declared_param_is_empty_hash?(params_nested_path)
end

def declared_param_is_hash?(params_nested_path)
def declared_param_is_empty_hash?(params_nested_path)
key = route_options_params_key(params_nested_path)
route_options_params[key] && route_options_params[key][:type] == 'Hash'
has_children = route_options_params.keys.any? { |k| k != key && k.start_with?(key) }

route_options_params[key] && route_options_params[key][:type] == 'Hash' && !has_children
end

def route_options_params
Expand Down
8 changes: 6 additions & 2 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,11 @@ def app

get '/declared?first=present&nested[fourth]=1'
expect(last_response.status).to eq(200)
expect(JSON.parse(last_response.body)['nested'].keys.size).to eq 4

body = JSON.parse(last_response.body)
expect(body['nested'].keys).to eq(%w[fourth fifth nested_two nested_arr])
expect(body['nested']['nested_two'].keys).to eq(%w[sixth nested_three])
expect(body['nested']['nested_two']['nested_three'].keys).to eq(%w[seventh])
end

it 'builds nested params when given array' do
Expand Down Expand Up @@ -424,7 +428,7 @@ def app

get '/declared?first=present'
expect(last_response.status).to eq(200)
expect(JSON.parse(last_response.body)['nested']).to eq({})
expect(JSON.parse(last_response.body)['nested']).to be_a(Hash)
end

it 'to be an array when include_missing is true' do
Expand Down

0 comments on commit e219776

Please sign in to comment.