lua-resty-radixtree icon indicating copy to clipboard operation
lua-resty-radixtree copied to clipboard

fix: sort_route uses vars count as tiebreaker to match max vars

Open janiussyafiq opened this issue 8 months ago β€’ 1 comments

Problem

When two routes share the same priority and uri, sort_route tiebreaks by path length. For identical paths (e.g. /*), this means insertion order wins β€” not specificity.

The result: a route with 1 vars condition is matched over a route with 2 vars conditions, even when the request satisfies both.

Ref: apache/apisix#9431
Fixes #157 Fixes apache/apisix#9431

Root Cause

sort_route in radixtree.lua:
https://github.com/api7/lua-resty-radixtree/blob/8166eea3e1128ab83e741aa665685648c234cac8/lib/resty/radixtree.lua#L206-L211

When both priority and path_org length are equal, the comparison is a no-op and insert_tab_in_order preserves insertion order.

Fix

Add vars condition count as a third tiebreaker β€” more conditions means more specific, so it should be tried first. Since route_opts.vars is a compiled expression object by sort time, the count is stored separately as vars_len during route construction. If the more-specific route's vars don't match at runtime, the matcher naturally falls through to the next candidate.

Summary by CodeRabbit

  • Bug Fixes

    • Route matching now consistently selects the more specific route when multiple routes share the same path and priority but differ in required parameters.
  • Tests

    • Added a test verifying correct selection of the more specific route when two routes match the same path but have different parameter requirements.

janiussyafiq avatar Apr 03 '26 07:04 janiussyafiq

No actionable comments were generated in the recent review. πŸŽ‰

ℹ️ Recent review info
βš™οΈ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 00ee84c3-9e07-4113-bb91-69c4956efa3f

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between 0ee4d2f81a6e2d1e0a47101856cf2f173df51292 and 67e3d5d64eea031ced80aec89a42089fb73eb451.

πŸ“’ Files selected for processing (1)
  • lib/resty/radixtree.lua
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/resty/radixtree.lua

πŸ“ Walkthrough

Walkthrough

Updated route sorting in the radix tree: when priority and path length tie, routes are now ordered by descending vars condition count (vars_len) so routes with more vars conditions are tried first. A new test verifies the behavior.

Changes

Cohort / File(s) Summary
Route Sorting Logic
lib/resty/radixtree.lua
Enhanced sort_route() to use vars_len as a tiebreaker when priority and #path_org are equal; common_route_data() now sets route_opts.vars_len = #route.vars`` when converting route.vars.
Route Matching Test
t/vars.t
Added TEST 20: registers two routes with identical path and priority but different vars counts and asserts the route with more vars conditions is matched.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

πŸš₯ Pre-merge checks | βœ… 5 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
βœ… Passed checks (5 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The pull request title accurately summarizes the main change: adding vars count as a tiebreaker in sort_route to prioritize routes with more specific conditions.
Linked Issues check βœ… Passed The code changes implement the required fix: adding vars_len as a tiebreaker in sort_route [#157, #9431] and storing vars count during route compilation to enable the comparison.
Out of Scope Changes check βœ… Passed All changes are directly scoped to the linked issues: modifications to sort_route logic and route compilation in radixtree.lua, plus a test case validating the fix.
E2e Test Quality Review βœ… Passed PR implements a proper E2E test validating that routes with more specific vars conditions are matched before less-specific ones when sharing identical URI and priority.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • [ ] Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Apr 03 '26 07:04 coderabbitai[bot]