Skip to content

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

@rsalbergaria

Description

@rsalbergaria

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>

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions