Skip to content

Commit 764b558

Browse files
committed
Polish "Bind Optional value object parameters as empty rather than null"
See gh-49152
1 parent 4244dd9 commit 764b558

3 files changed

Lines changed: 8 additions & 16 deletions

File tree

core/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/BindConverter.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,8 @@ private boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor t
9898

9999
@SuppressWarnings("unchecked")
100100
<T> @Nullable T convert(@Nullable Object source, ResolvableType targetType, Annotation... targetAnnotations) {
101-
if (source == null) {
102-
return null;
103-
}
104-
return (T) convert(source, TypeDescriptor.forObject(source),
105-
new ResolvableTypeDescriptor(targetType, targetAnnotations));
106-
}
107-
108-
@SuppressWarnings("unchecked")
109-
<T> @Nullable T convertNullValue(ResolvableType targetType, Annotation... targetAnnotations) {
110-
return (T) convert(null, null, new ResolvableTypeDescriptor(targetType, targetAnnotations));
101+
TypeDescriptor sourceType = (source != null) ? TypeDescriptor.forObject(source) : null;
102+
return (T) convert(source, sourceType, new ResolvableTypeDescriptor(targetType, targetAnnotations));
111103
}
112104

113105
private @Nullable Object convert(@Nullable Object source, @Nullable TypeDescriptor sourceType,
@@ -125,6 +117,9 @@ private boolean canConvert(@Nullable TypeDescriptor sourceType, TypeDescriptor t
125117
}
126118
}
127119
}
120+
if (source == null) {
121+
return null;
122+
}
128123
throw (failure != null) ? failure : new ConverterNotFoundException(sourceType, targetType);
129124
}
130125

core/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/ValueObjectBinder.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
* @author Stephane Nicoll
6262
* @author Phillip Webb
6363
* @author Scott Frederick
64+
* @author Ondřej Světlík
6465
*/
6566
class ValueObjectBinder implements DataObjectBinder {
6667

@@ -132,12 +133,7 @@ public <T> void onUnableToCreateInstance(Bindable<T> target, Context context, Ru
132133
return convertDefaultValue(context.getConverter(), defaultValue, type, annotations);
133134
}
134135
}
135-
return convertNullValue(context, type);
136-
}
137-
138-
private <T> @Nullable T convertNullValue(Binder.Context context, ResolvableType type, Annotation... annotations) {
139-
BindConverter converter = context.getConverter();
140-
return converter.convertNullValue(type, annotations);
136+
return context.getConverter().convert(null, type);
141137
}
142138

143139
private <T> @Nullable T convertDefaultValue(BindConverter converter, String[] defaultValue, ResolvableType type,

core/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/ValueObjectBinderTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* @author Phillip Webb
5151
* @author Pavel Anisimov
5252
* @author Yanming Zhou
53+
* @author Ondřej Světlík
5354
*/
5455
class ValueObjectBinderTests {
5556

0 commit comments

Comments
 (0)