Skip to content

Commit f4e382d

Browse files
authored
Merge pull request #388 from ahx/handle-path-dynamic-segment-without-slash
Handle dynamic path segments without slashes
2 parents c96718b + 77212bb commit f4e382d

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

lib/openapi_first/router.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,15 @@ def find_path_item(request_path)
9797
found = @static[request_path]
9898
return [found, {}] if found
9999

100-
@dynamic.find do |_path, path_item|
100+
matches = @dynamic.filter_map do |_path, path_item|
101101
params = path_item[:template].match(request_path)
102-
return [path_item, params] if params
102+
next unless params
103+
104+
[path_item, params]
103105
end
106+
return matches.first if matches.length == 1
107+
108+
matches&.min_by { |match| match[1].values.sum(&:length) }
104109
end
105110
end
106111
end

spec/router_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
[
99
double(path: '/{id}', request_method: 'get'),
1010
double(path: '/{id}', request_method: 'patch'),
11-
double(path: '/a', request_method: 'get')
11+
double(path: '/a', request_method: 'get'),
12+
double(path: '/a{format}', request_method: 'get')
1213
]
1314
end
1415

@@ -30,6 +31,12 @@
3031
expect(router.match('GET', '/c/d').error).to have_attributes(type: :not_found)
3132
end
3233

34+
it 'can match a path fragment with a variable' do
35+
match = router.match('GET', '/a.json')
36+
expect(match.request_definition.path).to eq('/a{format}')
37+
expect(match.request_definition).to be(requests[3])
38+
end
39+
3340
it 'returns an incomplete match for unknown request method' do
3441
expect(router.match('DELETE', '/b').error).to have_attributes(type: :method_not_allowed)
3542
end

0 commit comments

Comments
 (0)