Skip to content

Commit 69435b0

Browse files
authored
Fixes ISSUE-2175 - Options call failing if matching all routes (#2176)
1 parent 7741f02 commit 69435b0

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
#### Fixes
88

9+
* [#2176](https://github.com/ruby-grape/grape/pull/2176): Fixes issue-2175 - options call failing if matching all routes - [@myxoh](https://github.com/myxoh).
910
* Your contribution here.
10-
1111
### 1.5.3 (2021/03/07)
1212

1313
#### Fixes

lib/grape/api/instance.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ def add_head_not_allowed_methods_and_options_methods
200200
without_root_prefix do
201201
without_versioning do
202202
versioned_route_configs.each do |config|
203+
next if config[:options][:matching_wildchar]
204+
203205
allowed_methods = config[:methods].dup
204206

205207
unless self.class.namespace_inheritable(:do_not_route_head)
@@ -228,7 +230,7 @@ def collect_route_config_per_pattern
228230
last_route = routes.last # Most of the configuration is taken from the last endpoint
229231
matching_wildchar = routes.any? { |route| route.request_method == '*' }
230232
{
231-
options: {},
233+
options: { matching_wildchar: matching_wildchar },
232234
pattern: last_route.pattern,
233235
requirements: last_route.requirements,
234236
path: last_route.origin,
@@ -248,7 +250,6 @@ def generate_not_allowed_method(pattern, allowed_methods: [], **attributes)
248250
Grape::Http::Headers::SUPPORTED_METHODS_WITHOUT_OPTIONS
249251
end
250252
not_allowed_methods = supported_methods - allowed_methods
251-
return if not_allowed_methods.empty?
252253
@router.associate_routes(pattern, not_allowed_methods: not_allowed_methods, **attributes)
253254
end
254255

spec/grape/api_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,47 @@ class DummyFormatClass
749749
end
750750
end
751751

752+
describe 'when a resource routes by POST, GET, PATCH, PUT, and DELETE' do
753+
before do
754+
subject.namespace :example do
755+
get do
756+
'example'
757+
end
758+
759+
patch do
760+
'example'
761+
end
762+
763+
post do
764+
'example'
765+
end
766+
767+
delete do
768+
'example'
769+
end
770+
771+
put do
772+
'example'
773+
end
774+
end
775+
options '/example'
776+
end
777+
778+
describe 'it adds an OPTIONS route for namespaced endpoints that' do
779+
it 'returns a 204' do
780+
expect(last_response.status).to eql 204
781+
end
782+
783+
it 'has an empty body' do
784+
expect(last_response.body).to be_blank
785+
end
786+
787+
it 'has an Allow header' do
788+
expect(last_response.headers['Allow']).to eql 'OPTIONS, GET, PATCH, POST, DELETE, PUT, HEAD'
789+
end
790+
end
791+
end
792+
752793
describe 'adds an OPTIONS route for namespaced endpoints that' do
753794
before do
754795
subject.before { header 'X-Custom-Header', 'foo' }

0 commit comments

Comments
 (0)