Skip to content

Commit f786fc3

Browse files
committed
ObjectToOptionalConverter preserves existing Optional instances
Issue: SPR-12785
1 parent dcb1145 commit f786fc3

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

spring-core/src/main/java/org/springframework/core/convert/support/ObjectToOptionalConverter.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@
3131
* of Optional when known.
3232
*
3333
* @author Rossen Stoyanchev
34+
* @author Juergen Hoeller
3435
* @since 4.1
3536
*/
3637
@UsesJava8
@@ -64,7 +65,10 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t
6465
if (source == null) {
6566
return Optional.empty();
6667
}
67-
if (targetType.getResolvableType() == null) {
68+
else if (source instanceof Optional) {
69+
return source;
70+
}
71+
else if (targetType.getResolvableType() == null) {
6872
return Optional.of(source);
6973
}
7074
else {

spring-core/src/test/java/org/springframework/core/convert/support/DefaultConversionTests.java

+37-30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -71,7 +71,7 @@ public void testStringToCharacterEmptyString() {
7171
assertEquals(null, conversionService.convert("", Character.class));
7272
}
7373

74-
@Test(expected=ConversionFailedException.class)
74+
@Test(expected = ConversionFailedException.class)
7575
public void testStringToCharacterInvalidString() {
7676
conversionService.convert("invalid", Character.class);
7777
}
@@ -108,7 +108,7 @@ public void testStringToBooleanEmptyString() {
108108
assertEquals(null, conversionService.convert("", Boolean.class));
109109
}
110110

111-
@Test(expected=ConversionFailedException.class)
111+
@Test(expected = ConversionFailedException.class)
112112
public void testStringToBooleanInvalidString() {
113113
conversionService.convert("invalid", Boolean.class);
114114
}
@@ -267,36 +267,11 @@ public void testNumberToNumber() {
267267
assertEquals(Long.valueOf(1), conversionService.convert(1, Long.class));
268268
}
269269

270-
@Test(expected=ConversionFailedException.class)
270+
@Test(expected = ConversionFailedException.class)
271271
public void testNumberToNumberNotSupportedNumber() {
272272
conversionService.convert(1, CustomNumber.class);
273273
}
274274

275-
@SuppressWarnings("serial")
276-
public static class CustomNumber extends Number {
277-
278-
@Override
279-
public double doubleValue() {
280-
return 0;
281-
}
282-
283-
@Override
284-
public float floatValue() {
285-
return 0;
286-
}
287-
288-
@Override
289-
public int intValue() {
290-
return 0;
291-
}
292-
293-
@Override
294-
public long longValue() {
295-
return 0;
296-
}
297-
298-
}
299-
300275
@Test
301276
public void testNumberToCharacter() {
302277
assertEquals(Character.valueOf('A'), conversionService.convert(65, Character.class));
@@ -827,7 +802,7 @@ public void convertObjectToOptional() {
827802
TypeDescriptor descriptor = new TypeDescriptor(parameter);
828803
Object actual = conversionService.convert("1,2,3", TypeDescriptor.valueOf(String.class), descriptor);
829804
assertEquals(Optional.class, actual.getClass());
830-
assertEquals(Arrays.asList(1,2,3), ((Optional<List<Integer>>) actual).get());
805+
assertEquals(Arrays.asList(1, 2, 3), ((Optional<List<Integer>>) actual).get());
831806
}
832807

833808
@Test
@@ -837,6 +812,38 @@ public void convertObjectToOptionalNull() {
837812
assertSame(Optional.empty(), conversionService.convert(null, Optional.class));
838813
}
839814

815+
@Test
816+
public void convertExistingOptional() {
817+
assertSame(Optional.empty(), conversionService.convert(Optional.empty(), TypeDescriptor.valueOf(Object.class),
818+
TypeDescriptor.valueOf(Optional.class)));
819+
assertSame(Optional.empty(), conversionService.convert(Optional.empty(), Optional.class));
820+
}
821+
822+
823+
@SuppressWarnings("serial")
824+
public static class CustomNumber extends Number {
825+
826+
@Override
827+
public double doubleValue() {
828+
return 0;
829+
}
830+
831+
@Override
832+
public float floatValue() {
833+
return 0;
834+
}
835+
836+
@Override
837+
public int intValue() {
838+
return 0;
839+
}
840+
841+
@Override
842+
public long longValue() {
843+
return 0;
844+
}
845+
}
846+
840847

841848
public static class TestEntity {
842849

0 commit comments

Comments
 (0)