[JIRA] (BVAL-785) @NotNull validator is not triggered for custom Class with ValueExtractor
by Dariusz Mrówka (JIRA)
Dariusz Mrówka ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3... ) *created* an issue
Bean Validation ( https://hibernate.atlassian.net/browse/BVAL?atlOrigin=eyJpIjoiNTMzNzY0ZDY... ) / Bug ( https://hibernate.atlassian.net/browse/BVAL-785?atlOrigin=eyJpIjoiNTMzNzY... ) BVAL-785 ( https://hibernate.atlassian.net/browse/BVAL-785?atlOrigin=eyJpIjoiNTMzNzY... ) @NotNull validator is not triggered for custom Class with ValueExtractor ( https://hibernate.atlassian.net/browse/BVAL-785?atlOrigin=eyJpIjoiNTMzNzY... )
Issue Type: Bug Assignee: Unassigned Components: api Created: 26/Feb/2023 01:49 AM Priority: Major Reporter: Dariusz Mrówka ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3... )
Hi,
I have a simple value object class:
public class Duration {
private int timeInSec;
protected Duration() {
}
public Duration( int timeInSec) {
if (timeInSec < 0)
throw new IllegalArgumentException( "Duration cannot be negative" );
this.timeInSec = timeInSec;
}
public Duration( int min, int sec) {
this (min * 60 + sec);
}
public int getTimeInSec() {
return timeInSec;
}
}
And properly registered ValueExtractor class:
@UnwrapByDefault
public class DurationValueExtractor implements ValueExtractor<@ExtractedValue(type = Integer.class) Duration> {
@Override
public void extractValues(Duration originalValue, ValueReceiver receiver) {
receiver.value( null , originalValue != null ? originalValue.getTimeInSec() : null );
}
}
Then I have a class which is validated:
public class SomeClass {
@NotNull
@Positive
private Duration duration;
public Duration getDuration() {
return duration;
}
public void setDuration(Duration duration) {
this.duration = duration;
}
}
When hibernate validation is done the @Positive constraint is triggered fine for not null value (the value extractor is invoked as it should).
But when the field value is null the @NotNull constraint is not triggered and validation passes with no error.
It is because
org/hibernate/validator/internal/metadata/core/MetaConstraint.java
omits any default constraint validation when the custom type has the value extractor and value is null:
public boolean validateConstraint(ValidationContext<?> validationContext, ValueContext<?, Object > valueContext) {
boolean success = true ;
// constraint requiring value extraction to get the value to validate
* if ( valueExtractionPath != null ) {*
Object valueToValidate = valueContext.getCurrentValidatedValue();
* if ( valueToValidate != null ) {*
TypeParameterValueReceiver receiver = new TypeParameterValueReceiver( validationContext, valueContext, valueExtractionPath );
ValueExtractorHelper.extractValues( valueExtractionPath.getValueExtractorDescriptor(), valueToValidate, receiver );
success = receiver.isSuccess();
}
}
// regular constraint
* else {*
success = doValidateConstraint( validationContext, valueContext );
}
return success;
}
( https://hibernate.atlassian.net/browse/BVAL-785#add-comment?atlOrigin=eyJ... ) Add Comment ( https://hibernate.atlassian.net/browse/BVAL-785#add-comment?atlOrigin=eyJ... )
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.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100217- sha1:077879e )
1 year, 6 months