Skip to content

Commit

Permalink
Merge pull request #988 from u2/duplicating_endpoints
Browse files Browse the repository at this point in the history
Fixed duplicating endpoints
  • Loading branch information
dblock committed Apr 17, 2015
2 parents c96d42b + 47b471b commit 8ee8be7
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Metrics/BlockNesting:
# Offense count: 4
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 243
Max: 245

# Offense count: 15
Metrics/CyclomaticComplexity:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#### Fixes

* [#988](/~https://github.com/intridea/grape/pull/988): Fixed duplicate identical endpoints - [@u2](/~https://github.com/u2).
* [#936](/~https://github.com/intridea/grape/pull/936): Fixed default params processing for optional groups - [@dm1try](/~https://github.com/dm1try).
* [#942](/~https://github.com/intridea/grape/pull/942): Fixed forced presence for optional params when based on a reused entity that was also required in another context - [@croeck](/~https://github.com/croeck).

Expand Down
3 changes: 2 additions & 1 deletion lib/grape/dsl/routing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ def route(methods, paths = ['/'], route_options = {}, &block)
}).deep_merge(route_setting(:description) || {}).deep_merge(route_options || {})
}

endpoints << Grape::Endpoint.new(inheritable_setting, endpoint_options, &block)
new_endpoint = Grape::Endpoint.new(inheritable_setting, endpoint_options, &block)
endpoints << new_endpoint unless endpoints.any?{ |e| e.equals?(new_endpoint) }

route_end
reset_validations!
Expand Down
4 changes: 4 additions & 0 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ def endpoints
end
end

def equals?(e)
(options == e.options) && (inheritable_setting.to_hash == e.inheritable_setting.to_hash)
end

protected

def run(env)
Expand Down
7 changes: 6 additions & 1 deletion spec/grape/dsl/routing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,17 @@ class Dummy
.to change{ subject.endpoints.count }.from(0).to(1)
end

it 'does not duplicate identical endpoints' do
subject.route(:any)
expect { subject.route(:any) }
.to_not change(subject.endpoints, :count)
end

it 'generates correct endpoint options' do
allow(subject).to receive(:route_setting).with(:description).and_return(fiz: 'baz')
allow(Grape::DSL::Configuration).to receive(:stacked_hash_to_hash).and_return(nuz: 'naz')

expect(Grape::Endpoint).to receive(:new) do |inheritable_setting, endpoint_options|
puts endpoint_options
expect(endpoint_options[:method]).to eq :get
expect(endpoint_options[:path]).to eq '/foo'
expect(endpoint_options[:for]).to eq subject
Expand Down

0 comments on commit 8ee8be7

Please sign in to comment.