Skip to content

Commit

Permalink
fix: allow empty segments at start (#10921)
Browse files Browse the repository at this point in the history
Fixes a regression with HasUrlParameter by allowing to have an empty segment as first segment for parameters.

Fixes #10729
  • Loading branch information
caalador committed May 7, 2021
1 parent d23c0c9 commit a6bcc81
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
21 changes: 3 additions & 18 deletions flow-server/src/main/java/com/vaadin/flow/router/BeforeEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.vaadin.flow.router.internal.ErrorTargetEntry;
import com.vaadin.flow.router.internal.HasUrlParameterFormat;
import com.vaadin.flow.router.internal.NavigationStateRenderer;
import com.vaadin.flow.router.internal.PathUtil;

/**
* Abstract before event class that has the common functionalities for
Expand Down Expand Up @@ -360,7 +361,7 @@ public void forwardTo(String location) {
location));
} else {
// Inform that forward target location is not known.
forwardToUrl = trimPath(location);
forwardToUrl = PathUtil.trimPath(location);
}
}

Expand Down Expand Up @@ -504,7 +505,7 @@ public void rerouteTo(String route) {
route));
} else {
// Inform that reroute target location is not known.
rerouteToUrl = trimPath(route);
rerouteToUrl = PathUtil.trimPath(route);
}
}

Expand Down Expand Up @@ -822,20 +823,4 @@ public UI getUI() {
return ui;
}

private static String trimPath(String path) {
if (path == null) {
return "";
}

path = path.trim();

if (path.startsWith("/")) {
path = path.substring(1);
}
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
return path;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
* Utility class which contains various methods for parsing a route url into
Expand All @@ -39,7 +40,7 @@ public class PathUtil implements Serializable {
* @return a List containing the segments of the path.
*/
public static List<String> getSegmentsList(String path) {
path = trimPath(path);
path = path == null ? "" : trimSegmentsString(path);

final String[] segments = path.split("/");
if (segments.length == 1 && segments[0].isEmpty()) {
Expand All @@ -59,8 +60,8 @@ public static List<String> getSegmentsList(String path) {
* @return path form from input segments.
*/
public static String getPath(List<String> segments) {
return trimPath((segments == null || segments.isEmpty()) ? ""
: String.join("/", segments));
return trimSegmentsString(
segments == null ? "" : String.join("/", segments));
}

/**
Expand Down Expand Up @@ -105,4 +106,24 @@ public static String trimPath(String path) {
return path;
}

/**
* Trim the path by removing any leading and trailing whitespaces and
* trailing slashes.
*
* @param path
* url path to trim, not null
* @return a String representing the input path without any leading and
* trailing whitespaces or trailing slash.
*/
public static String trimSegmentsString(String path) {
Objects.requireNonNull(path);

path = path.trim();

if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
return path;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ public void url_resolves_correctly_for_optional_and_wild_parameters()
.getUrl(WildParameter.class, "there/are/many/of/us"));
}

@Test
public void wildcardPathWithEmptyParameter_emptyParameterIsAvailable() {
WildParameter.events.clear();
WildParameter.param = null;
setNavigationTargets(WildParameter.class);

router.navigate(ui, new Location("wild//two/three"),
NavigationTrigger.PROGRAMMATIC);

Assert.assertEquals("/two/three", WildParameter.param);

router.navigate(ui, new Location("wild////four/five"),
NavigationTrigger.PROGRAMMATIC);

Assert.assertEquals("///four/five", WildParameter.param);

router.navigate(ui, new Location("wild//two//four"),
NavigationTrigger.PROGRAMMATIC);

Assert.assertEquals("/two//four", WildParameter.param);
}

@Test
public void root_navigation_target_with_wildcard_parameter()
throws InvalidRouteConfigurationException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.vaadin.flow.router.internal;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -44,11 +45,15 @@ public void methods_output_expected_values() {

Assert.assertEquals("Unexpected result", segments,
PathUtil.getSegmentsList(path));
Assert.assertEquals("Unexpected result", segments,
PathUtil.getSegmentsList("/" + path));
Assert.assertEquals("Unexpected result", segments,
PathUtil.getSegmentsList(path + "/"));
Assert.assertEquals("Unexpected result", segments,

List<String> emptyStartSegment = new ArrayList<>();
emptyStartSegment.add("");
emptyStartSegment.addAll(segments);
Assert.assertEquals("Unexpected result", emptyStartSegment,
PathUtil.getSegmentsList("/" + path));
Assert.assertEquals("Unexpected result", emptyStartSegment,
PathUtil.getSegmentsList("/" + path + "/"));

Assert.assertEquals("Unexpected result", path, PathUtil.trimPath(path));
Expand Down

0 comments on commit a6bcc81

Please sign in to comment.