ObjectToOptionalConverter preserves existing Optional instances

Issue: SPR-12785
This commit is contained in:
Juergen Hoeller 2015-03-05 18:53:03 +01:00
parent dcb1145354
commit f786fc3226
2 changed files with 43 additions and 32 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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. * of Optional when known.
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Juergen Hoeller
* @since 4.1 * @since 4.1
*/ */
@UsesJava8 @UsesJava8
@ -64,7 +65,10 @@ final class ObjectToOptionalConverter implements ConditionalGenericConverter {
if (source == null) { if (source == null) {
return Optional.empty(); return Optional.empty();
} }
if (targetType.getResolvableType() == null) { else if (source instanceof Optional) {
return source;
}
else if (targetType.getResolvableType() == null) {
return Optional.of(source); return Optional.of(source);
} }
else { else {

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -272,31 +272,6 @@ public class DefaultConversionTests {
conversionService.convert(1, CustomNumber.class); 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 @Test
public void testNumberToCharacter() { public void testNumberToCharacter() {
assertEquals(Character.valueOf('A'), conversionService.convert(65, Character.class)); assertEquals(Character.valueOf('A'), conversionService.convert(65, Character.class));
@ -837,6 +812,38 @@ public class DefaultConversionTests {
assertSame(Optional.empty(), conversionService.convert(null, Optional.class)); 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 { public static class TestEntity {