-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add support for Optional
#3971
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
Add support for Optional
#3971
Conversation
documentation/src/main/asciidoc/chapter-10-advanced-mapping-options.asciidoc
Outdated
Show resolved
Hide resolved
a7d127f to
f95c3dd
Compare
|
regarding
this looks like a breaking change Is it possible to add config option for mapping like this
i.e. |
Breaking change compare to what? Currently we do not have support for optional at all. I personally really dislike using
This is not because Jackson does that, it's more because Jackson will not invoke the setter if there is nothing in the json. |
|
breaking in terms that mapstruct usually checks source for null. But breaking was a wrong word maybe unexpected/surprising because null check is a boilerplate code and using mapstruct is the main use case for null safe boilerplate mapping code generation. @Mapper
public interface OptionalMapper {
default <E> E unwrapFromOptional(Optional<E> optional) {
return optional == null
? null
: optional.orElse(null);
}
default <E> Optional<E> wrapToOptional(E value) {
return value == null
? null
: Optional.of(value);
}
}
I don't like it either, but ther are a lot of code that used it this way and at least part of the mapstruct users use is it this way and cannot change it due to their API contracts.
This is just implementaion detail and the main point is that Optional is used as a container for patch requests. Because Optional is part of the jdk and does not require additional dependencies and because jackson supports such case with Optional out of the box. An additional use case is cloning objects using mappers that map to the same type, and you want this to be symmetrical, but this is not possible with the |
If MapStruct users are using this right now it means that they have custom methods where this is being done, this means that this will keep working like that. User defined methods always have a precedence over MapStruct internals.
That stays the same. Have a look at the constructorOptionalToOptional = source.getConstructorOptionalToOptional();When the optional can be directly assigned, it is assigned without a presence check.
I added some tests like that. I am not super keen on adding something that only small number of users is going to use, especially if it can be achieved with custom methods. I would actually prefer to try to keep it simple and in the spirit of how optional is supposed to be used without any warnings from the IDE. If I treat |
| @Override | ||
| public String toString() { | ||
| if ( optionalType.getFullyQualifiedName().equals( "java.util.Optional" ) ) { | ||
| return getAssignment() + ".get()"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even since there is no difference in the result, I just wanted to mention that
the (official) preferred alternative instead of get() is orElseThrow().
(Not sure if this is the correct place for this comment; it just looked the most promising for me.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
orElseThrow() is there from Java 10 and we aim to support Java 8 code generation. It could be an improvement that we can do (like we are doing with the isEmpty() check), but I would do it separately from this PR. I've opened #3976 as a follow-up for this
$ Conflicts: $ processor/src/main/java/org/mapstruct/ap/internal/model/common/Type.java
* Support conversions * Use BeanMappingMethod for mappings
9bb42ac to
8366d06
Compare
|
Good job!!!!! |
This PR builds on top of PR #3183 and resolves #674.
The most notable things in the additional rework of #3183 are the following:
null, i.e. we can always callisPresent()/isEmpty()on anOptionalOptional<String>->String,Optional<Long>->Stringetc.