Consistent conversion of Optional array/list arrangements

Issue: SPR-15918
Issue: SPR-15919
Issue: SPR-15676

(cherry picked from commit 15c82af)
This commit is contained in:
Juergen Hoeller
2017-09-20 18:28:49 +02:00
parent d11bd64e0d
commit 53a9697ff1
3 changed files with 103 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -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;
@@ -47,7 +49,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<ConvertiblePair>(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
@@ -70,7 +76,11 @@ final class ObjectToOptionalConverter implements ConditionalGenericConverter {
}
else if (targetType.getResolvableType() != null) {
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);
@@ -82,7 +92,7 @@ final class ObjectToOptionalConverter implements ConditionalGenericConverter {
private static class GenericTypeDescriptor extends TypeDescriptor {
public GenericTypeDescriptor(TypeDescriptor typeDescriptor) {
super(typeDescriptor.getResolvableType().getGeneric(0), null, typeDescriptor.getAnnotations());
super(typeDescriptor.getResolvableType().getGeneric(), null, typeDescriptor.getAnnotations());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -131,12 +131,12 @@ public abstract class ObjectUtils {
return true;
}
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();
}