Skip to content

Conversation

@Hypnagokali
Copy link
Contributor

Fixes #3652

I have added a test case where the target has a property that does not exist on the source type.
The inverse inheritation still works, because this property is then simply ignored in BeanMappingMethod#handleDefinedMapping (line 1240):

else if ( inheritContext.isReversed() ) {
    // When a configuration is reverse inherited and there are no read or write accessor
    // then we should ignore this mapping.
    // This most likely means that we were mapping the source parameter to the target.
    // If the error is due to something else it will be reported on the original mapping
    return false;
}

I have another question:
The javadoc of MappingOptions#canInverse says:

mapping can only be inversed if the source was not a constant nor an expression nor a nested property

But there is no check for a nested property in this method and it seems to work: Some tests uses @InheritInverseConfiguration with nested properties:
e.g.: ReversingNestedSourcePropertiesConstructorTest#shouldGenerateNestedWithConfigReverse

Should I remove this statement from the javadoc comment?


/**
* mapping can only be inversed if the source was not a constant nor an expression nor a nested property
* and the mapping is not a 'target-source-ignore' mapping
Copy link
Contributor

@zyberzebra zyberzebra Aug 18, 2024

Choose a reason for hiding this comment

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

I'm still curious why this was explicitly stated here. I tried looking at the history of this method, but did not find a hint

Copy link
Member

Choose a reason for hiding this comment

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

I was curious too @zyberzebra. This looks like an ancient thing. The MappingOptions was called Mapping at some point. The rename was done in 7bee121.

The line isIgnored && sourceName == null was added first in 4d8bc29. And looking at that commit it seems like the logic was not ported properly, because prior to that we had

        // mapping can only be reversed if the source was not a constant nor an expression nor a nested property
        if ( constant != null || javaExpression != null ) {
            return null;
        }

        // should only ignore a property when 1) there is a sourceName defined or 2) there's a name match
        if ( isIgnored ) {
            if ( sourceName == null && !hasPropertyInReverseMethod( targetName, method ) ) {
                return null;
            }
        }

and after that we had

        // mapping can only be reversed if the source was not a constant nor an expression nor a nested property
        // and the mapping is not a 'target-source-ignore' mapping
        if ( constant != null || javaExpression != null || ( isIgnored && sourceName == null ) ) {
            return null;
        }

Things were quite different back then. We are now handling the target later and differently to back then, so the fix done by @Hypnagokali looks like spot on.

@filiphr
Copy link
Member

filiphr commented Aug 18, 2024

Thanks a lot for the quick fix @Hypnagokali. Nice spotting this.

Should I remove this statement from the javadoc comment?

Good catch @Hypnagokali. I think that we can remove the nor a nested property statement from the Javadoc. We are clearly not doing that there 😄

@Hypnagokali
Copy link
Contributor Author

All right, I have adjusted the comment 😊
Thanks for reviewing @filiphr, @zyberzebra

@filiphr filiphr merged commit b452d7f into mapstruct:main Aug 18, 2024
@filiphr
Copy link
Member

filiphr commented Aug 18, 2024

Thanks @Hypnagokali

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.

Inverse Inheritance Strategy not working for ignored mappings only with target

3 participants