Skip to content

Commit

Permalink
fix: add RouterLayout as bean classes (#85)
Browse files Browse the repository at this point in the history
Currently RouterLayout implementations are ignored by CDI in Quarkus
This change specify RouterLayout implementors as additional bean classes
to be analyzed during bean discovery.

Fixes #75
  • Loading branch information
mcollovati authored Sep 15, 2022
1 parent 500c8e9 commit 8b8e795
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.vaadin.quarkus.deployment;

import javax.servlet.annotation.WebServlet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
Expand Down Expand Up @@ -46,9 +45,12 @@
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vaadin.flow.router.HasErrorParameter;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouterLayout;
import com.vaadin.flow.server.VaadinServlet;
import com.vaadin.quarkus.QuarkusVaadinServlet;
import com.vaadin.quarkus.WebsocketHttpSessionAttachRecorder;
Expand All @@ -64,8 +66,6 @@
import com.vaadin.quarkus.context.UIScopedContext;
import com.vaadin.quarkus.context.VaadinServiceScopedContext;
import com.vaadin.quarkus.context.VaadinSessionScopedContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class VaadinQuarkusProcessor {

Expand Down Expand Up @@ -94,6 +94,18 @@ public void build(
.produce(new BeanDefiningAnnotationBuildItem(ROUTE_ANNOTATION));
}

@BuildStep
public void specifyRouterLayoutBeans(CombinedIndexBuildItem item,
BuildProducer<AdditionalBeanBuildItem> additionalBeanProducer) {
Collection<ClassInfo> layouts = item.getComputingIndex()
.getAllKnownImplementors(
DotName.createSimple(RouterLayout.class.getName()));
for (ClassInfo layoutInfo : layouts) {
additionalBeanProducer.produce(AdditionalBeanBuildItem
.unremovableOf(layoutInfo.name().toString()));
}
}

@BuildStep
public void specifyErrorViewsBeans(CombinedIndexBuildItem item,
BuildProducer<AdditionalBeanBuildItem> additionalBeanProducer) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.vaadin.flow.quarkus.it.layout;

import javax.inject.Inject;

import com.vaadin.flow.component.AttachEvent;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.quarkus.it.Counter;
import com.vaadin.flow.router.RouterLayout;

public class LayoutWithInjection extends Div implements RouterLayout {

public static final String LAYOUT_COUNTER_ID = "layoutCounter";

@Inject
Counter counter;

@Override
protected void onAttach(AttachEvent attachEvent) {
int value = counter.increment(LayoutWithInjection.class.getName());
Span span = new Span("Counter: " + value);
span.setId(LAYOUT_COUNTER_ID);
getElement().appendChild(span.getElement());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.vaadin.flow.quarkus.it.layout;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.Route;

@Route(value = "injected-layout-view", layout = LayoutWithInjection.class)
public class LayoutWithInjectionView extends Div {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.vaadin.flow.quarkus.it;

import io.quarkus.test.junit.QuarkusIntegrationTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.vaadin.flow.quarkus.it.layout.LayoutWithInjection;

@QuarkusIntegrationTest
class LayoutWithInjectionIT extends AbstractCdiIT {

@Override
protected String getTestPath() {
return "/injected-layout-view";
}

@Test
void layout_injectedComponent() {
open();
WebElement spanElement = waitUntil(driver -> driver
.findElement(By.id(LayoutWithInjection.LAYOUT_COUNTER_ID)));

Assertions.assertEquals("Counter: 1", spanElement.getText());
}

}

0 comments on commit 8b8e795

Please sign in to comment.