Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/openapi_first/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,15 @@ def find_path_item(request_path)
found = @static[request_path]
return [found, {}] if found

@dynamic.find do |_path, path_item|
matches = @dynamic.filter_map do |_path, path_item|
params = path_item[:template].match(request_path)
return [path_item, params] if params
next unless params

[path_item, params]
end
return matches.first if matches.length == 1

matches&.min_by { |match| match[1].values.sum(&:length) }
end
end
end
9 changes: 8 additions & 1 deletion spec/router_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
[
double(path: '/{id}', request_method: 'get'),
double(path: '/{id}', request_method: 'patch'),
double(path: '/a', request_method: 'get')
double(path: '/a', request_method: 'get'),
double(path: '/a{format}', request_method: 'get')
]
end

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

it 'can match a path fragment with a variable' do
match = router.match('GET', '/a.json')
expect(match.request_definition.path).to eq('/a{format}')
expect(match.request_definition).to be(requests[3])
end

it 'returns an incomplete match for unknown request method' do
expect(router.match('DELETE', '/b').error).to have_attributes(type: :method_not_allowed)
end
Expand Down