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

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

Open
rsalbergaria opened this issue Oct 9, 2024 Discussed in #1704 · 1 comment
Open

Comments

@rsalbergaria
Copy link

Discussed in #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:

@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<Requires> {

    @Override
    public Class<Requires> annotationType() {
        return Requires.class;
    }

    @Override
    public List<AnnotationValue<?>> map(AnnotationValue<Requires> annotation, VisitorContext visitorContext) {
        List<AnnotationValue<?>> mappedAnnotations = new LinkedList<>();
        var mappedAnnotation = resolveAnnotation(annotation);
        mappedAnnotations.add(mappedAnnotation);
        return mappedAnnotations;
    }

    private AnnotationValue<io.micronaut.context.annotation.Requires> resolveAnnotation(AnnotationValue<Requires> 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<Requires> 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?
</div>
@dstepanov
Copy link
Contributor

dstepanov commented Oct 9, 2024

It is probably related to micronaut-projects/micronaut-core#10936, maybe you should set the member value only if the value is not the default one or is missing

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

2 participants