fix(PathPatternResolver): remove leading slash from resolved pattern if first component is empty (#2339)
Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
@@ -271,9 +271,14 @@ public class PathPatternResolver {
|
||||
private String validateFinalPath(String path) {
|
||||
String[] components = SLASH_PATTERN.split(path);
|
||||
StringBuilder result = new StringBuilder(512);
|
||||
boolean first = true;
|
||||
|
||||
for (int i = 0; i < components.length; i++) {
|
||||
String component = components[i];
|
||||
if (component == null || component.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean isLastComponent = (i == components.length - 1);
|
||||
|
||||
if (isLastComponent && component.contains(".")) {
|
||||
@@ -287,8 +292,9 @@ public class PathPatternResolver {
|
||||
}
|
||||
}
|
||||
|
||||
if (i > 0) result.append("/");
|
||||
if (!first) result.append("/");
|
||||
result.append(component);
|
||||
first = false;
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
+16
@@ -889,4 +889,20 @@ class PathPatternResolverTest {
|
||||
assertTrue(result.getBytes(StandardCharsets.UTF_8).length <= MAX_FILENAME_BYTES,
|
||||
"Result bytes should be <= " + MAX_FILENAME_BYTES);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should remove leading slash from resolved pattern if first component is empty")
|
||||
void testResolvePattern_removesLeadingSlash_whenFirstComponentIsEmpty() {
|
||||
BookMetadata metadata = BookMetadata.builder()
|
||||
.title("Book Title")
|
||||
.authors(Set.of()) // Empty authors
|
||||
.build();
|
||||
|
||||
// Pattern implies a subdirectory, but authors is missing
|
||||
// This resolves to "/Book Title.pdf" currently
|
||||
String result = PathPatternResolver.resolvePattern(metadata, "{authors}/{title}", "original.pdf");
|
||||
|
||||
// This assertion ensures the path is relative (does not start with /)
|
||||
assertFalse(result.startsWith("/"), "Result should not start with slash: " + result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user