Skip to content

Commit 41b7519

Browse files
authored
Merge pull request #1788 from darren987469/fix_route_requirements_bug
Fix route requirements bug
2 parents 5ae64f0 + c701d48 commit 41b7519

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Your contribution here.
1010
* [#1776](https://github.com/ruby-grape/grape/pull/1776): Validate response returned by the exception handler - [@darren987469](https://github.com/darren987469).
1111
* [#1787](https://github.com/ruby-grape/grape/pull/1787): Add documented but not implemented ability to `.insert` a middleware in the stack - [@michaellennox](https://github.com/michaellennox).
12+
* [#1788](https://github.com/ruby-grape/grape/pull/1788): Fix route requirements bug - [@darren987469](https://github.com/darren987469), [@darrellnash](https://github.com/darrellnash).
1213

1314
### 1.1.0 (8/4/2018)
1415

lib/grape/endpoint.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def prepare_version
200200
end
201201

202202
def merge_route_options(**default)
203-
options[:route_options].clone.reverse_merge(**default)
203+
options[:route_options].clone.merge(**default)
204204
end
205205

206206
def map_routes
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require 'spec_helper'
2+
3+
describe Grape::Endpoint do
4+
subject { Class.new(Grape::API) }
5+
6+
def app
7+
subject
8+
end
9+
10+
context 'get' do
11+
it 'routes to a namespace param with dots' do
12+
subject.namespace ':ns_with_dots', requirements: { ns_with_dots: %r{[^\/]+} } do
13+
get '/' do
14+
params[:ns_with_dots]
15+
end
16+
end
17+
18+
get '/test.id.with.dots'
19+
expect(last_response.status).to eq 200
20+
expect(last_response.body).to eq 'test.id.with.dots'
21+
end
22+
23+
it 'routes to a path with multiple params with dots' do
24+
subject.get ':id_with_dots/:another_id_with_dots', requirements: { id_with_dots: %r{[^\/]+},
25+
another_id_with_dots: %r{[^\/]+} } do
26+
"#{params[:id_with_dots]}/#{params[:another_id_with_dots]}"
27+
end
28+
29+
get '/test.id/test2.id'
30+
expect(last_response.status).to eq 200
31+
expect(last_response.body).to eq 'test.id/test2.id'
32+
end
33+
34+
it 'routes to namespace and path params with dots, with overridden requirements' do
35+
subject.namespace ':ns_with_dots', requirements: { ns_with_dots: %r{[^\/]+} } do
36+
get ':another_id_with_dots', requirements: { ns_with_dots: %r{[^\/]+},
37+
another_id_with_dots: %r{[^\/]+} } do
38+
"#{params[:ns_with_dots]}/#{params[:another_id_with_dots]}"
39+
end
40+
end
41+
42+
get '/test.id/test2.id'
43+
expect(last_response.status).to eq 200
44+
expect(last_response.body).to eq 'test.id/test2.id'
45+
end
46+
47+
it 'routes to namespace and path params with dots, with merged requirements' do
48+
subject.namespace ':ns_with_dots', requirements: { ns_with_dots: %r{[^\/]+} } do
49+
get ':another_id_with_dots', requirements: { another_id_with_dots: %r{[^\/]+} } do
50+
"#{params[:ns_with_dots]}/#{params[:another_id_with_dots]}"
51+
end
52+
end
53+
54+
get '/test.id/test2.id'
55+
expect(last_response.status).to eq 200
56+
expect(last_response.body).to eq 'test.id/test2.id'
57+
end
58+
end
59+
end

0 commit comments

Comments
 (0)