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

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

Open
spring-issuemaster opened this issue Oct 25, 2018 · 1 comment

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster 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:

  • #21907 MethodValidationPostProcessor validates FactoryBean methods for which validation is not applicable
  • #21919 MethodValidationPostProcessor still validates FactoryBean methods on CGLIB proxies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.