Skip to content

Commit 47b471b

Browse files
committed
Fixed duplicating endpoints
1 parent 954f4c6 commit 47b471b

File tree

5 files changed

+14
-3
lines changed

5 files changed

+14
-3
lines changed

.rubocop_todo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Metrics/BlockNesting:
2626
# Offense count: 4
2727
# Configuration parameters: CountComments.
2828
Metrics/ClassLength:
29-
Max: 243
29+
Max: 245
3030

3131
# Offense count: 15
3232
Metrics/CyclomaticComplexity:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#### Fixes
1515

16+
* [#988](https://github.com/intridea/grape/pull/988): Fixed duplicate identical endpoints - [@u2](https://github.com/u2).
1617
* [#936](https://github.com/intridea/grape/pull/936): Fixed default params processing for optional groups - [@dm1try](https://github.com/dm1try).
1718
* [#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).
1819

lib/grape/dsl/routing.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ def route(methods, paths = ['/'], route_options = {}, &block)
120120
}).deep_merge(route_setting(:description) || {}).deep_merge(route_options || {})
121121
}
122122

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

125126
route_end
126127
reset_validations!

lib/grape/endpoint.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ def endpoints
207207
end
208208
end
209209

210+
def equals?(e)
211+
(options == e.options) && (inheritable_setting.to_hash == e.inheritable_setting.to_hash)
212+
end
213+
210214
protected
211215

212216
def run(env)

spec/grape/dsl/routing_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,17 @@ class Dummy
8787
.to change{ subject.endpoints.count }.from(0).to(1)
8888
end
8989

90+
it 'does not duplicate identical endpoints' do
91+
subject.route(:any)
92+
expect { subject.route(:any) }
93+
.to_not change(subject.endpoints, :count)
94+
end
95+
9096
it 'generates correct endpoint options' do
9197
allow(subject).to receive(:route_setting).with(:description).and_return(fiz: 'baz')
9298
allow(Grape::DSL::Configuration).to receive(:stacked_hash_to_hash).and_return(nuz: 'naz')
9399

94100
expect(Grape::Endpoint).to receive(:new) do |inheritable_setting, endpoint_options|
95-
puts endpoint_options
96101
expect(endpoint_options[:method]).to eq :get
97102
expect(endpoint_options[:path]).to eq '/foo'
98103
expect(endpoint_options[:for]).to eq subject

0 commit comments

Comments
 (0)