Consistent conversion of Optional array/list arrangements
Issue: SPR-15918 Issue: SPR-15919 Issue: SPR-15676
This commit is contained in:
@@ -16,7 +16,9 @@
|
||||
|
||||
package org.springframework.core.convert.support;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -46,7 +48,11 @@ final class ObjectToOptionalConverter implements ConditionalGenericConverter {
|
||||
|
||||
@Override
|
||||
public Set<ConvertiblePair> getConvertibleTypes() {
|
||||
return Collections.singleton(new ConvertiblePair(Object.class, Optional.class));
|
||||
Set<ConvertiblePair> convertibleTypes = new LinkedHashSet<>(4);
|
||||
convertibleTypes.add(new ConvertiblePair(Collection.class, Optional.class));
|
||||
convertibleTypes.add(new ConvertiblePair(Object[].class, Optional.class));
|
||||
convertibleTypes.add(new ConvertiblePair(Object.class, Optional.class));
|
||||
return convertibleTypes;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,7 +75,11 @@ final class ObjectToOptionalConverter implements ConditionalGenericConverter {
|
||||
}
|
||||
else if (targetType.getResolvableType().hasGenerics()) {
|
||||
Object target = this.conversionService.convert(source, sourceType, new GenericTypeDescriptor(targetType));
|
||||
return Optional.ofNullable(target);
|
||||
if (target == null || (target.getClass().isArray() && Array.getLength(target) == 0) ||
|
||||
(target instanceof Collection && ((Collection) target).isEmpty())) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(target);
|
||||
}
|
||||
else {
|
||||
return Optional.of(source);
|
||||
|
||||
@@ -139,12 +139,12 @@ public abstract class ObjectUtils {
|
||||
if (obj instanceof Optional) {
|
||||
return !((Optional) obj).isPresent();
|
||||
}
|
||||
if (obj.getClass().isArray()) {
|
||||
return Array.getLength(obj) == 0;
|
||||
}
|
||||
if (obj instanceof CharSequence) {
|
||||
return ((CharSequence) obj).length() == 0;
|
||||
}
|
||||
if (obj.getClass().isArray()) {
|
||||
return Array.getLength(obj) == 0;
|
||||
}
|
||||
if (obj instanceof Collection) {
|
||||
return ((Collection) obj).isEmpty();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user