I'm attempting to use immutable @ConfigurationProperties objects with @ConstructorBinding. Despite the opinion of many, I like my constructors to declare Optionals for optional arguments.

Currently I get a null value passed into my constructor if the property doesn't exist. It would be nice if an empty optional could be provided if the argument is of type optional and the value is null. Thoughts?

Juergen's suggestion https://github.com/spring-projects/spring-framework/issues/18505#issuecomment-453435966 works great for mutable ConfigurationProperties classes but not immutable ones.

For further details, I'd like to have a class like so:

@ConfigurationProperties
public class SomeProperties {
  @NotNull private final Optional<String> someConfig;

  @ConstructorBinding
  public SomeProperties(Optional<String> someConfig) {
    this.someConfig = someConfig;
  }

  public Optional<String> getSomeConfig() {
    return someConfig;
  }
}

Instead my class looks like:

@ConfigurationProperties
public class SomeProperties {
  @NotNull private final Optional<String> someConfig;

  @ConstructorBinding
  public SomeProperties(Optional<String> someConfig) {
    this.someConfig = someConfig == null ? Optional.empty() : someConfig;
  }

  public Optional<String> getSomeConfig() {
    return someConfig;
  }
}

Comment From: youngm

This probably doesn't help my case, but I should acknowledge I have the same problem with collection classes. It would be nice if empty versions of them were supplied instead of null also.

Thanks

Comment From: snicoll

@youngm this is a duplicate of #18917. The collection case, in particular definitely is, Optional is just yet another case that we should consider when resolving this. I'll add a note to make sure we consider that as well.