Dynamic Route + HasUrlParameter: com.vaadin.flow.server.AmbiguousRouteConfigurationException

I want to create several dynamic routes with Vaadin 24.1.10:

    final RouteConfiguration routeConfiguration = RouteConfiguration.forSessionScope();
    String simpleRdf = "rdf/simple.xml/create";
    if( !routeConfiguration.isPathAvailable(simpleRdf) ) {
      routeConfiguration.setRoute(simpleRdf, EngineFormView.class, Arrays.asList(MainLayout.class));
    }
    String fakeRdf = "rdf/fake.xml/create";
    if( !routeConfiguration.isPathAvailable(fakeRdf) ) {
      routeConfiguration.setRoute(fakeRdf, EngineFormView.class, Arrays.asList(MainLayout.class));
    }

My EngineFormView is a simple view:

public class EngineFormView extends VerticalLayout implements HasUrlParameter<String> {
  
  private static final Logger log = LoggerFactory.getLogger(EngineFormView.class);

  
  public EngineFormView() {
    log.info("Constructed");
  }

  @Override
  public void setParameter(BeforeEvent event, @OptionalParameter String parameter) {
    log.info("setParameter: parameter={}", parameter);
    
  }

}

When I run my application I get:

There was an exception while trying to navigate to 'rdf/fake.xml/create' with the root cause 'com.vaadin.flow.server.AmbiguousRouteConfigurationException: Navigation targets must have unique routes, found navigation targets 'ru.rlisystems.rdf.ui.vaadin.demo.views.EngineFormView' and 'ru.rlisystems.rdf.ui.vaadin.demo.views.EngineFormView' with parameter have the same route.'

What can be wrong?

It works for me. Maybe you add it twice somewhere else by accident?

Indeed. Maybe be something old in current session.

Thank you for your time.

I have reproduced https://github.com/anatoly-spb/vaadin-dynamic-route-app

  1. Run application, open in browser
  1. Change something and save, devtools redeployes application
  1. Error occures

There was an exception while trying to navigate to ‘view/test1/create’ with the root cause ‘com.vaadin.flow.server.AmbiguousRouteConfigurationException: Navigation targets must have unique routes, found navigation targets ‘com.example.application.views.MyView’ and ‘com.example.application.views.MyView’ with parameter have the same route.’
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘com.example.application.views.MainLayout’: Failed to instantiate [com.example.application.views.MainLayout]: Constructor threw exception
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:326)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:194)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1352)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:320)
at com.vaadin.flow.spring.SpringInstantiator.getOrCreate(SpringInstantiator.java:121)
at com.vaadin.flow.di.Instantiator.createRouteTarget(Instantiator.java:136)
at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.lambda$getRouteTarget$1(AbstractNavigationStateRenderer.java:132)
at java.base/java.util.Optional.orElseGet(Optional.java:364)

That’s normal because devtools tries to reuse the session. Sounds like your adding of routes isn’t intelligent enough to check if the route was already registered.

I use isPathAvailable

isPathAvailable returns false, I try to call setRoute and catch AmbiguousRouteConfigurationException

How it should be done correctly?

Maybe I’m reading it wrong, but shouldn’t you only call setRoute if isPathAvailable returns true?

yes, I think so, but isPathAvailable returns false for existing path after devtools reloading

That sounds like a bug, then

as the result I get AmbiguousRouteConfigurationException

here is my code: https://github.com/anatoly-spb/vaadin-dynamic-route-app/blob/main/src/main/java/com/example/application/views/MainLayout.java#L68

Probably the issue here is that you have a HasUrlParameter view, so you should probably check for "view/test1/create/"+HasUrlParameterFormat.PARAMETER+"?" in isPathAvailbable

Good point, thank you, I will check it.