From 27400c9ad373550c0d2bc54ece85eb873c3de346 Mon Sep 17 00:00:00 2001 From: Adam Reichold Date: Fri, 19 Aug 2022 10:45:14 +0200 Subject: [PATCH] Check for the special case of the root facet as prefix of other facets. (#1448) --- src/schema/facet.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/schema/facet.rs b/src/schema/facet.rs index 3e826e384..f516995b0 100644 --- a/src/schema/facet.rs +++ b/src/schema/facet.rs @@ -156,7 +156,7 @@ impl Facet { let other_str = other.encoded_str(); self_str.len() < other_str.len() && other_str.starts_with(self_str) - && other_str.as_bytes()[self_str.len()] == FACET_SEP_BYTE + && (self_str.len() == 0 || other_str.as_bytes()[self_str.len()] == FACET_SEP_BYTE) } /// Extract path from the `Facet`. @@ -301,4 +301,18 @@ mod tests { Facet::from_text("INVALID") ); } + + #[test] + fn only_proper_prefixes() { + assert!(Facet::from("/foo").is_prefix_of(&Facet::from("/foo/bar"))); + + assert!(!Facet::from("/foo/bar").is_prefix_of(&Facet::from("/foo/bar"))); + } + + #[test] + fn root_is_a_prefix() { + assert!(Facet::from("/").is_prefix_of(&Facet::from("/foobar"))); + + assert!(!Facet::from("/").is_prefix_of(&Facet::from("/"))); + } }