Skip to content

#674 Support Java 8 java.util.Optional type#2874

Closed
ibogdanchikov wants to merge 1 commit intomapstruct:mainfrom
ibogdanchikov:support-java-optional
Closed

#674 Support Java 8 java.util.Optional type#2874
ibogdanchikov wants to merge 1 commit intomapstruct:mainfrom
ibogdanchikov:support-java-optional

Conversation

@ibogdanchikov
Copy link
Contributor

Adds support for mapping between java.util.Optional<T> and T. See #674 for details.

Asking the maintainers for a code review. Thank you for your time.

This is my first time ever trying to contribute to an open source project, so I apologize in advance for anything I might have done wrong.

@filiphr
Copy link
Member

filiphr commented Jun 6, 2022

Thanks a lot for the PR @ibogdanchikov. On first sight it looks OK.

I saw that you want for the build in method approach. However, I don't think that this is needed. I think that this would be more suited to work with inline conversions. And do things like Optional.ofNullable( ... ) and optional.get() for the mapping for from and to.

In addition to this, for Optional#get to be able to be used I think that we need to add some out of the box conditional method that would do the check if the optional is present.

Basically the code that is generated to look something like:

targetOptionalProperty.setToOptionalProp( Optional.ofNullable( source.getToOptionalProp() ) );
if ( source.getFromOptionalProp().isPresent() ) {
    targetOptionalProperty.setFromOptionalProp( source.getFromOptionalProp().get() );
}

@filiphr
Copy link
Member

filiphr commented Jun 6, 2022

This is my first time ever trying to contribute to an open source project, so I apologize in advance for anything I might have done wrong.

By the way. Congrats on your first open source contribution. We are happy to see that you chose MapStruct 😄

@ibogdanchikov
Copy link
Contributor Author

I saw that you want for the build in method approach. However, I don't think that this is needed. I think that this would be more suited to work with inline conversions.

I totally agree. I think this would be a much better solution. May I try and implement inline conversion instead? Would you be OK with that?

@filiphr
Copy link
Member

filiphr commented Jun 6, 2022

Yes of course @ibogdanchikov. Feel free to try the other approach. Let us know if you have any questions

@ibogdanchikov ibogdanchikov force-pushed the support-java-optional branch 2 times, most recently from 9c4b87e to 95725b1 Compare June 7, 2022 10:54
@ibogdanchikov
Copy link
Contributor Author

@filiphr I've reimplemented Optional mapping using inline conversion. Could you please take a look?

@jlamb1
Copy link

jlamb1 commented Jul 21, 2022

thank you for taking this on! @filiphr do you mind looking over this?

@filiphr
Copy link
Member

filiphr commented Jul 24, 2022

I've started looking into this and we want and will have this in 1.6. One thing I want to fix is the null checks that are being done on Optional properties. We shouldn't be doing that, and I am not sure how easy it would be to achieve this.

@ibogdanchikov
Copy link
Contributor Author

@filiphr Could you please elaborate on how would you like to fix the null checks? I would be very grateful if you could provide trivial examples showing in what ways the generated code would look wrong and how would you like it to look. Thank you.

@filiphr
Copy link
Member

filiphr commented Aug 20, 2022

Sorry for taking so long time @ibogdanchikov.

What I basically would like to see is instead of:

if ( source.getFromOptionalProp() != null ) {
    target.setFromOptionalProp( source.getFromOptionalProp().orElse( null ) );
}

I would like to see

if ( source.getFromOptionalProp().isPresent() ) {
    target.setFromOptionalProp( source.getFromOptionalProp().get() );
}

or if we had some out-of-the-box presence check method for optionals:

if ( isPresent( source.getFromOptionalProp() ) ) {
    target.setFromOptionalProp( source.getFromOptionalProp().get() );
}

@ibogdanchikov ibogdanchikov force-pushed the support-java-optional branch from 95725b1 to 765de64 Compare August 27, 2022 14:09
@ibogdanchikov
Copy link
Contributor Author

ibogdanchikov commented Aug 27, 2022

@filiphr Thank you very much for the examples, they were very helpful! I adjusted the implementation according to these examples.

Specifically, I added the following condition to CommonMacros.ftl:

<#if getSourceType().isOptionalType()>.isPresent()<#else> != null</#if>

and changed OptionalToObjectConversion.getToExpression to "<SOURCE>.get()", so now it generates the following code:

if ( source.getFromOptionalProp().isPresent() ) {
    target.setFromOptionalProp( source.getFromOptionalProp().get() );
}

Please let me know if this solution is acceptable. Thank you.

@filiphr
Copy link
Member

filiphr commented Aug 28, 2022

This looks better @ibogdanchikov. Thanks a lot. I'll get your branch and play around with it before merging it

@Tristan107
Copy link

Hi @filiphr, did you find something blocking the merge of this feature ?

@filiphr
Copy link
Member

filiphr commented Apr 22, 2023

Hi @filiphr, did you find something blocking the merge of this feature ?

Unfortunately, I haven't had the time I want to look better into this one. Recently there was another PR (#3183) for this functionality. So I'll need to analyze both and see what works best for MapStruct

@filiphr
Copy link
Member

filiphr commented May 1, 2023

I had some time to look into this and #3183. The approach which is taken in #3183 is more flexible and allows mapping between different types e.g. Optional<Integer> -> Optional<String>. However, in this PR that doesn't work. Therefore, I am going to close this in favour of #3183. I am really sorry for this @ibogdanchikov, I hope you understand.

@filiphr filiphr closed this May 1, 2023
@MakraGitHub
Copy link

Optional selectByEmail(String email) does not support constructor in spring boot myBatis query
How to fixed it>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants