Skip to content

#3902: validate unknown properties in @Ignored#3908

Merged
thunderhook merged 5 commits into
mapstruct:mainfrom
znight1020:feature#3902
Jul 30, 2025
Merged

#3902: validate unknown properties in @Ignored#3908
thunderhook merged 5 commits into
mapstruct:mainfrom
znight1020:feature#3902

Conversation

@znight1020

@znight1020 znight1020 commented Jul 29, 2025

Copy link
Copy Markdown
Contributor

Fixes #3902

Solution
The root cause of the NullPointerException was identified within the BeanMappingMethod.handleDefinedMapping method's error reporting logic.

Root Cause Analysis

  1. For each property string in @Ignored(targets = {"foo"}), MapStruct internally creates a MappingOptions object. When processing an unknown property like "foo", the call to mapping.getElement() returns null.
  2. The processor then attempts to report the "Unknown property" error by calling Messager.printMessage(), passing the null element. This results in an NPE, as the messager requires a valid source element to determine the error location.

Implementation Details

  1. The fix addresses this by modifying the error handling logic for unknown properties within handleDefinedMapping:
    A new, more specific error message BEANMAPPING_UNKNOWN_PROPERTY_IN_IGNORED was added to Message.java.
  2. Before reporting an error, the logic now uses a specific condition (mapping.isIgnored() && mapping.getElement() == null) to detect properties that originate from an @Ignored annotation and do not exist. This correctly distinguishes the case from a standard @Mapping(target="...", ignore=true) on an unknown property.
  3. When the mapping element is null, the error is reported on the method itself (using its ExecutableElement). This prevents the original NPE and provides a clear diagnostic to the user at the method level.
  • ex) No property named "foo" exists in @ignored for target type "Issue3902Mapper.ZooDto"

With these changes, all mvn clean install builds now pass successfully.

@thunderhook thunderhook left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi @znight1020!

Thanks a lot for your PR! I have two minor comments, but other than that, great work!

additionalExcludes.add( "org/mapstruct/ap/test/bugs/_1596/*.java" );
additionalExcludes.add( "org/mapstruct/ap/test/bugs/_1801/*.java" );
additionalExcludes.add( "org/mapstruct/ap/test/bugs/_3089/*.java" );
additionalExcludes.add( "org/mapstruct/ap/test/bugs/_3902/*.java" );

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think that this is needed. I uncommented that line and my maven build was successful. Could you please take another look at it?

@znight1020 znight1020 Jul 29, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

When I run a mvn clean install locally, I'm seeing a CompilationFailureException during MavenIntegrationTest.fullFeatureTest() with the following error:

[ERROR] .../mapstruct/processor/src/test/java/org/mapstruct/ap/test/bugs/_3902/Issue3902Mapper.java:[23,12] No property named "foo" exists in @Ignored for target type "Issue3902Mapper.ZooDto"

I suspect this is because I'm running the build which includes the integrationtest module.

I noticed the comment in MavenIntegrationTest that mentions automatic exclusion for files named with *Erroneous*.

/**
 * ... excluding all classes that contain ... in their path/file name:
 * ...
 * <li>{@code *Erroneous*}</li>
 * ...
 */

Would the correct approach here be to rename Issue3902Mapper.java to ErroneousIssue3902Mapper.java and remove the explicit exclusion I added?

@thunderhook thunderhook Jul 30, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good catch!
Yes that would indeed be better, the FullFeatureCompilationExclusionCliEnhancer should be used to exclude tests for specific compilers (and it seems for some edge cases using SPIs or accessor naming strategies).

(and my comment "I uncommented that line and my maven build was successful" was a lie, I did not run the integration test module, sorry for that 😅)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's totally fine! Your comment was the perfect hint that led me to discover the Erroneous naming convention, so it was very helpful 😄

I've applied the requested changes. Thank you again for the detailed review and great guidance!

Comment thread processor/src/main/java/org/mapstruct/ap/internal/util/Message.java Outdated
@znight1020 znight1020 requested a review from thunderhook July 29, 2025 22:12
@thunderhook thunderhook merged commit 46a9c29 into mapstruct:main Jul 30, 2025
4 checks passed

@filiphr filiphr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @znight1020 for the fix.

What was the reason for not using " Did you mean "%s"?" in the BEANMAPPING_UNKNOWN_PROPERTY_IN_IGNORED error message?

args = new String[] {
targetPropertyName,
resultTypeToMap.describe(),
mostSimilarProperty

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't understand why we removed the mostSimilarProperty from this error message?

@znight1020 znight1020 Jul 31, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To be honest, my main focus was on fixing the NullPointerException and ensuring a clear, specific error message for @Ignored was reported. I completely overlooked adding the "Did you mean" suggestion, my apologies for that.

Adding it would be much more consistent with the other 'Unknown property' errors. Should I create a new PR for this enhancement?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be great if you could create another PR and add that as well

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.

Unknown fields in @Ignored lead to an internal error

3 participants