Skip to content

Commit 6629807

Browse files
authored
Allow empty paths when .catchalling (#146)
* Allow empty paths when `.catchall`ing * Add check * Add bug tracker to test * Update test name
1 parent 9f601ed commit 6629807

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

Sources/RoutingKit/TrieRouter/TrieRouter.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ public final class TrieRouter<Output: Sendable>: Router, Sendable, CustomStringC
9595
} else if let (catchall, subpaths) = currentCatchall {
9696
parameters.setCatchall(matched: subpaths)
9797
return catchall.output
98+
} else if path.isEmpty, let catchall = currentNode.catchall {
99+
parameters.setCatchall(matched: [])
100+
return catchall.output
98101
} else {
99102
return nil
100103
}

Tests/RoutingKitTests/RouterTests.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,4 +382,14 @@ struct RouterTests {
382382
p = Parameters()
383383
#expect(r.route(path: ["y", "file.md"], parameters: &p) == 1)
384384
}
385+
386+
@Test("Catchall catches empty path", .bug("https://github.com/vapor/routing-kit/issues/147"))
387+
func emptyCatchall() throws {
388+
var b = TrieRouterBuilder<Int>()
389+
b.register(1, at: [.catchall])
390+
let r = b.build()
391+
392+
var p = Parameters()
393+
#expect(r.route(path: [], parameters: &p) == 1)
394+
}
385395
}

0 commit comments

Comments
 (0)