0

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

godo57
  • 508
  • 2
  • 24
  • 1
    Have you "indexed" the `runtime` module of you dependency (using one of the methods described in https://stackoverflow.com/a/55513723/2504224)? – geoand Apr 26 '22 at 05:55
  • Thanks for pointing that thread. I was sure that I did not need to index runtime since it would be indexed by default in an extension. I added the empty beans.xml and all is okay. Thanks again – godo57 Apr 28 '22 at 10:31
  • Great to hear! I added this as an answer, please accept it so future readers can easily find the proper solution. – geoand Apr 29 '22 at 09:02

1 Answers1

1

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

godo57
  • 508
  • 2
  • 24
geoand
  • 60,071
  • 24
  • 172
  • 190