private void configureParameterNameProvider(ParameterNameDiscoverer discoverer, Configuration<?> configuration) { final ParameterNameProvider defaultProvider = configuration.getDefaultParameterNameProvider(); configuration.parameterNameProvider(new ParameterNameProvider() { @Override public List<String> getParameterNames(Constructor<?> constructor) { String[] paramNames = discoverer.getParameterNames(constructor); return (paramNames != null ? Arrays.asList(paramNames) : defaultProvider.getParameterNames(constructor)); } @Override public List<String> getParameterNames(Method method) { String[] paramNames = discoverer.getParameterNames(method); return (paramNames != null ? Arrays.asList(paramNames) : defaultProvider.getParameterNames(method)); } }); }
private void configureParameterNameProvider(ParameterNameDiscoverer discoverer, Configuration<?> configuration) { final ParameterNameProvider defaultProvider = configuration.getDefaultParameterNameProvider(); configuration.parameterNameProvider(new ParameterNameProvider() { @Override public List<String> getParameterNames(Constructor<?> constructor) { String[] paramNames = discoverer.getParameterNames(constructor); return (paramNames != null ? Arrays.asList(paramNames) : defaultProvider.getParameterNames(constructor)); } @Override public List<String> getParameterNames(Method method) { String[] paramNames = discoverer.getParameterNames(method); return (paramNames != null ? Arrays.asList(paramNames) : defaultProvider.getParameterNames(method)); } }); }
@Override public <T> void beforeMethodInvoke(SwaggerInvocation invocation, SwaggerProducerOperation producerOperation, Object[] args) throws ConstraintViolationException { if (null == executableValidator) { ValidatorFactory factory = Validation.byDefaultProvider() .configure() .parameterNameProvider(new DefaultParameterNameProvider()) .buildValidatorFactory(); executableValidator = factory.getValidator().forExecutables(); } Set<ConstraintViolation<Object>> violations = executableValidator.validateParameters(producerOperation.getProducerInstance(), producerOperation.getProducerMethod(), args, Default.class); if (violations.size() > 0) { LOGGER.warn("Parameter validation failed : " + violations.toString()); throw new ConstraintViolationException(violations); } }
public ValidationExtension() { Configuration<?> config = Validation.byDefaultProvider().configure(); // we use the default ParameterNameProvider here as we cannot use the injected one // as it hasn't been turned into a managed bean yet and might not be able to // return a parameter name. At this stage, we don't care about the parameter names. config.parameterNameProvider( config.getDefaultParameterNameProvider() ); BootstrapConfiguration bootstrap = config.getBootstrapConfiguration(); globalExecutableTypes = bootstrap.getDefaultValidatedExecutableTypes(); isExecutableValidationEnabled = bootstrap.isExecutableValidationEnabled(); validatorFactory = config.buildValidatorFactory(); validator = validatorFactory.getValidator(); getterPropertySelectionStrategyHelper = GetterPropertySelectionStrategyHelper.forValidationFactory( validatorFactory ); executableHelper = new ExecutableHelper( new TypeResolutionHelper() ); }
@Override public T parameterNameProvider(ParameterNameProvider parameterNameProvider) { return delegate.parameterNameProvider(parameterNameProvider); }
@Override public ValidatorFactory create(CreationalContext<ValidatorFactory> ctx) { Configuration<?> config = getConfiguration(); config.constraintValidatorFactory( createConstraintValidatorFactory( config ) ); config.messageInterpolator( createMessageInterpolator( config ) ); config.traversableResolver( createTraversableResolver( config ) ); config.parameterNameProvider( createParameterNameProvider( config ) ); config.clockProvider( createClockProvider( config ) ); addValueExtractorBeans( config ); return config.buildValidatorFactory(); }
private void configureParameterNameProvider(ParameterNameDiscoverer discoverer, Configuration<?> configuration) { final ParameterNameProvider defaultProvider = configuration.getDefaultParameterNameProvider(); configuration.parameterNameProvider(new ParameterNameProvider() { @Override public List<String> getParameterNames(Constructor<?> constructor) { String[] paramNames = discoverer.getParameterNames(constructor); return (paramNames != null ? Arrays.asList(paramNames) : defaultProvider.getParameterNames(constructor)); } @Override public List<String> getParameterNames(Method method) { String[] paramNames = discoverer.getParameterNames(method); return (paramNames != null ? Arrays.asList(paramNames) : defaultProvider.getParameterNames(method)); } }); }
@PostConstruct public void start() { ValidatorFactory factory = Validation.byDefaultProvider().configure().parameterNameProvider(this).buildValidatorFactory(); validator = factory.getValidator(); }
@PostConstruct public void start() { ValidatorFactory factory = Validation.byDefaultProvider().configure().parameterNameProvider(this).buildValidatorFactory(); validator = factory.getValidator(); }
private static void initFactoryConfig(Configuration<?> factoryCfg, ValidationConfiguration cfg) { if (cfg != null) { factoryCfg.parameterNameProvider(cfg.getParameterNameProvider()); factoryCfg.messageInterpolator(cfg.getMessageInterpolator()); factoryCfg.traversableResolver(cfg.getTraversableResolver()); factoryCfg.constraintValidatorFactory(cfg.getConstraintValidatorFactory()); for (Map.Entry<String, String> entry : cfg.getProperties().entrySet()) { factoryCfg.addProperty(entry.getKey(), entry.getValue()); } } }
private static void initFactoryConfig(Configuration<?> factoryCfg, ValidationConfiguration cfg) { if (cfg != null) { factoryCfg.parameterNameProvider(cfg.getParameterNameProvider()); factoryCfg.messageInterpolator(cfg.getMessageInterpolator()); factoryCfg.traversableResolver(cfg.getTraversableResolver()); factoryCfg.constraintValidatorFactory(cfg.getConstraintValidatorFactory()); for (Map.Entry<String, String> entry : cfg.getProperties().entrySet()) { factoryCfg.addProperty(entry.getKey(), entry.getValue()); } } }
@Test(expectedExceptions = UnsupportedOperationException.class, expectedExceptionsMessageRegExp = "Exception in ParameterNameProvider") @SpecAssertion(section = Sections.CONSTRAINTDECLARATIONVALIDATIONPROCESS_METHODLEVELCONSTRAINTS_PARAMETERCONSTRAINTS_NAMINGPARAMETERS, id = "b") public void testExceptionInParameterNameProviderIsWrappedIntoValidationException() throws Throwable { Validator validator = TestUtil.getConfigurationUnderTest() .parameterNameProvider( new BrokenCustomParameterNameProvider() ) .buildValidatorFactory() .getValidator(); try { Object object = new User(); Method method = User.class.getMethod( "setNames", String.class, String.class ); Object[] parameters = new Object[] { null, null }; validator.forExecutables().validateParameters( object, method, parameters ); fail( "Expected exception wasn't thrown" ); } catch ( ValidationException e ) { throw e.getCause(); } }
@Test @SpecAssertion(section = Sections.VALIDATIONAPI_BOOTSTRAPPING_VALIDATORFACTORY, id = "e") public void testGetParameterNameProviderFromValidatorFactory() { BrokenCustomParameterNameProvider parameterNameProvider = new BrokenCustomParameterNameProvider(); ValidatorFactory validatorFactory = TestUtil.getConfigurationUnderTest() .parameterNameProvider( parameterNameProvider ) .buildValidatorFactory(); assertSame( validatorFactory.getParameterNameProvider(), parameterNameProvider, "getParameterNameProvider() should return the parameter name provider set via configuration" ); }
@Override public <T> void beforeMethodInvoke(SwaggerInvocation invocation, SwaggerProducerOperation producerOperation, Object[] args) throws ConstraintViolationException { if (null == executableValidator) { ValidatorFactory factory = Validation.byDefaultProvider() .configure() .parameterNameProvider(new DefaultParameterNameProvider()) .buildValidatorFactory(); executableValidator = factory.getValidator().forExecutables(); } Set<ConstraintViolation<Object>> violations = executableValidator.validateParameters(producerOperation.getProducerInstance(), producerOperation.getProducerMethod(), args, Default.class); if (violations.size() > 0) { LOGGER.warn("Parameter validation failed : " + violations.toString()); throw new ConstraintViolationException(violations); } }
.parameterNameProvider( new CustomParameterNameProvider() ) .buildValidatorFactory() .getValidator()
private GlobalValidatorFactory() { validatorFactory = Validation.byDefaultProvider() .configure() .parameterNameProvider(new ReflectionParameterNameProvider()) .messageInterpolator(new SeedMessageInterpolator()) .buildValidatorFactory(); }
public ValidationExtension() { Configuration<?> config = Validation.byDefaultProvider().configure(); // we use the default ParameterNameProvider here as we cannot use the injected one // as it hasn't been turned into a managed bean yet and might not be able to // return a parameter name. At this stage, we don't care about the parameter names. config.parameterNameProvider( config.getDefaultParameterNameProvider() ); BootstrapConfiguration bootstrap = config.getBootstrapConfiguration(); globalExecutableTypes = bootstrap.getDefaultValidatedExecutableTypes(); isExecutableValidationEnabled = bootstrap.isExecutableValidationEnabled(); validatorFactory = config.buildValidatorFactory(); validator = validatorFactory.getValidator(); executableHelper = new ExecutableHelper( new TypeResolutionHelper() ); }
@Override public ValidatorFactory get() { return Validation.byDefaultProvider() .configure() .parameterNameProvider(new ReflectionParameterNameProvider()) .messageInterpolator(new SeedMessageInterpolator()) .constraintValidatorFactory(new SeedConstraintValidatorFactory(injector)) .buildValidatorFactory(); } }
@Test @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "aw") @SpecAssertion(section = Sections.VALIDATIONAPI_CONSTRAINTVIOLATION, id = "az") public void testAddParameterNodeUsingCustomParameterNameProvider() throws Exception { ExecutableValidator executableValidator = TestUtil.getConfigurationUnderTest() .parameterNameProvider( new CustomParameterNameProvider() ) .buildValidatorFactory() .getValidator() .forExecutables(); Object[] parameterValues = new Object[] { Collections.emptyMap() }; Set<ConstraintViolation<User>> constraintViolations = executableValidator.validateParameters( new User(), User.class.getMethod( "setAddresses", Map.class ), parameterValues ); assertThat( constraintViolations ).containsOnlyPaths( pathWith().method( "setAddresses" ).parameter( "param0", 0 ), pathWith().method( "setAddresses" ).parameter( "param0", 0 ).bean(), pathWith().method( "setAddresses" ) .parameter( "param0", 0 ) .property( "myNode1", true, null, 23 ) .bean() ); }
@Override public ValidatorFactory create(CreationalContext<ValidatorFactory> ctx) { Configuration<?> config = getConfiguration(); config.constraintValidatorFactory( createConstraintValidatorFactory( config ) ); config.messageInterpolator( createMessageInterpolator( config ) ); config.traversableResolver( createTraversableResolver( config ) ); config.parameterNameProvider( createParameterNameProvider( config ) ); config.clockProvider( createClockProvider( config ) ); addValueExtractorBeans( config ); return config.buildValidatorFactory(); }