Skip to content

Commit 287087c

Browse files
committed
Fix prefix-suffix conflict due to insertion order of trailing slashes
When inserting route parameters with static suffixes, `matchit` checked for conflicts between newly inserted suffixes and existing ones. It had an explicit exception to allow a new suffix that only differed by an extra trailing slash (e.g., `o/` vs existing `o`). However, this check only evaluated one direction: if the *new* suffix was longer than the *existing* suffix. If the routes were inserted in the reverse order (e.g., `o/` inserted first, then `o` inserted second), the router incorrectly flagged them as a prefix-suffix conflict. This commit makes the trailing slash check bidirectional by adding an `else` branch to verify if the *existing* suffix is the one with the extra trailing slash, resolving the insertion order dependency. Signed-off-by: Changyuan Lyu <changyuanl@google.com>
1 parent 87804fe commit 287087c

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

src/tree.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ impl<T> Node<T> {
223223
if *common == *child.prefix && remaining == *b"/" {
224224
extra_trailing_slash = true;
225225
}
226+
} else {
227+
let (common, remaining) = child.prefix.split_at(suffix.len());
228+
if *common == **suffix && remaining == *b"/" {
229+
extra_trailing_slash = true;
230+
}
226231
}
227232
}
228233

tests/insert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ fn prefix_suffix_conflict() {
260260
("/x19/f{a}o", Ok(())),
261261
("/x19/f{a}o/{*path}", Ok(())),
262262
("/x20/f{a}o/{*path}", Ok(())),
263-
("/x20/f{a}o", Err(conflict("/x20/f{a}o/{*path}"))),
263+
("/x20/f{a}o", Ok(())),
264264
])
265265
.run()
266266
}

0 commit comments

Comments
 (0)