lua-radix-router icon indicating copy to clipboard operation
lua-radix-router copied to clipboard

`trailing_slash_match=true` causes a bug

Open re-gor opened this issue 8 months ago • 0 comments

Hello and thank you for your great work on this project!

I've been trying to use radix-router in my project and I think I may have found a bug. I want to share a test case that reproduces the issue. I cloned the repo and add a couple of tests

-- describe chain
        it("subpath", function()
          local router = Router.new({
            {
              paths = { "/aa/{var1}" },
              handler = "1"
            },
            {
              paths = { "/aa/{var1}/bb" },
              handler = "2",
            }
          })

          assert.equal("1", router:match("/aa/var1"))
          assert.equal("2", router:match("/aa/var1/bb"))
        end)

        it("subpath2", function()
          local router = Router.new({
              {paths = {"/aa/{id}"}, handler="1"},
              {paths = {"/aa/{id}/bb"}, handler="2"},
          }, {
              trailing_slash_match = true
          })

          assert.equal("1", router:match("/aa/var1"))
          assert.equal("2", router:match("/aa/var1/bb"))
        end)

The result is that subpath2 test is broken. The first assertion is failed. I would have expected that the router should not match /aa/var1 against the second route.

Just as a guess: I looked into the source code a bit, and it seems that the issue might be in iterator.lua around line 112. Specifically, it looks like there should be a line setting not_found = false, because at that point we actually found a corresponding node. Also, the logic at line 118 might make an incorrect assumption about the state of the trie (for example, there is a / node, but it leads to the /bb path).

ps. I also noticed that there are some tests failed on main branch

[  ERROR   ] spec/router_spec.lua:512: Router regex sanity

pss. Thank you for your attention, and please let me know if I can provide any more information or help with investigating this further!

re-gor avatar May 06 '25 19:05 re-gor