spring-projects / spring-projects/spring-framework

Custom ConstraintValidator injects null in native-image

Open
#34,079 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: core theme: aot type: bug
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked demo project and AnnoValidator.isValid, then compare ConstraintValidator injection in JVM mode with the GraalVM native-image build. Reproduce the NullPointerException and trace why TestComponent is null; done when the validator receives its injected component in native-image mode without breaking JVM behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.