ObjectToOptionalConverter preserves existing Optional instances
Issue: SPR-12785
This commit is contained in:
parent
dcb1145354
commit
f786fc3226
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
|
@ -31,6 +31,7 @@ import org.springframework.lang.UsesJava8;
|
|||
* of Optional when known.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.1
|
||||
*/
|
||||
@UsesJava8
|
||||
|
@ -64,7 +65,10 @@ final class ObjectToOptionalConverter implements ConditionalGenericConverter {
|
|||
if (source == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
if (targetType.getResolvableType() == null) {
|
||||
else if (source instanceof Optional) {
|
||||
return source;
|
||||
}
|
||||
else if (targetType.getResolvableType() == null) {
|
||||
return Optional.of(source);
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
|
@ -71,7 +71,7 @@ public class DefaultConversionTests {
|
|||
assertEquals(null, conversionService.convert("", Character.class));
|
||||
}
|
||||
|
||||
@Test(expected=ConversionFailedException.class)
|
||||
@Test(expected = ConversionFailedException.class)
|
||||
public void testStringToCharacterInvalidString() {
|
||||
conversionService.convert("invalid", Character.class);
|
||||
}
|
||||
|
@ -108,7 +108,7 @@ public class DefaultConversionTests {
|
|||
assertEquals(null, conversionService.convert("", Boolean.class));
|
||||
}
|
||||
|
||||
@Test(expected=ConversionFailedException.class)
|
||||
@Test(expected = ConversionFailedException.class)
|
||||
public void testStringToBooleanInvalidString() {
|
||||
conversionService.convert("invalid", Boolean.class);
|
||||
}
|
||||
|
@ -267,36 +267,11 @@ public class DefaultConversionTests {
|
|||
assertEquals(Long.valueOf(1), conversionService.convert(1, Long.class));
|
||||
}
|
||||
|
||||
@Test(expected=ConversionFailedException.class)
|
||||
@Test(expected = ConversionFailedException.class)
|
||||
public void testNumberToNumberNotSupportedNumber() {
|
||||
conversionService.convert(1, CustomNumber.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public static class CustomNumber extends Number {
|
||||
|
||||
@Override
|
||||
public double doubleValue() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float floatValue() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int intValue() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long longValue() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNumberToCharacter() {
|
||||
assertEquals(Character.valueOf('A'), conversionService.convert(65, Character.class));
|
||||
|
@ -827,7 +802,7 @@ public class DefaultConversionTests {
|
|||
TypeDescriptor descriptor = new TypeDescriptor(parameter);
|
||||
Object actual = conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), descriptor);
|
||||
assertEquals(Optional.class, actual.getClass());
|
||||
assertEquals(Arrays.asList(1,2,3), ((Optional<List<Integer>>) actual).get());
|
||||
assertEquals(Arrays.asList(1, 2, 3), ((Optional<List<Integer>>) actual).get());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -837,6 +812,38 @@ public class DefaultConversionTests {
|
|||
assertSame(Optional.empty(), conversionService.convert(null, Optional.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertExistingOptional() {
|
||||
assertSame(Optional.empty(), conversionService.convert(Optional.empty(), TypeDescriptor.valueOf(Object.class),
|
||||
TypeDescriptor.valueOf(Optional.class)));
|
||||
assertSame(Optional.empty(), conversionService.convert(Optional.empty(), Optional.class));
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public static class CustomNumber extends Number {
|
||||
|
||||
@Override
|
||||
public double doubleValue() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float floatValue() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int intValue() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long longValue() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class TestEntity {
|
||||
|
||||
|
|
Loading…
Reference in New Issue