File tree Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Expand file tree Collapse file tree 2 files changed +47
-1
lines changed Original file line number Diff line number Diff line change @@ -290,7 +290,7 @@ get :public_timeline do
290
290
end
291
291
```
292
292
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
294
294
XML content-types.
295
295
296
296
The request:
@@ -323,6 +323,14 @@ post "upload" do
323
323
end
324
324
```
325
325
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
+
326
334
## Parameter Validation and Coercion
327
335
328
336
You can define validations and coercion options for your parameters using a ` params ` block.
Original file line number Diff line number Diff line change @@ -395,6 +395,44 @@ def app
395
395
last_response . body . should == '{"error":"The requested content-type \'application/xml\' is not supported."}'
396
396
end
397
397
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
+
398
436
end
399
437
400
438
describe '#error!' do
You can’t perform that action at this time.
0 commit comments