Skip to content

MethodValidationPostProcessor does not support JDK proxy from FactoryBean [SPR-17431] #21964

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
spring-projects-issues opened this issue Oct 25, 2018 · 2 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: superseded An issue that has been superseded by another

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 25, 2018

Yanming Zhou opened SPR-17431 and commented

package com.example;

import static org.junit.Assert.assertEquals;

import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import javax.validation.constraints.NotEmpty;

import org.aopalliance.intercept.MethodInterceptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.validation.annotation.Validated;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;

import com.example.MethodValidationPostProcessorTests.MyConfiguration;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = MyConfiguration.class)
public class MethodValidationPostProcessorTests {

	@Autowired
	private MyInterface myInterface;

	@Autowired
	private MyInterface myImplementation;

	@Test(expected = ConstraintViolationException.class)
	public void testJdkProxy() throws Exception {
		assertEquals("", myInterface.echo(""));
	}

	@Test(expected = ConstraintViolationException.class)
	public void testImplementation() throws Exception {
		assertEquals("", myImplementation.echo(""));
	}

	@Validated
	public interface MyInterface {

		public String echo(@NotEmpty String text);

	}

	@Configuration
	static class MyConfiguration {

		@Bean
		public FactoryBean<MyInterface> myInterface() {
			return new FactoryBean<MyInterface>() {

				@Override
				public MyInterface getObject() throws Exception {
					return (MyInterface) new ProxyFactory(MyInterface.class,
							(MethodInterceptor) mi -> mi.getArguments()[0])
									.getProxy(MyInterface.class.getClassLoader());
				}

				@Override
				public Class<?> getObjectType() {
					return MyInterface.class;
				}

			};
		}

		@Bean
		public MyInterface myImplementation() {
			return new MyInterface() {

				@Override
				public String echo(String text) {
					return text;
				}

			};
		}

		@Bean
		public LocalValidatorFactoryBean validatorFactory() {
			return new LocalValidatorFactoryBean();
		}

		@Bean
		public static MethodValidationPostProcessor methodValidationPostProcessor(@Lazy Validator validator) {
			MethodValidationPostProcessor postProcessor = new MethodValidationPostProcessor();
			postProcessor.setValidator(validator);
			return postProcessor;
		}

	}

}

testImplementation() pass and testJdkProxy() failed


Affects: 5.1.1

Issue Links:

@spring-projects-issues
Copy link
Collaborator Author

Yanming Zhou commented

Maybe it caused by FactoryBean not jdk proxy.

@snicoll
Copy link
Member

snicoll commented Sep 18, 2023

Thanks for the sample. Updating to a supported version is behaving as expected.

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Sep 18, 2023
@snicoll snicoll added status: superseded An issue that has been superseded by another and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 18, 2023
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) status: superseded An issue that has been superseded by another
Projects
None yet
Development

No branches or pull requests

2 participants