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

Custom ConstraintValidator injects null in native-image #34079

Open
mingchiuli opened this issue Dec 12, 2024 · 2 comments
Open

Custom ConstraintValidator injects null in native-image #34079

mingchiuli opened this issue Dec 12, 2024 · 2 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing type: bug A general bug
Milestone

Comments

@mingchiuli
Copy link

demo project link: https://github.com/mingchiuli/demo

public class AnnoValidator implements ConstraintValidator<Anno, Pojo>  {

    @Resource
    private TestComponent testComponent;

    @Override
    public boolean isValid(Pojo value, ConstraintValidatorContext context) {
        testComponent.call();
        return true;
    }
}

In Spring boot 3.3.6, I found if I injected a component in a implement Class of ConstraintValidator in JVM mode, it worked normal:

(console output:)

123
test

But It would generated a NullPointerExeception in GraalVM's native-image :

2024-12-12T12:46:54.279+08:00 ERROR 36733 --- [demo] [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: jakarta.validation.ValidationException: HV000028: Unexpected exception during isValid call.] with root cause

java.lang.NullPointerException: null
	at com.example.demo.pkg.AnnoValidator.isValid(AnnoValidator.java:14) ~[demo:na]
	at com.example.demo.pkg.AnnoValidator.isValid(AnnoValidator.java:7) ~[demo:na]

It seems the TestComponent can't be injected in native-image mode

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 12, 2024
@snicoll
Copy link
Member

snicoll commented Dec 12, 2024

@mingchiuli thanks for the report and the reproducer. ConstraintValidator is an hibernate entity that is instantiated on demand from Hibernate Validator. As it's not discovered by Spring AOT, we can't detect the use of @Resource and generate the appropriate code so that it works in a native image.

Adding hints won't matter as the bean post processor that's responsible for this is not active when running in a native image. We'll have to figure out what to do for this use case.

@snicoll snicoll added in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 12, 2024
@snicoll snicoll added this to the 6.2.x milestone Dec 12, 2024
@snicoll
Copy link
Member

snicoll commented Dec 12, 2024

In the meantime, please move the dependency to the constructor so that we can process the type as expected, something like:

public class AnnoValidator implements ConstraintValidator<Anno, Pojo> {

	private final TestComponent testComponent;

	public AnnoValidator(TestComponent testComponent) {
		this.testComponent = testComponent;
	}

	...
} 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants