As a user of Hibernate Validator, I would expect that a field like `private Optional<List<ConsumerGroupDTO>> consumerGroups;` where `ConsumerGroupDTO` is the following pojo:
@Data
@SuperBuilder
@NoArgsConstructor
public class ConsumerGroupDTO extends ConsumerDTO {
private @NotEmpty String consumerGroupId;
private boolean simpleConsumerGroup;
}
gets properly validated and constraint violation exception will be thrown in case `consumerGroupId` value will be set either to null or an empty string, instead validation does not kick-in for the list of pojos wrapped in the `Optional`. If the value of `Optional` is a pojo or a primitive, validation rules are working fine. I did a little bit more digging and tried to write my own value extractor:
public class OptionalListValueExtractor<T> implements ValueExtractor<@ExtractedValue(type = List.class) Optional<List<T>>> {
@Override
public void extractValues(@ExtractedValue(type = List.class) Optional<List<T>> originalValue, ValueReceiver valueReceiver) {
valueReceiver.value(null, originalValue.orElse(null));
}
}
but when I register this class as a service, I get an exception with the cause:
Caused by: javax.validation.valueextraction.ValueExtractorDefinitionException: HV000225: Only unbound wildcard type arguments are supported for the container type of the value extractor: com.solace.maas.event.core.validation.OptionalListValueExtractor.
It seems it doesn't like that I have a class type specified in my Optional. But without this generic, it is impossible to have the extractor working the right way. Anyways, I think it is either a bug in HV or a functional efficiency. Looking forward to your reply! |