Skip to content

Dependency injection from inherited class doesn't work if class itself doesn't have one #338

Description

@brian-mulier-p

Expected Behavior

Dependency injection for parent class should work even if I don't provide a DI on the end class

Actual Behaviour

See this boilerplate

public class InjectCommand implements Runnable {
    @Inject
    private ApplicationContext applicationContext;

    public void run() {
        System.out.println("App Context: " + applicationContext);
    }
}

with

@Command(name = "picocli-working", description = "...",
    mixinStandardHelpOptions = true)
public class WorkingCommand extends InjectCommand {
    @Inject
    @Nullable
    private ObjectMapper mapper;

    public static void main(String[] args) throws Exception {
        PicocliRunner.run(WorkingCommand.class, args);
    }
}

the dependency injection is properly propagated to the parent class and it works
image

With that (notice I removed the dependency injection from the end class) it won't work and I get a null injection

@Command(name = "picocli-not-working", description = "...",
    mixinStandardHelpOptions = true)
public class NotWorkingCommand extends InjectCommand {
    public static void main(String[] args) throws Exception {
        PicocliRunner.run(NotWorkingCommand.class, args);
    }
}

image

Also, if any fix, would it be possible to backport it on 3.X.X ? Thanks !

Steps To Reproduce

No response

Environment Information

No response

Example Application

https://github.com/brian-mulier-p/micronaut-picocli-di-issue

Version

PICO CLI: 5.0.1, Micronaut: 4.0.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething isn't working

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions