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

#3591 Potential fix for duplicate methods generated. #3604

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

Hypnagokali
Copy link

Potential fix for: #3591

I am not sure if my solution is an acceptable one, but it solves this bug for now.
Problem: There were two methods generated and both are written to the mapper class because the selectionParameters in the ContainerMappingMethods are not equal and therefore the methods are not equal and finally both are used.

  • The defined mapping has an empty instance of SelectionParameters (returned from MappingOptions)
  • The generated mapping has null for selectionParameters.

Solution: MappingOption now returns null for selectionParameters if no values have been set.

I am still confused why two methods of beanListToBeanDtoList are forged at all. I would like to take another look at this.

@zyberzebra
Copy link

zyberzebra commented May 18, 2024

I am not sure if my solution is an acceptable one, but it solves this bug for now. Problem: There were two methods generated and both are written to the mapper class because the selectionParameters in the ContainerMappingMethods are not equal and therefore the methods are not equal and finally both are used.

Good Catch, the question is now what leads to this behaviour? And is your provided solution just a workaround/bandaid for the actual problem. But I tried your solution and it works.
I'm also gonna try and have look what causes this.

Maybe it's something caused by the recursive nature here?
Bean#Beans#Bean#Beans#......
And maybe it would generate even more duplicate methods, but it stops before that because of the first duplicate error

@Hypnagokali
Copy link
Author

Hypnagokali commented May 18, 2024

Maybe it's something caused by the recursive nature here? Bean#Beans#Bean#Beans#...... And maybe it would generate even more duplicate methods, but it stops before that because of the first duplicate error

Thanks for the feedback. Yes, the recursive structure of the bean must be the cause, but the MapperCreationProcess ends "successfully" and the duplication error only becomes visible during compilation. At some point, the mapper decides not to create any more methods although the equals method fails.

Edited:
Okay, I may have found the root cause:
In the method PropertyMapping.PropertyMappingBuilder#forge() an IterableMapping is forged, but never inserted into or retrieved from the map MappingBuilderContext#getForgedMethodsUnderCreation(). With this change it seems to work as expected and only one beanListToBeanDtoList method is forged.
I still have to look at the other assignments in the PropertyMappingBuilder#forge().

@Hypnagokali
Copy link
Author

Hypnagokali commented May 25, 2024

Edited:

I have added a test for StreamMapping and MapMapping.
It works, but actually both mapping methods are generated twice. However, finally only one method is added to MappingBuilderContext#mappingsToGenerate
Sorry, I have totally missed something. There are actually two methods added for StreamMapping, but it does not lead to a compilation error, because the second method is suffixed with 1: containerBeanStreamToContainerBeanDtoStream1
I will fix this quickly. And I will rewrite the test using GeneratedSource

@Hypnagokali
Copy link
Author

Okay, everything should be done now. I hope I haven't missed anything.

I still have a question:
All workflow jobs fail with: "Some files do not have the expected license header", what does that mean?

@thunderhook
Copy link
Contributor

thunderhook commented May 25, 2024

Okay, everything should be done now. I hope I haven't missed anything.

I still have a question: All workflow jobs fail with: "Some files do not have the expected license header", what does that mean?

This is a check if every file contains the copyright header at the beginning of a file.

/*
 * Copyright MapStruct Authors.
 *
 * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
 */

this.selectionParameters = selectionParameters;

if ( selectionParameters == null ) {
this.selectionParameters = SelectionParameters.forSourceRHS( null );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a new factory method would be better than using the null as parameter? Just a thought, my architecture knowledge for mapstruct is not big enough

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right - it could be confusing. I have changed that to SelectionParameters.emptyInstance()

@zyberzebra
Copy link

It's working! Pretty cool! :) Lets see what the maintainers have to say

@zyberzebra
Copy link

Is this Issue related? #3532

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.

None yet

3 participants