Skip to content

Commit

Permalink
fix: use hierarchical route as key for collecting client menus (#2462)
Browse files Browse the repository at this point in the history
* fix: use hierarchical route as key for collecting client menus

Fixes #2460

* add tests

* add tests
  • Loading branch information
taefi authored May 24, 2024
1 parent cd46d72 commit f5cdf5f
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.vaadin.hilla.route.records.ClientViewConfig;
import org.jsoup.nodes.DataNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -138,21 +139,28 @@ protected Map<String, AvailableViewInfo> collectClientViews(
deploymentConfiguration);
}

return clientRouteRegistry.getAllRoutes().values().stream()
.filter(config -> config.getRouteParameters() == null
|| config.getRouteParameters().isEmpty()
|| config.getRouteParameters().values().stream()
.noneMatch(
param -> param == RouteParamType.REQUIRED))
.filter(config -> routeUtil.isRouteAllowed(isUserInRole,
isUserAuthenticated, config))
.map(config -> new AvailableViewInfo(config.getTitle(),
config.getRolesAllowed(), config.isLoginRequired(),
config.getRoute(), config.isLazy(),
config.isAutoRegistered(), config.menu(),
config.getRouteParameters()))
.collect(Collectors.toMap(AvailableViewInfo::route,
Function.identity()));
return clientRouteRegistry.getAllRoutes().entrySet().stream()
.filter(viewMapping -> !hasRequiredParameter(
viewMapping.getValue().getRouteParameters()))
.filter(viewMapping -> routeUtil.isRouteAllowed(isUserInRole,
isUserAuthenticated, viewMapping.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey,
viewMapping -> toAvailableViewInfo(
viewMapping.getValue())));
}

private boolean hasRequiredParameter(
Map<String, RouteParamType> routeParameters) {
return routeParameters != null && !routeParameters.isEmpty()
&& routeParameters.values().stream().anyMatch(
paramType -> paramType == RouteParamType.REQUIRED);
}

private AvailableViewInfo toAvailableViewInfo(ClientViewConfig config) {
return new AvailableViewInfo(config.getTitle(),
config.getRolesAllowed(), config.isLoginRequired(),
config.getRoute(), config.isLazy(), config.isAutoRegistered(),
config.menu(), config.getRouteParameters());
}

protected Map<String, AvailableViewInfo> collectServerViews(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,30 @@ private Map<String, ClientViewConfig> prepareClientRoutes() {
.setRouteParameters(Map.of(":userId", RouteParamType.REQUIRED));
routes.put("/user/:userId", userProfileConfig);

ClientViewConfig rootIndexConfig = new ClientViewConfig();
rootIndexConfig.setTitle("Index");
rootIndexConfig.setRolesAllowed(null);
rootIndexConfig.setLoginRequired(false);
rootIndexConfig.setRoute("");
rootIndexConfig.setLazy(false);
rootIndexConfig.setAutoRegistered(false);
rootIndexConfig.setMenu(null);
rootIndexConfig.setChildren(Collections.emptyList());
rootIndexConfig.setRouteParameters(Collections.emptyMap());
routes.put("/", rootIndexConfig);

ClientViewConfig ordersIndexConfig = new ClientViewConfig();
ordersIndexConfig.setTitle("Orders");
ordersIndexConfig.setRolesAllowed(null);
ordersIndexConfig.setLoginRequired(false);
ordersIndexConfig.setRoute("");
ordersIndexConfig.setLazy(false);
ordersIndexConfig.setAutoRegistered(false);
ordersIndexConfig.setMenu(null);
ordersIndexConfig.setChildren(Collections.emptyList());
ordersIndexConfig.setRouteParameters(Collections.emptyMap());
routes.put("/orders", ordersIndexConfig);

return routes;
}

Expand Down Expand Up @@ -360,7 +384,7 @@ public void when_productionMode_should_collectClientViews() {
boolean isAuthenticated = true;
var views = requestListener.collectClientViews(isUserInRole,
isAuthenticated);
MatcherAssert.assertThat(views, Matchers.aMapWithSize(2));
MatcherAssert.assertThat(views, Matchers.aMapWithSize(4));
}

@Test
Expand All @@ -371,7 +395,7 @@ public void when_developmentMode_should_collectClientViews()
Predicate<? super String> isUserInRole = role -> true;
var views = requestListener.collectClientViews(isUserInRole,
isUserAuthenticated);
MatcherAssert.assertThat(views, Matchers.aMapWithSize(2));
MatcherAssert.assertThat(views, Matchers.aMapWithSize(4));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,25 @@
"params": {
":___commentId?": "opt"
}
},
"/": {
"title": "Index",
"rolesAllowed": null,
"requiresLogin": false,
"route": "",
"lazy": false,
"register": false,
"menu": null,
"params": {}
},
"/orders": {
"title": "Orders",
"rolesAllowed": null,
"requiresLogin": false,
"route": "",
"lazy": false,
"register": false,
"menu": null,
"params": {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,25 @@
"params": {
":___commentId?": "opt"
}
},
"/": {
"title": "Index",
"rolesAllowed": null,
"requiresLogin": false,
"route": "",
"lazy": false,
"register": false,
"menu": null,
"params": {}
},
"/orders": {
"title": "Orders",
"rolesAllowed": null,
"requiresLogin": false,
"route": "",
"lazy": false,
"register": false,
"menu": null,
"params": {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,25 @@
"params": {
":___commentId?": "opt"
}
},
"/": {
"title": "Index",
"rolesAllowed": null,
"requiresLogin": false,
"route": "",
"lazy": false,
"register": false,
"menu": null,
"params": {}
},
"/orders": {
"title": "Orders",
"rolesAllowed": null,
"requiresLogin": false,
"route": "",
"lazy": false,
"register": false,
"menu": null,
"params": {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,25 @@
"register": false,
"menu": null,
"params": {}
},
"/": {
"title": "Index",
"rolesAllowed": null,
"requiresLogin": false,
"route": "",
"lazy": false,
"register": false,
"menu": null,
"params": {}
},
"/orders": {
"title": "Orders",
"rolesAllowed": null,
"requiresLogin": false,
"route": "",
"lazy": false,
"register": false,
"menu": null,
"params": {}
}
}

0 comments on commit f5cdf5f

Please sign in to comment.