Skip to content

Fix route requirements bug #1788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Your contribution here.
* [#1776](https://github.com/ruby-grape/grape/pull/1776): Validate response returned by the exception handler - [@darren987469](https://github.com/darren987469).
* [#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).
* [#1788](https://github.com/ruby-grape/grape/pull/1788): Fix route requirements bug - [@darren987469](https://github.com/darren987469), [@darrellnash](https://github.com/darrellnash).

### 1.1.0 (8/4/2018)

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def prepare_version
end

def merge_route_options(**default)
options[:route_options].clone.reverse_merge(**default)
options[:route_options].clone.merge(**default)
end

def map_routes
Expand Down
59 changes: 59 additions & 0 deletions spec/grape/api/routes_with_requirements_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'spec_helper'

describe Grape::Endpoint do
subject { Class.new(Grape::API) }

def app
subject
end

context 'get' do
it 'routes to a namespace param with dots' do
subject.namespace ':ns_with_dots', requirements: { ns_with_dots: %r{[^\/]+} } do
get '/' do
params[:ns_with_dots]
end
end

get '/test.id.with.dots'
expect(last_response.status).to eq 200
expect(last_response.body).to eq 'test.id.with.dots'
end

it 'routes to a path with multiple params with dots' do
subject.get ':id_with_dots/:another_id_with_dots', requirements: { id_with_dots: %r{[^\/]+},
another_id_with_dots: %r{[^\/]+} } do
"#{params[:id_with_dots]}/#{params[:another_id_with_dots]}"
end

get '/test.id/test2.id'
expect(last_response.status).to eq 200
expect(last_response.body).to eq 'test.id/test2.id'
end

it 'routes to namespace and path params with dots, with overridden requirements' do
subject.namespace ':ns_with_dots', requirements: { ns_with_dots: %r{[^\/]+} } do
get ':another_id_with_dots', requirements: { ns_with_dots: %r{[^\/]+},
another_id_with_dots: %r{[^\/]+} } do
"#{params[:ns_with_dots]}/#{params[:another_id_with_dots]}"
end
end

get '/test.id/test2.id'
expect(last_response.status).to eq 200
expect(last_response.body).to eq 'test.id/test2.id'
end

it 'routes to namespace and path params with dots, with merged requirements' do
subject.namespace ':ns_with_dots', requirements: { ns_with_dots: %r{[^\/]+} } do
get ':another_id_with_dots', requirements: { another_id_with_dots: %r{[^\/]+} } do
"#{params[:ns_with_dots]}/#{params[:another_id_with_dots]}"
end
end

get '/test.id/test2.id'
expect(last_response.status).to eq 200
expect(last_response.body).to eq 'test.id/test2.id'
end
end
end