Skip to content

Commit 9fa4ed5

Browse files
authored
feat: Add hasMandatoryParameter check to registry (#12005)
Make it possible to check if navigationTarget has a mandatory parameter that needs to be given. Fix RouteFormat parameter check error when regex parameter doesn't end with a `/`. Fixes #11502
1 parent cf025cb commit 9fa4ed5

4 files changed

Lines changed: 72 additions & 4 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/router/internal/AbstractRouteRegistry.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.vaadin.flow.function.SerializableBiConsumer;
3030
import com.vaadin.flow.internal.ReflectTools;
3131
import com.vaadin.flow.router.HasErrorParameter;
32+
import com.vaadin.flow.router.NotFoundException;
3233
import com.vaadin.flow.router.RouteAliasData;
3334
import com.vaadin.flow.router.RouteBaseData;
3435
import com.vaadin.flow.router.RouteData;
@@ -347,6 +348,18 @@ public void clean() {
347348
configure(ConfigureRoutes::clear);
348349
}
349350

351+
@Override
352+
public boolean hasMandatoryParameter(
353+
Class<? extends Component> navigationTarget) {
354+
String template = getTemplate(navigationTarget)
355+
.orElseThrow(() -> new NotFoundException(
356+
"Requested navigation target is not registered."));
357+
return (HasUrlParameterFormat.hasUrlParameter(navigationTarget)
358+
&& HasUrlParameterFormat
359+
.hasMandatoryParameter(navigationTarget))
360+
|| RouteFormat.hasRequiredParameter(template);
361+
}
362+
350363
private void configureWithFullTemplate(String path,
351364
Class<? extends Component> navigationTarget,
352365
SerializableBiConsumer<ConfigureRoutes, String> templateConfiguration) {

‎flow-server/src/main/java/com/vaadin/flow/router/internal/RouteFormat.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ static boolean hasRequiredParameter(String template) {
6161

6262
int parameterNameEnding = Math.min(regexIndex, slashIndex);
6363

64-
// Missing regex.
64+
// ending -1 then check if it's regex or slash or both that is
65+
// missing
6566
if (parameterNameEnding < 0) {
66-
parameterNameEnding = slashIndex;
67+
parameterNameEnding = regexIndex == -1 ? slashIndex
68+
: regexIndex;
6769
}
6870
// End of the string.
6971
if (parameterNameEnding < 0) {

‎flow-server/src/main/java/com/vaadin/flow/server/RouteRegistry.java‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.vaadin.flow.component.Component;
2323
import com.vaadin.flow.router.Location;
24+
import com.vaadin.flow.router.NotFoundException;
2425
import com.vaadin.flow.router.ParentLayout;
2526
import com.vaadin.flow.router.Route;
2627
import com.vaadin.flow.router.RouteAlias;
@@ -129,7 +130,7 @@ void setRoute(String path, Class<? extends Component> navigationTarget,
129130
/**
130131
* Gets the {@link RouteTarget} instance matching the given target component
131132
* and route parameters.
132-
*
133+
*
133134
* @param target
134135
* a component class which is a navigation target.
135136
* @param parameters
@@ -255,8 +256,19 @@ List<Class<? extends RouterLayout>> getRouteLayouts(String path,
255256

256257
/**
257258
* Gets the Vaadin context which the registry belongs to.
258-
*
259+
*
259260
* @return the Vaadin context
260261
*/
261262
VaadinContext getContext();
263+
264+
/**
265+
* Check if the given navigationTarget requires parameters.
266+
*
267+
* @param navigationTarget
268+
* navigation target to check
269+
* @throws NotFoundException
270+
* if given navigation target is not registered
271+
* @return {@code true} if parameters are required
272+
*/
273+
boolean hasMandatoryParameter(Class<? extends Component> navigationTarget);
262274
}

‎flow-server/src/test/java/com/vaadin/flow/router/internal/AbstractRouteRegistryTest.java‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
import com.vaadin.flow.router.BeforeEvent;
1010
import com.vaadin.flow.router.HasUrlParameter;
11+
import com.vaadin.flow.router.NotFoundException;
1112
import com.vaadin.flow.router.OptionalParameter;
13+
import com.vaadin.flow.router.RouteParameterRegex;
1214
import com.vaadin.flow.router.WildcardParameter;
1315
import com.vaadin.flow.server.InvalidRouteConfigurationException;
1416
import org.junit.Assert;
@@ -733,6 +735,40 @@ public void removing_all_targets_is_possible_and_returns_empty() {
733735
config().getTargetRoutes().isEmpty());
734736
}
735737

738+
@Test
739+
public void check_has_parameters_returns_correctly() {
740+
registry.setRoute("", NormalRoute.class, null);
741+
registry.setRoute("url", HasUrlRoute.class, null);
742+
registry.setRoute("optional", OptionalRoute.class, null);
743+
registry.setRoute("wild", WildcardRoute.class, null);
744+
registry.setRoute(
745+
ParameterView.class.getAnnotation(Route.class).value()[0],
746+
ParameterView.class, null);
747+
748+
Assert.assertEquals("All routes should be registered.", 5,
749+
config().getTargetRoutes().size());
750+
751+
Assert.assertFalse(
752+
"Normal route should not mark as requiring parameter",
753+
registry.hasMandatoryParameter(NormalRoute.class));
754+
Assert.assertFalse(
755+
"Optional parameter should not mark as requiring parameter",
756+
registry.hasMandatoryParameter(OptionalRoute.class));
757+
Assert.assertFalse(
758+
"Wildcard parameter should not mark as requiring parameter",
759+
registry.hasMandatoryParameter(WildcardRoute.class));
760+
761+
Assert.assertTrue("HasUrl should require parameter",
762+
registry.hasMandatoryParameter(HasUrlRoute.class));
763+
Assert.assertTrue("Template parameter should require parameter",
764+
registry.hasMandatoryParameter(ParameterView.class));
765+
766+
Assert.assertThrows(
767+
"Checking unregistered route should throw exception",
768+
NotFoundException.class,
769+
() -> registry.hasMandatoryParameter(Secondary.class));
770+
}
771+
736772
/* Private stuff */
737773

738774
private void awaitCountDown(CountDownLatch countDownLatch) {
@@ -824,6 +860,11 @@ public void setParameter(BeforeEvent event,
824860
}
825861
}
826862

863+
@Route(value = "item/:long(" + RouteParameterRegex.LONG + ")")
864+
@Tag("div")
865+
private static class ParameterView extends Component {
866+
}
867+
827868
@Tag(Tag.DIV)
828869
private static class Parent extends Component implements RouterLayout {
829870
}

0 commit comments

Comments
 (0)