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

Setters for java.util.Optional Properties do not allow null Values #1166

Open
buett opened this issue Apr 2, 2020 · 6 comments
Open

Setters for java.util.Optional Properties do not allow null Values #1166

buett opened this issue Apr 2, 2020 · 6 comments
Labels

Comments

@buett
Copy link

buett commented Apr 2, 2020

Following example:

@Value.Immutable
@Value.Style(jdkOnly = true)
public interface TestDTO {
    Optional<String> getName();
}

The generated setters in immutable class and builder doesn't allow null values because of using Objects.requireNonNull.

public final ImmutableTestDTO withName(String value) {
    @Nullable String newValue = Objects.requireNonNull(value, "name");
    if (Objects.equals(this.name, newValue)) return this;
    return new ImmutableTestDTO(newValue);
  }
@elucash
Copy link
Member

elucash commented Apr 3, 2020

That is correct and is according to design. Use .withName(Optional.ofNullable(null)) or enable implicit and more prone to errors nulls with Style.optionalAcceptNullable = true (then .withName(null) would work IIRC. Thanks!

@buett
Copy link
Author

buett commented Apr 3, 2020

Thanks, it works!
Is there also a possibility to avoid generating the setters with the optional parameter. Optional as method parameter is a pattern which is often discouraged and IMHO there are good reasons for that. Therefore it would be nice to have a switch to turn it off.

@elucash
Copy link
Member

elucash commented Apr 3, 2020

Optional as method parameter is a pattern which is often discouraged and IMHO there are good reasons for that.

pls, don't cite the "propaganda" B-) If someone doesn't like optionals, s/he can use @Nullable instead, other than that there are no rational reasons why return value position magically different from parameter or field.

basically, x.withValue(x.getValue()).equals(x) == true should always work, if value is optional or any other type.

@buett
Copy link
Author

buett commented Apr 3, 2020

Who decides what propaganda is? But that's not the point. ;-) Optional as result is a good choice for api description and usage.

I only ask if you could provide a way to turn it off for setters, maybe with a Style attribute.

Automatic property propagation with null values won't work because of method overloading. (Which setter to use?)
And the manually way .withName(null) won't work at the moment. It has to be cast explicitly. .withName((String)null) isn't a nice way and .withName(Optional.empty()) doesn't make it better, I think.

@buett
Copy link
Author

buett commented Apr 3, 2020

I only ask if you could provide a way to turn it off for setters, maybe with a Style attribute.

At least for builders. They don't have getters and so the change has no influence to your rule x.withValue(x.getValue()).equals(x) == true ;-)

@Anusien
Copy link

Anusien commented Oct 30, 2020

I believe this is possible by setting@Value.Style(optionalAcceptNullable = true)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants