From 5baa77e96cedff45933846809ee9b12e89e595bf Mon Sep 17 00:00:00 2001 From: Teppo Kurki Date: Wed, 28 Aug 2024 12:32:41 +0300 Subject: [PATCH] Do not use callback if forward is called for PreserveOnRefresh target (#19817) Fixes #19813 --- .../AbstractNavigationStateRenderer.java | 12 ++++- .../ui/PreserveOnRefreshForwardToView.java | 45 +++++++++++++++++++ .../ui/PreserveOnRefreshForwardingView.java | 31 +++++++++++++ .../ui/PreserveOnRefreshForwardingIT.java | 35 +++++++++++++++ 4 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/PreserveOnRefreshForwardToView.java create mode 100644 flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/PreserveOnRefreshForwardingView.java create mode 100644 flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/PreserveOnRefreshForwardingIT.java diff --git a/flow-server/src/main/java/com/vaadin/flow/router/internal/AbstractNavigationStateRenderer.java b/flow-server/src/main/java/com/vaadin/flow/router/internal/AbstractNavigationStateRenderer.java index 5d3293d1b8b..7fc16194a1d 100644 --- a/flow-server/src/main/java/com/vaadin/flow/router/internal/AbstractNavigationStateRenderer.java +++ b/flow-server/src/main/java/com/vaadin/flow/router/internal/AbstractNavigationStateRenderer.java @@ -726,10 +726,20 @@ private int forwardToExternalUrl(NavigationEvent event, private int forward(NavigationEvent event, BeforeEvent beforeNavigation) { NavigationHandler handler = beforeNavigation.getForwardTarget(); + Class forwardTargetType = beforeNavigation + .getForwardTargetType(); + + List> parentLayouts = RouteUtil + .getParentLayouts(event.getUI().getRouter().getRegistry(), + forwardTargetType, beforeNavigation.getForwardUrl()); + + boolean preserveOnRefreshTarget = isPreserveOnRefreshTarget( + forwardTargetType, parentLayouts); + NavigationEvent newNavigationEvent = getNavigationEvent(event, beforeNavigation); newNavigationEvent.getUI().getPage().getHistory().replaceState(null, - newNavigationEvent.getLocation(), true); + newNavigationEvent.getLocation(), !preserveOnRefreshTarget); return handler.handle(newNavigationEvent); } diff --git a/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/PreserveOnRefreshForwardToView.java b/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/PreserveOnRefreshForwardToView.java new file mode 100644 index 00000000000..a7965a3a6a7 --- /dev/null +++ b/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/PreserveOnRefreshForwardToView.java @@ -0,0 +1,45 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.uitest.ui; + +import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.router.BeforeEnterEvent; +import com.vaadin.flow.router.BeforeEnterObserver; +import com.vaadin.flow.router.Location; +import com.vaadin.flow.router.PreserveOnRefresh; +import com.vaadin.flow.router.QueryParameters; +import com.vaadin.flow.router.Route; + +@Route("com.vaadin.flow.uitest.ui.PreserveOnRefreshForwardToView") +@PreserveOnRefresh +public class PreserveOnRefreshForwardToView extends Div + implements BeforeEnterObserver { + + public PreserveOnRefreshForwardToView() { + } + + @Override + public void beforeEnter(BeforeEnterEvent event) { + if (event.getLocation().getPathWithQueryParameters() + .contains("initial")) { + QueryParameters queryParameters = QueryParameters.of("afterforward", + "true"); + Location location = new Location(event.getLocation().getPath(), + queryParameters); + event.getUI().getPage().getHistory().replaceState(null, location); + } + } +} diff --git a/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/PreserveOnRefreshForwardingView.java b/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/PreserveOnRefreshForwardingView.java new file mode 100644 index 00000000000..b40d8f8a1df --- /dev/null +++ b/flow-tests/test-root-context/src/main/java/com/vaadin/flow/uitest/ui/PreserveOnRefreshForwardingView.java @@ -0,0 +1,31 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.uitest.ui; + +import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.router.BeforeEnterEvent; +import com.vaadin.flow.router.BeforeEnterObserver; +import com.vaadin.flow.router.Route; + +@Route("com.vaadin.flow.uitest.ui.PreserveOnRefreshForwardingView") +public class PreserveOnRefreshForwardingView extends Div + implements BeforeEnterObserver { + + @Override + public void beforeEnter(BeforeEnterEvent event) { + event.forwardTo(PreserveOnRefreshForwardToView.class); + } +} diff --git a/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/PreserveOnRefreshForwardingIT.java b/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/PreserveOnRefreshForwardingIT.java new file mode 100644 index 00000000000..f4a69bb0243 --- /dev/null +++ b/flow-tests/test-root-context/src/test/java/com/vaadin/flow/uitest/ui/PreserveOnRefreshForwardingIT.java @@ -0,0 +1,35 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.flow.uitest.ui; + +import org.junit.Test; + +import com.vaadin.flow.testutil.ChromeBrowserTest; + +public class PreserveOnRefreshForwardingIT extends ChromeBrowserTest { + + @Test + public void forwadingToPreserveOnRefreshRoute_allowsUpdatingQueryParameters() { + open("initial"); + + waitUntil(driver -> { + String url = driver.getCurrentUrl(); + return url.endsWith( + "PreserveOnRefreshForwardToView?afterforward=true"); + }); + } +}