Skip to content

Commit

Permalink
fix: navigatin to client view from server (#20151) (#20174)
Browse files Browse the repository at this point in the history
* fix: navigatin to client view from server

Navigation to the client view
using server navigate should
work normally and not try to
add layout for non layout
client view.

* remove class used for hilla compatibility

Co-authored-by: caalador <[email protected]>
  • Loading branch information
vaadin-bot and caalador authored Oct 8, 2024
1 parent 2ed1691 commit 99c0b5e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.vaadin.flow.router.RouteResolver;
import com.vaadin.flow.server.RouteRegistry;
import com.vaadin.flow.internal.menu.MenuRegistry;
import com.vaadin.flow.server.menu.AvailableViewInfo;

/**
* Default implementation of the {@link RouteResolver} interface.
Expand All @@ -47,17 +48,26 @@ public NavigationState resolve(ResolveRequest request) {

if (!navigationResult.hasTarget()) {
if (MenuRegistry.hasClientRoute(path)) {
Class<? extends Component> layout = (Class<? extends Component>) registry
.getLayout(path);
if (layout == null) {
throw new NotFoundException(
"No layout for client path '%s'".formatted(path));
AvailableViewInfo viewInfo = MenuRegistry.getClientRoutes(false)
.get(path.isEmpty() ? path
: path.startsWith("/") ? path : "/" + path);
if (viewInfo != null && viewInfo.flowLayout()) {

Class<? extends Component> layout = (Class<? extends Component>) registry
.getLayout(path);
if (layout == null) {
throw new NotFoundException(
"No layout for client path '%s'"
.formatted(path));
}
RouteTarget target = new RouteTarget(layout,
Collections.emptyList());
navigationResult = new NavigationRouteTarget(
navigationResult.getPath(), target,
Collections.emptyMap());
} else {
return null;
}
RouteTarget target = new RouteTarget(layout,
Collections.emptyList());
navigationResult = new NavigationRouteTarget(
navigationResult.getPath(), target,
Collections.emptyMap());
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
*/
package com.vaadin.flow.router;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -35,6 +38,8 @@
import com.vaadin.flow.server.InvalidRouteConfigurationException;
import com.vaadin.flow.server.RouteRegistry;
import com.vaadin.flow.internal.menu.MenuRegistry;
import com.vaadin.flow.server.menu.AvailableViewInfo;
import com.vaadin.flow.server.menu.RouteParamType;

public class DefaultRouteResolverTest extends RoutingTestBase {

Expand Down Expand Up @@ -133,7 +138,10 @@ public void clientRouteRequest_getDefinedLayout() {
.mockStatic(MenuRegistry.class)) {
menuRegistry.when(() -> MenuRegistry.hasClientRoute(path))
.thenReturn(true);

menuRegistry.when(() -> MenuRegistry.getClientRoutes(false))
.thenReturn(Collections.singletonMap("/route",
new AvailableViewInfo("", null, false, "/route",
false, false, null, null, null, true)));
NavigationState greeting = resolveNavigationState(path);
Assert.assertEquals(
"Layout should be returned for a non server route when matching @Layout exists",
Expand All @@ -152,7 +160,10 @@ public void clientRouteRequest_noLayoutForPath_Throws() {
.mockStatic(MenuRegistry.class)) {
menuRegistry.when(() -> MenuRegistry.hasClientRoute(path))
.thenReturn(true);

menuRegistry.when(() -> MenuRegistry.getClientRoutes(false))
.thenReturn(Collections.singletonMap("/route",
new AvailableViewInfo("", null, false, "/route",
false, false, null, null, null, true)));
NavigationState greeting = resolveNavigationState(path);
}
}
Expand Down

0 comments on commit 99c0b5e

Please sign in to comment.