Skip to content

Commit

Permalink
#18012 feat!: reject AppShells classes which extend Components. Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
mvysny committed Nov 30, 2023
1 parent 738ec7c commit eaad7a9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.function.Consumer;

import com.vaadin.flow.component.Component;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

Expand Down Expand Up @@ -62,6 +63,9 @@ public class AppShellRegistry implements Serializable {

private static final String ERROR_MULTIPLE_ANNOTATION = "%n%s is not a repeatable annotation type.%n";

private static final String ERROR_EXTENDS_COMPONENT = "%nApp shell class is not allowed to extend Vaadin Component: %s. "
+ "App shells are intended for page configuration and are instantiated before the UI is created.%n";

// There must be no more than one of the following elements per document
private static final String[] UNIQUE_ELEMENTS = { "meta[name=viewport]",
"meta[name=description]", "title", "base" };
Expand Down Expand Up @@ -134,6 +138,10 @@ public void setShell(Class<? extends AppShellConfigurator> shell) {
String.format(AppShellRegistry.ERROR_MULTIPLE_SHELL,
this.appShellClass.getName(), shell.getName()));
}
if (shell != null && Component.class.isAssignableFrom(shell)) {
throw new InvalidApplicationConfigurationException(
String.format(ERROR_EXTENDS_COMPONENT, shell.getName()));
}
this.appShellClass = shell;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Set;
import java.util.concurrent.ConcurrentMap;

import com.vaadin.flow.component.Tag;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.After;
Expand Down Expand Up @@ -174,6 +175,11 @@ public static class OffendingNonPwaClass {
public static class AppShellWithPWA implements AppShellConfigurator {
}

@Tag("div")
public static class AppShellExtendingComponent extends Component
implements AppShellConfigurator {
}

@Rule
public ExpectedException exception = ExpectedException.none();

Expand Down Expand Up @@ -439,6 +445,13 @@ public void should_not_link_to_PWA_article() throws Exception {
assertTrue(arg.getValue().contains("@Viewport"));
}

@Test(expected = InvalidApplicationConfigurationException.class)
public void should_throwException_when_appShellExtendsComponent()
throws Exception {
classes.add(AppShellExtendingComponent.class);
initializer.process(classes, servletContext);
}

private VaadinServletRequest createVaadinRequest(String pathInfo) {
HttpServletRequest request = createRequest(pathInfo);
return new VaadinServletRequest(request, service);
Expand Down

0 comments on commit eaad7a9

Please sign in to comment.