Skip to content

Commit

Permalink
fix: resolve route parameters when rerouting and forwarding with quer…
Browse files Browse the repository at this point in the history
…y parameters (#20210) (CP: 23.5) (#20222)

Fixes #20205
  • Loading branch information
vaadin-bot authored and mshabarov committed Oct 16, 2024
1 parent fd7d674 commit 0f2bf75
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 35 deletions.
2 changes: 1 addition & 1 deletion flow-client/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@vaadin/flow-deps",
"name": "@vaadin/flow-client",
"description": "Flow client package",
"version": "0.0.1",
"main": "src/main/resources/META-INF/frontend/Flow",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@

public enum Country {

FINLAND("Finland"), SWEDEN("Sweden"), USA("USA"), RUSSIA(
"Russia"), NETHERLANDS("Netherlands"), SOUTH_AFRICA("South Africa");
FINLAND("Finland"),
SWEDEN("Sweden"),
USA("USA"),
RUSSIA("Russia"),
NETHERLANDS("Netherlands"),
SOUTH_AFRICA("South Africa");

private final String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,21 @@ private static Optional<ImportanceType> fromAttributeValue(
* Sandbox types.
*/
public enum SandboxType {
RESTRICT_ALL(""), ALLOW_FORMS("allow-forms"), ALLOW_MODALS(
"allow-modals"), ALLOW_ORIENTATION_LOCK(
"allow-orientation-lock"), ALLOW_POINTER_LOCK(
"allow-pointer-lock"), ALLOW_POPUPS(
"allow-popups"), ALLOW_POPUPS_TO_ESCAPE_SANDBOX(
"allow-popups-to-escape-sandbox"), ALLOW_PRESENTATION(
"allow-presentation"), ALLOW_SAME_ORIGIN(
"allow-same-origin"), ALLOW_SCRIPTS(
"allow-scripts"), ALLOW_STORAGE_ACCESS_BY_USER_ACTIVATION(
"allow-storage-access-by-user-activation"), ALLOW_TOP_NAVIGATION(
"allow-top-navigation"), ALLOW_TOP_NAVIGATION_BY_USER_ACTIVATION(
"allow-top-navigation-by-user-activation");
RESTRICT_ALL(""),
ALLOW_FORMS("allow-forms"),
ALLOW_MODALS("allow-modals"),
ALLOW_ORIENTATION_LOCK("allow-orientation-lock"),
ALLOW_POINTER_LOCK("allow-pointer-lock"),
ALLOW_POPUPS("allow-popups"),
ALLOW_POPUPS_TO_ESCAPE_SANDBOX("allow-popups-to-escape-sandbox"),
ALLOW_PRESENTATION("allow-presentation"),
ALLOW_SAME_ORIGIN("allow-same-origin"),
ALLOW_SCRIPTS("allow-scripts"),
ALLOW_STORAGE_ACCESS_BY_USER_ACTIVATION(
"allow-storage-access-by-user-activation"),
ALLOW_TOP_NAVIGATION("allow-top-navigation"),
ALLOW_TOP_NAVIGATION_BY_USER_ACTIVATION(
"allow-top-navigation-by-user-activation");

private final String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public final class StringUtil {
* Comment parser state enumeration.
*/
private enum State {
NORMAL, IN_LINE_COMMENT, IN_BLOCK_COMMENT, IN_STRING, IN_STRING_APOSTROPHE
NORMAL,
IN_LINE_COMMENT,
IN_BLOCK_COMMENT,
IN_STRING,
IN_STRING_APOSTROPHE
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,11 @@ public <T> void forwardTo(String location, List<T> locationParams) {
*/
public void forwardTo(String locationString,
QueryParameters queryParameters) {
final Optional<Class<? extends Component>> target = getSource()
.getRegistry().getNavigationTarget(locationString);
final Optional<NavigationState> navigationState = getSource()
.resolveNavigationTarget(new Location(locationString));
this.redirectQueryParameters = queryParameters;
if (target.isPresent()) {
forwardTo(getNavigationState(locationString, List.of()));
if (navigationState.isPresent()) {
forwardTo(navigationState.get());
} else {
// Inform that forward target location is not known.
unknownForward = PathUtil.trimPath(locationString);
Expand Down Expand Up @@ -878,12 +878,11 @@ public <T> void rerouteTo(String route, List<T> routeParams) {
* query parameters for the target
*/
public void rerouteTo(String route, QueryParameters queryParameters) {
final Optional<Class<? extends Component>> target = getSource()
.getRegistry().getNavigationTarget(route);

final Optional<NavigationState> navigationState = getSource()
.resolveNavigationTarget(new Location(route));
this.redirectQueryParameters = queryParameters;
if (target.isPresent()) {
rerouteTo(getNavigationState(route, List.of()));
if (navigationState.isPresent()) {
rerouteTo(navigationState.get());
} else {
// Inform that reroute target location is not known.
unknownReroute = PathUtil.trimPath(route);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
*/
package com.vaadin.flow.uitest.ui;

import java.util.List;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.AfterNavigationEvent;
import com.vaadin.flow.router.AfterNavigationObserver;
import com.vaadin.flow.router.BeforeEvent;
import com.vaadin.flow.router.HasUrlParameter;
import com.vaadin.flow.router.OptionalParameter;
import com.vaadin.flow.router.QueryParameters;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.uitest.servlet.ViewTestLayout;

Expand All @@ -22,27 +25,50 @@ public class SetParameterForwardToView extends Div
implements HasUrlParameter<String>, AfterNavigationObserver {

static final String LOCATION_ID = "location";
static final String PARAMETER_ID = "parameter";

private final Div location;
private final Div param;

public SetParameterForwardToView() {
location = new Div();
location.setId(LOCATION_ID);
param = new Div();
param.setId(PARAMETER_ID);
}

@Override
public void setParameter(BeforeEvent event,
@OptionalParameter String parameter) {
if (parameter != null && parameter.equals("one")) {
event.forwardTo(
"com.vaadin.flow.uitest.ui.SetParameterForwardToView",
"two");
if (parameter != null) {
switch (parameter) {
case "location":
event.forwardTo(
"com.vaadin.flow.uitest.ui.SetParameterForwardToView/locationTwo");
break;
case "locationRouteParameter":
event.forwardTo(
"com.vaadin.flow.uitest.ui.SetParameterForwardToView",
"locationRouteParameterTwo");
break;
case "locationRouteParameterList":
event.forwardTo(
"com.vaadin.flow.uitest.ui.SetParameterForwardToView",
List.of("locationRouteParameterListTwo"));
break;
case "locationQueryParams":
event.forwardTo(
"com.vaadin.flow.uitest.ui.SetParameterForwardToView/locationQueryParamsTwo",
QueryParameters.empty());
break;
}
}
param.setText(parameter);
}

@Override
public void afterNavigation(AfterNavigationEvent event) {
location.setText(event.getLocation().getPath());
add(location);
add(location, param);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.vaadin.flow.uitest.ui;

import java.util.List;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.AfterNavigationEvent;
import com.vaadin.flow.router.AfterNavigationObserver;
import com.vaadin.flow.router.BeforeEvent;
import com.vaadin.flow.router.HasUrlParameter;
import com.vaadin.flow.router.OptionalParameter;
import com.vaadin.flow.router.QueryParameters;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.uitest.servlet.ViewTestLayout;

@Route(value = "com.vaadin.flow.uitest.ui.SetParameterRerouteToView", layout = ViewTestLayout.class)
public class SetParameterRerouteToView extends Div
implements HasUrlParameter<String>, AfterNavigationObserver {

static final String LOCATION_ID = "location";
static final String PARAMETER_ID = "parameter";

private final Div location;
private final Div param;

public SetParameterRerouteToView() {
location = new Div();
location.setId(LOCATION_ID);
param = new Div();
param.setId(PARAMETER_ID);
}

@Override
public void setParameter(BeforeEvent event,
@OptionalParameter String parameter) {
if (parameter != null) {
switch (parameter) {
case "location":
event.rerouteTo(
"com.vaadin.flow.uitest.ui.SetParameterRerouteToView/locationTwo");
break;
case "locationRouteParameter":
event.rerouteTo(
"com.vaadin.flow.uitest.ui.SetParameterRerouteToView",
"locationRouteParameterTwo");
break;
case "locationRouteParameterList":
event.rerouteTo(
"com.vaadin.flow.uitest.ui.SetParameterRerouteToView",
List.of("locationRouteParameterListTwo"));
break;
case "locationQueryParams":
event.rerouteTo(
"com.vaadin.flow.uitest.ui.SetParameterRerouteToView/locationQueryParamsTwo",
QueryParameters.empty());
break;
}
}
param.setText(parameter);
}

@Override
public void afterNavigation(AfterNavigationEvent event) {
location.setText(event.getLocation().getPath());
add(location, param);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,45 @@
public class SetParameterForwardToIT extends ChromeBrowserTest {

@Test
public void testForwardingToViewInSetParameter() {
public void testForwardingToViewWithLocation_setParameter() {
testForwardingToView("location");
}

@Test
public void testForwardingToViewWithRouteParameter_setParameter() {
testForwardingToView("locationRouteParameter");
}

@Test
public void testForwardingToViewWithRouteParameterList_setParameter() {
testForwardingToView("locationRouteParameterList");
}

@Test
public void testForwardingToViewWithQueryParameter_setParameter() {
testForwardingToView("locationQueryParams");
}

private void testForwardingToView(String location) {
final String baseLoc = "/view/com.vaadin.flow.uitest.ui.SetParameterForwardToView";
getDriver().get(getRootURL() + baseLoc + "/one");
final String expectedParameter = location + "Two";
final String expectedLoc = "/" + expectedParameter;
getDriver().get(getRootURL() + baseLoc + "/" + location);
waitForDevServer();

waitForElementPresent(By.id(SetParameterForwardToView.LOCATION_ID));
final String locationId = findElement(
By.id(SetParameterForwardToView.LOCATION_ID)).getText();
Assert.assertTrue("should redirect to " + baseLoc + "/two",
locationId.endsWith("/two"));
waitForElementPresent(By.id(SetParameterForwardToView.PARAMETER_ID));
final String parameterValue = findElement(
By.id(SetParameterForwardToView.PARAMETER_ID)).getText();

Assert.assertTrue("should redirect to " + baseLoc + expectedLoc,
locationId.endsWith(expectedLoc));
Assert.assertTrue("should update the URL",
getDriver().getCurrentUrl().endsWith(baseLoc + "/two"));
getDriver().getCurrentUrl().endsWith(baseLoc + expectedLoc));
Assert.assertEquals("forwarded parameter", expectedParameter,
parameterValue);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.vaadin.flow.uitest.ui;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;

import com.vaadin.flow.testutil.ChromeBrowserTest;

public class SetParameterRerouteToIT extends ChromeBrowserTest {

@Test
public void testReroutingToViewWithLocation_setParameter() {
testReroutingToView("location");
}

@Test
public void testReroutingToViewWithRouteParameter_setParameter() {
testReroutingToView("locationRouteParameter");
}

@Test
public void testReroutingToViewWithRouteParameterList_setParameter() {
testReroutingToView("locationRouteParameterList");
}

@Test
public void testReroutingToViewWithQueryParameter_setParameter() {
testReroutingToView("locationQueryParams");
}

private void testReroutingToView(String location) {
final String baseLoc = "/view/com.vaadin.flow.uitest.ui.SetParameterRerouteToView";
final String expectedParameter = location + "Two";
final String expectedLoc = "/" + expectedParameter;
final String originalLoc = baseLoc + "/" + location;
getDriver().get(getRootURL() + originalLoc);
waitForDevServer();

waitForElementPresent(By.id(SetParameterForwardToView.LOCATION_ID));
final String locationId = findElement(
By.id(SetParameterForwardToView.LOCATION_ID)).getText();
waitForElementPresent(By.id(SetParameterForwardToView.PARAMETER_ID));
final String parameterValue = findElement(
By.id(SetParameterForwardToView.PARAMETER_ID)).getText();

Assert.assertTrue("should redirect to " + baseLoc + expectedLoc,
locationId.endsWith(expectedLoc));
Assert.assertTrue("should not update the URL",
getDriver().getCurrentUrl().endsWith(originalLoc));
Assert.assertEquals("rerouted parameter", expectedParameter,
parameterValue);
}

}

0 comments on commit 0f2bf75

Please sign in to comment.