Skip to content

Commit

Permalink
fix(fs-router) check for routes with / first, then without (#2258)
Browse files Browse the repository at this point in the history
  • Loading branch information
cromoteca authored Mar 27, 2024
1 parent a4d3dfb commit 74399f7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vaadin.flow.function.DeploymentConfiguration;
import com.vaadin.hilla.route.records.ClientViewConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
Expand All @@ -32,6 +33,8 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.Objects;
import java.util.stream.Stream;

/**
* Keeps track of registered client side routes.
Expand Down Expand Up @@ -98,12 +101,23 @@ public void removeRoute(String route) {
public ClientViewConfig getRouteByPath(String path) {
final Set<String> routes = registeredRoutes.keySet();
final AntPathMatcher pathMatcher = new AntPathMatcher();
for (String route : routes) {
if (pathMatcher.match(route, path)) {
return registeredRoutes.get(route);
}
}
return null;
return Stream.of(addTrailingSlash(path), removeTrailingSlash(path))
.map(p -> {
for (String route : routes) {
if (pathMatcher.match(route, p)) {
return registeredRoutes.get(route);
}
}
return null;
}).filter(Objects::nonNull).findFirst().orElse(null);
}

private String addTrailingSlash(String path) {
return path.endsWith("/") ? path : path + '/';
}

private String removeTrailingSlash(String path) {
return path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,31 @@ public void test_login_required_on_page() {
boolean actual = endpointUtil.isRouteAllowed(request);
Assert.assertFalse(actual);
}

/**
* Verifies that the root route is allowed when login is not required,
* despite the mismatch between "/" and "".
*/
@Test
public void test_login_not_required_on_root() {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setRequestURI("/context/");
request.setContextPath("/context");
request.setUserPrincipal(null);

ClientViewConfig config = new ClientViewConfig();
config.setTitle("Root");
config.setRolesAllowed(null);
config.setLoginRequired(false);
config.setRoute("");
config.setLazy(false);
config.setAutoRegistered(false);
config.setMenu(null);
config.setChildren(null);
config.setRouteParameters(null);
registry.addRoute("", config);

boolean actual = endpointUtil.isRouteAllowed(request);
Assert.assertTrue(actual);
}
}

0 comments on commit 74399f7

Please sign in to comment.