Search code examples
javajax-rscdiquarkus

Injected Bean is null in my quarkus extension


I have a quite simple quarkus extension which defines a ContainerRequestFilter to filter authentication and add data to a custom AuthenticationContext.

Here is my code:

runtime/AuthenticationContext.java

public interface AuthenticationContext {
  User getCurrentUser();
}

runtime/AuthenticationContextImpl.java

@RequestScoped
public class AuthenticationContextImpl implements AuthenticationContext {
    private User user;

    @Override
    public User getCurrentUser() {
        return user;
    }

    public void setCurrentUser(User user) {
        this.user = user;
    }
}

runtime/MyFilter.java

@ApplicationScoped
public class MyFilter implements ContainerRequestFilter  {

  @Inject
  AuthenticationContextImpl authCtx;

  @Override
  public void filter(ContainerRequestContext requestContext){

    // doing some stuff like retrieving the user from the request Context
    // ...

    authCtx.setCurrentUser(retrievedUser)
    
  }
}

deployment/MyProcessor.java:

class MyProcessor {

  //... Some stuff

  @BuildStep
  AdditionalBeanBuildItem createContext() {
      return new AdditionalBeanBuildItem(AuthenticationContextImpl.class);
  }
}

I have a Null Pointer Exception in authCtx.setCurrentUser(retrievedUser) call (authCtx is never injected)

What am I missing here ?

Thanks


Solution

  • Indexing the runtime module of the extension fixes the problem. There are multiple ways to do that as mentioned in https://stackoverflow.com/a/55513723/2504224