Skip to content

Commit

Permalink
Merge pull request #506 from myitcv/params_precedence_readme
Browse files Browse the repository at this point in the history
Test and docs for params precedence
  • Loading branch information
dblock committed Nov 7, 2013
2 parents 94028e9 + 9483f09 commit fd18d1c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ get :public_timeline do
end
```

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

The request:
Expand Down Expand Up @@ -323,6 +323,14 @@ post "upload" do
end
```

In the case of conflict between either of:

* route string parameters
* `GET`, `POST` and `PUT` parameters
* the contents of the request body on `POST` and `PUT`

route string parameters will have precedence.

## Parameter Validation and Coercion

You can define validations and coercion options for your parameters using a `params` block.
Expand Down
38 changes: 38 additions & 0 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,44 @@ def app
last_response.body.should == '{"error":"The requested content-type \'application/xml\' is not supported."}'
end

context 'precedence' do

before do
subject.format :json
subject.namespace '/:id' do
get do
{
params: params[:id]
}
end
post do
{
params: params[:id]
}
end
put do
{
params: params[:id]
}
end
end
end

it 'route string params have higher precedence than body params' do
post '/123', { id: 456 }.to_json
expect(JSON.parse(last_response.body)['params']).to eq '123'
put '/123', { id: 456 }.to_json
expect(JSON.parse(last_response.body)['params']).to eq '123'
end

it 'route string params have higher precedence than URL params' do
get '/123?id=456'
expect(JSON.parse(last_response.body)['params']).to eq '123'
post '/123?id=456'
expect(JSON.parse(last_response.body)['params']).to eq '123'
end
end

end

describe '#error!' do
Expand Down

0 comments on commit fd18d1c

Please sign in to comment.