[hibernate-issues] [JIRA] (HV-1761) Not Work!! When i use custom validation,ALL version!!!

deng A (JIRA) jira at hibernate.atlassian.net
Sat Mar 14 07:12:53 EDT 2020


deng A ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5e6cb3747653310cfe82b017 ) *updated* an issue

Hibernate Validator ( https://hibernate.atlassian.net/browse/HV?atlOrigin=eyJpIjoiN2RiNGMyOTZjOWRmNDA5Zjk3ZjFlZmU1MDc2ZmIwN2EiLCJwIjoiaiJ9 ) / Bug ( https://hibernate.atlassian.net/browse/HV-1761?atlOrigin=eyJpIjoiN2RiNGMyOTZjOWRmNDA5Zjk3ZjFlZmU1MDc2ZmIwN2EiLCJwIjoiaiJ9 ) HV-1761 ( https://hibernate.atlassian.net/browse/HV-1761?atlOrigin=eyJpIjoiN2RiNGMyOTZjOWRmNDA5Zjk3ZjFlZmU1MDc2ZmIwN2EiLCJwIjoiaiJ9 ) Not Work!! When i use custom validation,ALL version!!! ( https://hibernate.atlassian.net/browse/HV-1761?atlOrigin=eyJpIjoiN2RiNGMyOTZjOWRmNDA5Zjk3ZjFlZmU1MDc2ZmIwN2EiLCJwIjoiaiJ9 )

Change By: deng A ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=5e6cb3747653310cfe82b017 )

i learn springboot validation with hibernate-validator recently.
i write some custom validator
however it not work when i custom message
i want to valid a enum flag

relation class:

[https://github.com/hibernate/hibernate - validator/blob/master/engine/src/main/java/org/hibernate/validator/internal/engine/messageinterpolation/ParameterTermResolver.java|https://github.com/hibernate/hibernate - validator/blob/master/engine/src/main/java/org/hibernate/validator/internal/engine/messageinterpolation/ParameterTermResolver.java|smart - link]

- ---

{code:java}
/*
* Copyright (c) 2020. ascend 版权所有禁止转载。
*/

package cn.edu.zua.springbootdemo2.util.core.validation;

import cn.edu.zua.springbootdemo2.util.core.validation.bound.IntEnumValidatorForInteger;

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* 说明:The annotated element must be a number of values .
*
* @author adeng 2020/3/14 10:12
*/
@Documented
@Constraint(validatedBy = {IntEnumValidatorForInteger.class})
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(IntEnum.List.class)
public @interface IntEnum {
/**
* 合法值
*/
int[] value() default {};

String message() default "{javax.validation.constraints.IntEnum.message}";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

/**
* Defines several {@code @Email} constraints on the same element.
*
* @see IntEnum
*/
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Documented
@interface List {
IntEnum[] value();
}
}
{code}

----

{code:java}
/*
* Copyright (c) 2020. ascend 版权所有禁止转载。
*/

package cn.edu.zua.springbootdemo2.util.core.validation.bound;

import cn.edu.zua.springbootdemo2.util.core.validation.IntEnum;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
* 说明:
* 验证提供的值是否合法
*
* @author adeng 2020/3/14 15:21
*/
public class IntEnumValidatorForInteger implements ConstraintValidator<IntEnum, Integer> {
/**
* 合法值
*/
private int[] values;

/**
* Initializes the validator in preparation for
* {@link #isValid(Integer, ConstraintValidatorContext)} calls.
* The constraint annotation for a given constraint declaration
* is passed.
* <p>
* This method is guaranteed to be called before any use of this instance for
* validation.
* <p>
* The default implementation is a no-op.
*
* @param intEnum annotation instance for a given constraint declaration
*/
@Override
public void initialize(IntEnum intEnum) {
this.values = intEnum.value();
}

/**
* Implements the validation logic.
* The state of {@code value} must not be altered.
* <p>
* This method can be accessed concurrently, thread-safety must be ensured
* by the implementation.
*
* @param value   object to validate
* @param context context in which the constraint is evaluated
* @return {@code false} if {@code value} does not pass the constraint
*/
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
for (int item : values) {
if (value == item) {
return true;
}
}
return false;
}
}
{code}

----

*controller*

{code:java}
/**
* 自定义int枚举校验
*
* @param input 输入
* @return 输出
*/
@GetMapping(value = "/get/custom/integer")
public CommonResult<Integer> getCustomInteger(@IntEnum(value = {0, 1}) Integer input) {
CommonResult<Integer> retResult = new CommonResult<>();
retResult.fillResult(CommonCode.SUCCESS(), input);
return retResult;
}
{code}

----

messages.properties

{code:java}
javax.validation.constraints.IntEnum.message=value not valid(valid values have {value})
{code}

----

error \ ! \ ! \ !

{code:java}
Caused by: java.lang.ClassCastException: class [I cannot be cast to class [Ljava.lang.Object; ([I and [Ljava.lang.Object; are in module java.base of loader 'bootstrap')
at org.hibernate.validator.internal.engine.messageinterpolation.ParameterTermResolver.interpolate(ParameterTermResolver.java:30)
at org.hibernate.validator.internal.engine.messageinterpolation.InterpolationTerm.interpolate(InterpolationTerm.java:64)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolate(ResourceBundleMessageInterpolator.java:159)
at org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator.interpolateExpression(AbstractMessageInterpolator.java:519)
at org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator.interpolateMessage(AbstractMessageInterpolator.java:408)
at org.hibernate.validator.messageinterpolation.AbstractMessageInterpolator.interpolate(AbstractMessageInterpolator.java:355)
at org.springframework.validation.beanvalidation.LocaleContextMessageInterpolator.interpolate(LocaleContextMessageInterpolator.java:51)
at org.hibernate.validator.internal.engine.validationcontext.AbstractValidationContext.interpolate(AbstractValidationContext.java:313)
... 75 common frames omitted
{code}

( https://hibernate.atlassian.net/browse/HV-1761#add-comment?atlOrigin=eyJpIjoiN2RiNGMyOTZjOWRmNDA5Zjk3ZjFlZmU1MDc2ZmIwN2EiLCJwIjoiaiJ9 ) Add Comment ( https://hibernate.atlassian.net/browse/HV-1761#add-comment?atlOrigin=eyJpIjoiN2RiNGMyOTZjOWRmNDA5Zjk3ZjFlZmU1MDc2ZmIwN2EiLCJwIjoiaiJ9 )

Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.core&referrer=utm_source%3DNotificationLink%26utm_medium%3DEmail ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailNotificationLink&mt=8 ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100121- sha1:a2b3dcc )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/hibernate-issues/attachments/20200314/abc115ca/attachment.html 


More information about the hibernate-issues mailing list