Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide empty Optional for missing values when using @ConstructorBinding #19803

Closed
youngm opened this issue Jan 18, 2020 · 2 comments
Closed
Labels
status: duplicate A duplicate of another issue

Comments

@youngm
Copy link
Contributor

youngm commented Jan 18, 2020

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 spring-projects/spring-framework#18505 (comment) 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;
  }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 18, 2020
@youngm
Copy link
Contributor Author

youngm commented Jan 18, 2020

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

@snicoll
Copy link
Member

snicoll commented Jan 20, 2020

@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.

@snicoll snicoll closed this as completed Jan 20, 2020
@snicoll snicoll added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants