micronaut-projects / micronaut-platform

Micronaut Bill Of Materials and Version Catalog
Apache License 2.0
14 stars 10 forks source link

Dependency Injection Issues After Upgrading from Micronaut 4.5.1 to 4.6.x #1705

Open rsalbergaria opened 2 weeks ago

rsalbergaria commented 2 weeks ago

Discussed in https://github.com/micronaut-projects/micronaut-platform/discussions/1704

Originally posted by **rsalbergaria** October 9, 2024 We are using version **4.5.1** of the parent and everything works perfectly. I tried updating to **4.6.0**, **4.6.1**, **4.6.2**, and **4.6.3** and for some reason, my dependency injections are compromised. I have something like: ```java @Bean @Singleton @Requires(property = "SERVER") public OAuth2ClientConfig provideOAuth2ClientConfig(Config config) { return OAuth2ClientConfig.builder() .server((URI) config.getValue("SERVER", URI.class)) .authUser((String) config.getValue("USER", String.class)) .authPass((String) config.getValue("PASSWORD", String.class)) .scopes((List) config.getOptionalValues("SCOPES", String.class).orElse(ListUtil.EMPTY)) .build(); } Even with the property SERVER set, the config is not being injected. We have our own annotations and map them to the corresponding annotations in each Java framework we use: public class RequiresAnnotationMapper implements TypedAnnotationMapper { @Override public Class annotationType() { return Requires.class; } @Override public List> map(AnnotationValue annotation, VisitorContext visitorContext) { List> mappedAnnotations = new LinkedList<>(); var mappedAnnotation = resolveAnnotation(annotation); mappedAnnotations.add(mappedAnnotation); return mappedAnnotations; } private AnnotationValue resolveAnnotation(AnnotationValue annotation) { return AnnotationValue.builder(io.micronaut.context.annotation.Requires.class) .member("beans", annotation.annotationClassValues("beans")) .member("property", resolveProperty(annotation)) .member("value", annotation.get("value", String.class).orElse("")) .member("defaultValue", annotation.get("defaultValue", String.class).orElse("")) .build(); } private String resolveProperty(AnnotationValue annotation) { return annotation.get("property", String.class) .map(e -> e.toLowerCase().replace("_", ".")) .orElse(""); } } We also have the file io.micronaut.inject.annotation.AnnotationMapper with the path to our annotations. Did something change in 4.6.x that could be causing this issue?
dstepanov commented 2 weeks ago

It is probably related to https://github.com/micronaut-projects/micronaut-core/pull/10936, maybe you should set the member value only if the value is not the default one or is missing