Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beans created by factories have their interceptor(s) from factory method rather than the bean classes #10836

Open
nbrugger-tgm opened this issue May 17, 2024 · 4 comments

Comments

@nbrugger-tgm
Copy link

nbrugger-tgm commented May 17, 2024

Expected Behavior

Important note: Currently this bug provides a workaround for issue #10833 so solving this bug (before the other one) would actually remove the current workaround for #10833)

  1. When a class has intercepted(/executable?) annotations on it (like @NotNull) the class should have a $Interceptor generated when created in a @Factory
  2. A class should not have an interceptor generated when it has no intercepted(/executable?) annotations on it (like @NotNull) even when instantiated by an @Factory

Actual Behaviour

Pretty much the opposite of the aformentioned expected behaviors. The annotations on the types of the beans do not influence interceptor generation at all, but only the annotations on the factory method!

So this in this example

@Factory
public class BrokenFactory {
    @Bean
    public @NotNull BeanThatDoesntNeedInterceptor noInterceptor() {
        return new BeanThatDoesntNeedInterceptor();
    }
    @Bean
    public BeanThatShouldHaveInterceptor withInterceptor() {
        return new BeanThatShouldHaveInterceptor();
    }
}
  1. the noInterceptor bean will always be intercepted regardless of its type (BeanThatDoesntNeedInterceptor)
  2. the withInterceptor bean wil never be intercepted regardless of its type (BeanThatShouldHaveInterceptor)

the interceptor creation is controlled by the @NotNull on the factory method rather than by the type.

Its questionable if @NotNull on a factory method makes sense in the first place but for now its very usefull to work around bug #10833!

Steps To Reproduce

  1. git clone https://github.com/nbrugger-tgm/micronaut-reproductions
  2. cd micronaut-reproductions/micronaut-4-factory-regression-3
  3. ./gradlew test

Environment Information

  • OS: Kubuntu
  • JDK: 17

Example Application

https://github.com/nbrugger-tgm/micronaut-reproductions

Version

4.4.2

@dstepanov
Copy link
Contributor

Right now, method annotations are merged with the class annotations; that's why the interceptor is created. If you don't need it, don't include annotations that require an interceptor.

@nbrugger-tgm
Copy link
Author

nbrugger-tgm commented May 17, 2024

@graemerocher
Copy link
Contributor

there is a debate to be had an maybe an annotation hint could be provided whether the annotation applies to the factory method or the class. Currently they apply to the class and changing this would be a breaking change that we cannot implement. If you want a new feature then sure create a feature request to allow disambiguation.

@nbrugger-tgm
Copy link
Author

nbrugger-tgm commented May 17, 2024

I mean I do not have too much of a problem with the Annotation from the factory being applied to the class (altho yes it should be documented)

But as far as I see it in the case of BeanThatShouldHaveInterceptor the missing interceptor is more a bug - because the validations set in BeanThatShouldHaveInterceptor do not apply and it's unnoticeable unless there are tests on it -> its a silent ignoring of annotations. If it is a technical limitation (annotations that are on classes constructed in Factories do not work) an error or more likely warning should appear.

Calling a @NotNull annotated method with null being possible is a bug from my point of view

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants