Skip to content

Commit fd18d1c

Browse files
committed
Merge pull request #506 from myitcv/params_precedence_readme
Test and docs for params precedence
2 parents 94028e9 + 9483f09 commit fd18d1c

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ get :public_timeline do
290290
end
291291
```
292292

293-
Parameters are automatically populated from the request body on POST and PUT for form input, JSON and
293+
Parameters are automatically populated from the request body on `POST` and `PUT` for form input, JSON and
294294
XML content-types.
295295

296296
The request:
@@ -323,6 +323,14 @@ post "upload" do
323323
end
324324
```
325325

326+
In the case of conflict between either of:
327+
328+
* route string parameters
329+
* `GET`, `POST` and `PUT` parameters
330+
* the contents of the request body on `POST` and `PUT`
331+
332+
route string parameters will have precedence.
333+
326334
## Parameter Validation and Coercion
327335

328336
You can define validations and coercion options for your parameters using a `params` block.

spec/grape/endpoint_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,44 @@ def app
395395
last_response.body.should == '{"error":"The requested content-type \'application/xml\' is not supported."}'
396396
end
397397

398+
context 'precedence' do
399+
400+
before do
401+
subject.format :json
402+
subject.namespace '/:id' do
403+
get do
404+
{
405+
params: params[:id]
406+
}
407+
end
408+
post do
409+
{
410+
params: params[:id]
411+
}
412+
end
413+
put do
414+
{
415+
params: params[:id]
416+
}
417+
end
418+
end
419+
end
420+
421+
it 'route string params have higher precedence than body params' do
422+
post '/123', { id: 456 }.to_json
423+
expect(JSON.parse(last_response.body)['params']).to eq '123'
424+
put '/123', { id: 456 }.to_json
425+
expect(JSON.parse(last_response.body)['params']).to eq '123'
426+
end
427+
428+
it 'route string params have higher precedence than URL params' do
429+
get '/123?id=456'
430+
expect(JSON.parse(last_response.body)['params']).to eq '123'
431+
post '/123?id=456'
432+
expect(JSON.parse(last_response.body)['params']).to eq '123'
433+
end
434+
end
435+
398436
end
399437

400438
describe '#error!' do

0 commit comments

Comments
 (0)