Bind 'Optional' value object parameters as empty rather than null by osvetlik · Pull Request #49152 · spring-projects/spring-boot
I should have checked why the build failed first. It was a trivial fix.
Here is some more information on the ConversionService use.
- The
getDefaultValuenever calls conversion on anullsource now. If the@DefaultValueannotation provides no actual value,getNewDefaultValueInstanceIfPossibleis called. That's where the onlyOptional.empty()is handled. - If
@DefaultValueprovides a non-empty value, conversion is called, but with a non-null source. It eventually reachesBindConverter.convert(Object, ResolvableType, Annotation...)- line 100. - When a
nullsource is passed there, it never tries the actual conversion:
<T> @Nullable T convert(@Nullable Object source, ResolvableType targetType, Annotation... targetAnnotations) { if (source == null) { return null; } return (T) convert(source, TypeDescriptor.forObject(source), new ResolvableTypeDescriptor(targetType, targetAnnotations)); }
There is a method I could call, but it's private:
BindConverter.convert(Object, TypeDescriptor, TypeDescriptor)
To keep this method's visibility as is, I introduced a new method into BindConverter and reimplemented this using the ConversionService as you suggested. Excuse me for misleading you about the ConversionService not being available at this level. New push follows.