Skip to content

MethodValidationPostProcessor and record configuration properties lead to IllegalArgumentException #45129

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

Closed
benfonty opened this issue Apr 10, 2025 · 1 comment
Labels
status: invalid An issue that we don't feel is valid

Comments

@benfonty
Copy link

What happens

In a same Springboot project, we have a record configuration property:

@ConfigurationProperties(prefix = "myapp")
@Validated
public record MyProperties(@NotBlank String property) {}

If we want to enhance method level validation by creating a bean:

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
	return new MethodValidationPostProcessor();
}

Then when the application starts we have an exception java.lang.IllegalArgumentException: Cannot subclass final class com.example.demo.MyProperties.

It seems that Spring tries to make a cglib proxy for the record, which obviously won't work.

Minimal setup to reproduce

You will find a minimal setup to reproduce in this repo.

Work around

We found a workaround by explicitly removing the record:

public MethodValidationPostProcessor methodValidationPostProcessor() {
	return new MethodValidationPostProcessor() {
		@Override
		public boolean isEligible(Object bean, String beanName) {
			if (bean.getClass().isRecord()) {
				return false;
			}
			return super.isEligible(bean, beanName);
		}
	};
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 10, 2025
@snicoll
Copy link
Member

snicoll commented Apr 10, 2025

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Apr 10, 2025
@snicoll snicoll added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants