Markus Jung created OWB-1442:
--------------------------------

             Summary: Derive AbstractOwbBean returnType from types instead of 
beanClass
                 Key: OWB-1442
                 URL: https://issues.apache.org/jira/browse/OWB-1442
             Project: OpenWebBeans
          Issue Type: Improvement
          Components: Core
    Affects Versions: 4.0.2
            Reporter: Markus Jung


AbstractOwbBean#getReturnType() currently defaults to getBeanClass(): 
[https://github.com/apache/openwebbeans/blob/main/webbeans-impl/src/main/java/org/apache/webbeans/component/AbstractOwbBean.java#L280]

 

This works fine for normal managed beans - producer beans override this method 
as beanClass is nut suitable there. However there is a third case that probably 
isn't handled correctly: Synthetic beans registered with a portable extension. 
IMO the spec is ambiguous on what beanClass should mean here.

 

CDI users tend to register beans with the beanClass pointing to the Extension 
that registered this synthetic bean. For example mojarra 
[https://github.com/eclipse-ee4j/mojarra/pull/5158]) and soteria 
([https://github.com/eclipse-ee4j/soteria/pull/338]) do this. This causes OWB 
to proxy the wrong type.

 

This issue originates from https://issues.apache.org/jira/browse/TOMEE-4355 but 
I do believe this is nothing that should be fixed in TomEE. Mojarra is also not 
blame here as the spec just is not concise enough. IMO it would greatly benefit 
users if OWB would just allow a case like this to exist and proxy the correct 
type by resolving it from BeanAttributes#getTypes()

 

Last but not least, a test says more than 1000 words ;)
{code:java}
public class SyntheticBeanMismatchedBeanClassTest extends AbstractUnitTest {

    @Test
    public void test() {
        addExtension(new MyExtension());
        startContainer();

        assertTrue(getInstance("myFirstBean") instanceof MyFirstBean);
    }

    public static class MyExtension implements Extension {
        public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, 
BeanManager bm) {
            abd.addBean()
                    .beanClass(MyExtension.class)
                    .scope(ApplicationScoped.class)
                    .name("myFirstBean")
                    
.types(bm.createAnnotatedType(MyFirstBean.class).getTypeClosure())
                    .createWith(ctx -> new MyFirstBean());
        }
    }

    public static class MyFirstBean { }
}
{code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to