Uploaded image for project: 'OpenEJB'
  1. OpenEJB
  2. OPENEJB-2125

CDI extension that wraps an InjectionTarget (JAX-RS) causes a ClassCastException

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • (not version related)
    • None
    • integration, tomee
    • None

    Description

      If an arbitrary CDI extension wraps a InjectionTarget during @Observes ProcessInjectionTarget<T> event, it will result in a ClassCastException in TomEE JAX-RS service startup.

      Example

      // InjectionTarget wrapper
      public class InjectionTargetWrapper<T> implements InjectionTarget<T>
      {
          private final InjectionTarget<T> wrapped;
          
          public InjectionTargetWrapper(InjectionTarget<T> injectionTarget){
              this.wrapped = injectionTarget;
          }
      
          // All InjectionTarget interface methods
          // delegate to the wrapped instance and 
          // decorate the wrapped instance 
          // implementation where needed
      
      }
      
      // CDI extension
      public class SomeExtension implements Extension {
      
          <T> void processInjectionTarget(@Observes ProcessInjectionTarget<T> pit, BeanManager beanManager) {
              InjectionTarget<T> it = pit.getInjectionTarget();
              pit.setInjectionTarget(new InjectionTargetWrapper<T>(it);
          }
      }
      

      With the aforementioned wrapping, any existing CDI managed JAX-RS endpoint that is processed by the CDI extension will have its InjectionTarget changed to the wrapper type: InjectionTargetWrapper.

      This results in a ClassCastException during TomEE JAX-RS service startup, more precisely in org.apache.openejb.server.cxf.rs.CdiResourceProvider#findContexts(final Class<?> clazz).

      This method will be called by the CXF application deployment while processing the JAX-RS endpoint bean and will cast the bean InjectionTarget to InjectionTargetImpl. Since the JAX-RS bean InjectionTarget has been changed to the wrapper - of type InjectionTargetWrapper - this results in a ClassCastException.

      This has been originally reported at GitHub. The specific CDI extension class may also be found at GitHub com.byteslounge.cdi.extension.PropertyExtension.

      While the just mentioned extension is more complex than the initial example described in this issue, this problem will occur for every CDI extension that happens to process and change a JAX-RS endpoint InjectionTarget to another type, ex: a wrapper (if the wrapper itself does not inherit from OpenWebBeans InjectionTargetImpl of course, but this is not likely to happen for the obvious reasons).

      I will attach a very simple ready-to-build project that demonstrates the issue. The project has a JAX-RS endpoint and includes the aforementioned CDI extension. The problem will immediately occur if the application is deployed in TomEE JAX-RS v1.7.3

      Attachments

        1. test-app.zip
          3 kB
          Gonçalo Marques

        Activity

          People

            Unassigned Unassigned
            gonmarques Gonçalo Marques
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated: