Skip to content

Commit b67179c

Browse files
authored
Merge pull request #402 from ahx/fix-match-trailing-slash
Ignore trailing slashes when routing requests
2 parents e60e199 + ccd1b9c commit b67179c

4 files changed

Lines changed: 43 additions & 3 deletions

File tree

CHANGELOG.md

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

33
## Unreleased
44

5+
- Breaking: Trailing slashes are no longer ignored in dynamic paths. See [#403](https://github.com/ahx/openapi_first/issues/403).
56
- Remove superflous `Test::Coverage.current_run, .plans, .install, .uninstall`
67
- Update dependency `openapi_paramters` to >= 0.7.0, because that version fixes unpacking exploded deepObject paramters
78
- Add `OpenapiFirst::Test::Configuration#ignore_unknown_response_status=`. When this is set to true, it won't complain about undefined response statuses it sees during a test run. Use with caution.

lib/openapi_first/router/path_template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def build_pattern(template)
4141
part.start_with?('{') ? ALLOWED_PARAMETER_CHARACTERS : Regexp.escape(part)
4242
end
4343

44-
%r{^#{parts.join}/?$}
44+
/^#{parts.join}$/
4545
end
4646
end
4747
end

spec/router/path_template_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
expect(described_class.new('/{a}/{b}').match('/1/2')).to eq({ 'a' => '1', 'b' => '2' })
2424
end
2525

26-
it 'ignores trailing slashes in paths' do
27-
expect(described_class.new('/{a}/{b}').match('/1/2/')).to eq({ 'a' => '1', 'b' => '2' })
26+
it 'respect trailing slashes in paths' do
27+
expect(described_class.new('/{a}/{b}').match('/1/2/')).to be_nil
2828
end
2929

3030
it 'returns params with kebab-case names' do

spec/router_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@
4141
expect(router.match('DELETE', '/b').error).to have_attributes(type: :method_not_allowed)
4242
end
4343

44+
it 'does not match a dynamic path with a trailing slash' do
45+
match = router.match('GET', '/42/')
46+
expect(match.request_definition).to be_nil
47+
end
48+
49+
it 'does not match a static path with a trailing slash' do
50+
match = router.match('GET', '/a/')
51+
expect(match.request_definition).to be_nil
52+
end
53+
4454
context 'with ambiguous paths' do
4555
let(:requests) do
4656
[
@@ -55,6 +65,35 @@
5565
end
5666
end
5767

68+
context 'with trailing slashes in path definitions' do
69+
let(:requests) do
70+
[
71+
double(path: '/{id}/', request_method: 'get'),
72+
double(path: '/a/', request_method: 'get')
73+
]
74+
end
75+
76+
it 'matches a dynamic path' do
77+
match = router.match('GET', '/42/')
78+
expect(match.request_definition.path).to eq('/{id}/')
79+
end
80+
81+
it 'does not match a dynamic path with missing trailing slash' do
82+
match = router.match('GET', '/42')
83+
expect(match.error.type).to eq(:not_found)
84+
end
85+
86+
it 'matches a static path' do
87+
match = router.match('GET', '/a/')
88+
expect(match.request_definition.path).to eq('/a/')
89+
end
90+
91+
it 'does not match a static path with missing trailing slash' do
92+
match = router.match('GET', '/a')
93+
expect(match.error.type).to eq(:not_found)
94+
end
95+
end
96+
5897
context 'with ambiguous paths reversed' do
5998
let(:requests) do
6099
[

0 commit comments

Comments
 (0)