Skip to content

Commit

Permalink
fix: always create AvailableViewInfo.menu (#20136)
Browse files Browse the repository at this point in the history
Create always MenuData in `AvailableViewInfo.menu` to avoid need for extra null checking in automatic menu in Hilla. This change affects only code that reads available client routes from Hilla generated json file.

Fixes: #20109
  • Loading branch information
tltv authored Oct 4, 2024
1 parent 430c40f commit aa83939
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,26 @@ private static void collectClientViews(String basePath,
: viewConfig.route().startsWith("/")
? basePath + viewConfig.route()
: basePath + '/' + viewConfig.route();
if (viewConfig.menu() == null) {
// create MenuData anyway to avoid need for null checking
viewConfig = copyAvailableViewInfo(viewConfig,
new MenuData(viewConfig.title(), null, false, null));
}
configurations.put(path, viewConfig);
if (viewConfig.children() != null) {
viewConfig.children().forEach(
child -> collectClientViews(path, child, configurations));
}
}

private static AvailableViewInfo copyAvailableViewInfo(
AvailableViewInfo source, MenuData newMenuData) {
return new AvailableViewInfo(source.title(), source.rolesAllowed(),
source.loginRequired(), source.route(), source.lazy(),
source.register(), newMenuData, source.children(),
source.routeParameters(), source.flowLayout());
}

public static final String FILE_ROUTES_JSON_NAME = "file-routes.json";
public static final String FILE_ROUTES_JSON_PROD_PATH = "META-INF/VAADIN/"
+ FILE_ROUTES_JSON_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ private void assertClientRoutes(Map<String, AvailableViewInfo> menuItems,
boolean authenticated, boolean hasRole, boolean excludeExpected) {
Assert.assertTrue("Client route '' missing", menuItems.containsKey(""));
Assert.assertEquals("Public", menuItems.get("").title());
Assert.assertNull("Public doesn't contain specific menu data",
Assert.assertNotNull("Public should contain default menu data",
menuItems.get("").menu());

if (authenticated) {
Expand All @@ -353,7 +353,7 @@ private void assertClientRoutes(Map<String, AvailableViewInfo> menuItems,
Assert.assertEquals("About", menuItems.get("/about").title());
Assert.assertTrue("Login should be required",
menuItems.get("/about").loginRequired());
Assert.assertNull("About doesn't contain specific menu data",
Assert.assertNotNull("About should contain default menu data",
menuItems.get("/about").menu());

if (hasRole) {
Expand All @@ -365,7 +365,7 @@ private void assertClientRoutes(Map<String, AvailableViewInfo> menuItems,
Assert.assertArrayEquals("Faulty roles fo hilla",
new String[] { "ROLE_USER" },
menuItems.get("/hilla").rolesAllowed());
Assert.assertNull("Hilla doesn't contain specific menu data",
Assert.assertNotNull("Hilla should contain default menu data",
menuItems.get("/hilla").menu());

Assert.assertTrue("Client child route 'hilla/sub' missing",
Expand Down

0 comments on commit aa83939

Please sign in to comment.