Skip to content
This repository
Browse code

Merge pull request #9469 from senny/9466_format_enforcing_routes

`format: true` does not override existing format constraints.
  • Loading branch information...
commit 17cff2c1b867ccf46d2c2dad4ae0ee9d506e9017 2 parents 9126d53 + afddc04
Andrew White pixeltrix authored
10 actionpack/CHANGELOG.md
Source Rendered
... ... @@ -1,5 +1,15 @@
1 1 ## Rails 4.0.0 (unreleased) ##
2 2
  3 +* `format: true` does not override existing format constraints.
  4 + Fixes #9466.
  5 +
  6 + Example:
  7 +
  8 + # this will force the .json extension
  9 + get '/json_only', to: ok, format: true, constraints: { format: /json/ }
  10 +
  11 + *Yves Senn*
  12 +
3 13 * Skip valid encoding checks for non-String parameters that come
4 14 from the matched route's defaults.
5 15 Fixes #9435.
2  actionpack/lib/action_dispatch/routing/mapper.rb
@@ -129,7 +129,7 @@ def normalize_requirements!
129 129 end
130 130
131 131 if options[:format] == true
132   - @requirements[:format] = /.+/
  132 + @requirements[:format] ||= /.+/
133 133 elsif Regexp === options[:format]
134 134 @requirements[:format] = options[:format]
135 135 elsif String === options[:format]
24 actionpack/test/dispatch/routing_test.rb
@@ -3407,6 +3407,8 @@ class TestFormatConstraints < ActionDispatch::IntegrationTest
3407 3407
3408 3408 get '/string', to: ok, constraints: { format: 'json' }
3409 3409 get '/regexp', to: ok, constraints: { format: /json/ }
  3410 + get '/json_only', to: ok, format: true, constraints: { format: /json/ }
  3411 + get '/xml_only', to: ok, format: 'xml'
3410 3412 end
3411 3413 end
3412 3414
@@ -3434,6 +3436,28 @@ def test_regexp_format_constraints
3434 3436 get 'http://www.example.com/regexp.html'
3435 3437 assert_response :not_found
3436 3438 end
  3439 +
  3440 + def test_enforce_with_format_true_with_constraint
  3441 + get 'http://www.example.com/json_only.json'
  3442 + assert_response :success
  3443 +
  3444 + get 'http://www.example.com/json_only.html'
  3445 + assert_response :not_found
  3446 +
  3447 + get 'http://www.example.com/json_only'
  3448 + assert_response :not_found
  3449 + end
  3450 +
  3451 + def test_enforce_with_string
  3452 + get 'http://www.example.com/xml_only.xml'
  3453 + assert_response :success
  3454 +
  3455 + get 'http://www.example.com/xml_only'
  3456 + assert_response :success
  3457 +
  3458 + get 'http://www.example.com/xml_only.json'
  3459 + assert_response :not_found
  3460 + end
3437 3461 end
3438 3462
3439 3463 class TestRouteDefaults < ActionDispatch::IntegrationTest

0 comments on commit 17cff2c

Please sign in to comment.
Something went wrong with that request. Please try again.