Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Check main layout presence separately (#2831)(CP: main) #2836

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.vaadin.flow.server.auth.MenuAccessControl;
import com.vaadin.flow.server.auth.NavigationAccessControl;
import com.vaadin.flow.server.auth.ViewAccessChecker;
import com.vaadin.flow.server.communication.IndexHtmlResponse;
import com.vaadin.flow.server.menu.AvailableViewInfo;
import com.vaadin.flow.internal.menu.MenuRegistry;
import com.vaadin.flow.server.menu.RouteParamType;
Expand Down Expand Up @@ -67,13 +66,12 @@ public String createFileRoutesJson(VaadinRequest request)
throws JsonProcessingException {
final Map<String, AvailableViewInfo> availableViews = new HashMap<>(
collectClientViews(request));
final boolean hasMainMenuRoute = hasMainMenu(
MenuRegistry.collectClientMenuItems(true,
deploymentConfiguration, request));
final boolean hasAutoLayout = MenuRegistry.hasHillaMainLayout(
request.getService().getDeploymentConfiguration());
if (exposeServerRoutesToClient) {
LOGGER.debug(
"Exposing server-side views to the client based on user configuration");
availableViews.putAll(collectServerViews(hasMainMenuRoute));
availableViews.putAll(collectServerViews(hasAutoLayout));
}

return mapper.writeValueAsString(availableViews);
Expand Down Expand Up @@ -159,24 +157,6 @@ protected Map<String, AvailableViewInfo> collectServerViews(
Function.identity()));
}

private boolean hasMainMenu(Map<String, AvailableViewInfo> availableViews) {
Map<String, AvailableViewInfo> clientItems = new HashMap<>(
availableViews);

Set<String> clientEntries = new HashSet<>(clientItems.keySet());
for (String key : clientEntries) {
if (!clientItems.containsKey(key)) {
continue;
}
AvailableViewInfo viewInfo = clientItems.get(key);
if (viewInfo.children() != null) {
RouteUtil.removeChildren(clientItems, viewInfo, key);
}
}
return !clientItems.isEmpty() && clientItems.size() == 1
&& clientItems.values().iterator().next().route().equals("");
}

/**
* Gets menu link with omitted route parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class RouteUnifyingIndexHtmlRequestListenerTest {

@Before
public void setUp() throws IOException {
MenuRegistry.clearFileRoutesCache();
vaadinService = Mockito.mock(VaadinService.class);

VaadinContext vaadinContext = Mockito.mock(VaadinContext.class);
Expand All @@ -94,6 +95,10 @@ public void setUp() throws IOException {
Mockito.when(vaadinRequest.getUserPrincipal())
.thenReturn(userPrincipal);

Mockito.when(vaadinRequest.getService()).thenReturn(vaadinService);
Mockito.when(vaadinService.getDeploymentConfiguration())
.thenReturn(deploymentConfiguration);

final Document document = Mockito.mock(Document.class);
final Element element = new Element("head");
Mockito.when(document.head()).thenReturn(element);
Expand Down Expand Up @@ -561,9 +566,22 @@ public void when_exposeServerRoutesToClient_noLayout_serverSideRoutesAreNotInRes
@Test
public void when_exposeServerRoutesToClient_layoutExists_serverSideRoutesAreInResponse()
throws IOException {
assertServerRoutesExposedToClientWhenLayoutExists(
"clientRoutesWithLayout.json", "server-and-client-views.json");
}

@Test
public void when_exposeServerRoutesToClient_layoutExists_routeWithEmptyPath_serverSideRoutesAreInResponse()
throws IOException {
assertServerRoutesExposedToClientWhenLayoutExists(
"clientRoutesWithLayoutAndIndexView.json",
"server-and-client-views-layout-and-index-route.json");
}

private void assertServerRoutesExposedToClientWhenLayoutExists(
String testJsonFile, String expectedJsonFile) throws IOException {
// Use routes with layout
copyClientRoutes("clientRoutesWithLayout.json", productionRouteFile);
copyClientRoutes(testJsonFile, productionRouteFile);

try (MockedStatic<VaadinService> mocked = Mockito
.mockStatic(VaadinService.class);
Expand Down Expand Up @@ -611,8 +629,8 @@ public void when_exposeServerRoutesToClient_layoutExists_serverSideRoutesAreInRe
final var mapper = new ObjectMapper();

var actual = mapper.readTree(views);
var expected = mapper.readTree(getClass()
.getResource("/META-INF/VAADIN/server-and-client-views.json"));
var expected = mapper.readTree(
getClass().getResource("/META-INF/VAADIN/" + expectedJsonFile));

MatcherAssert.assertThat("Different amount of items", actual.size(),
Matchers.is(expected.size()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"/foo": {
"params": {},
"title": "RouteTarget",
"rolesAllowed": null,
"loginRequired": false,
"route": "/foo",
"lazy": false,
"register": false,
"menu": null,
"flowLayout":false
},
"/bar": {
"params": {},
"title": "Component",
"rolesAllowed": null,
"loginRequired": false,
"route": "/bar",
"lazy": false,
"register": false,
"menu": null,
"flowLayout":false
},
"": {
"title": "Home",
"rolesAllowed": null,
"loginRequired": false,
"route": "",
"lazy": false,
"register": false,
"menu": {
"title": "Home",
"order": null,
"exclude": false,
"icon": null,
"menuClass": null
},
"flowLayout":false,
"params": {}
},
"/comments": {
"params": {
":___commentId?": "opt"
},
"title": "RouteTarget",
"rolesAllowed": null,
"loginRequired": false,
"route": "/comments",
"lazy": false,
"register": false,
"menu": null,
"flowLayout":false
},
"/wildcard": {
"params": {
":___wildcard*": "*"
},
"title": "RouteTarget",
"rolesAllowed": null,
"loginRequired": false,
"route": "/wildcard",
"lazy": false,
"register": false,
"menu": null,
"flowLayout":false
},
"/profile": {
"params": {},
"title": "Profile",
"rolesAllowed": [
"ROLE_USER",
"ROLE_ADMIN"
],
"loginRequired": true,
"route": "/profile",
"lazy": false,
"register": false,
"menu": {
"title": "Profile",
"order": null,
"exclude": false,
"icon": null,
"menuClass": null
},
"flowLayout":false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[
{
"route": "",
"params": {},
"title": "Layout",
"children": [
{
"route": "",
"params": {},
"title": "Home"
},
{
"route": "/profile",
"params": {},
"title": "Profile",
"loginRequired": true,
"rolesAllowed": [
"ROLE_USER",
"ROLE_ADMIN"
]
},
{
"route": "/user/:userId",
"loginRequired": true,
"rolesAllowed": [
"ROLE_ADMIN"
],
"params": {
":userId": "req"
},
"title": "User Profile"
}
]
}
]