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 getDefaultValue never calls conversion on a null source now. If the @DefaultValue annotation provides no actual value, getNewDefaultValueInstanceIfPossible is called. That's where the only Optional.empty() is handled.
  • If @DefaultValue provides a non-empty value, conversion is called, but with a non-null source. It eventually reaches BindConverter.convert(Object, ResolvableType, Annotation...) - line 100.
  • When a null source 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.