Skip to content

Prevent NPE in inheritance assembler - #12632

Draft
elharo wants to merge 6 commits into
masterfrom
fix-12603-inheritance-npe
Draft

Prevent NPE in inheritance assembler#12632
elharo wants to merge 6 commits into
masterfrom
fix-12603-inheritance-npe

Conversation

@elharo

@elharo elharo commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

fixes #12603

Bug

DefaultInheritanceAssembler.getChildPathAdjustment() throws NPE when:

  1. child.getProjectDirectory().getFileName() returns null (root path like /)
  2. child.getArtifactId() returns null

Fix

  • Added null check for child.getProjectDirectory().getFileName() before calling .toString()

Testing

Added DefaultInheritanceAssemblerTest with three test cases:

  • Null artifactId (no project directory)
  • Root project directory (getFileName() returns null)
  • Both null artifactId and root project directory

Co-authored-by: elharo

@elharo elharo changed the title MNG-7531: Prevent NPE in inheritance assembler [MNG-7531] Prevent NPE in inheritance assembler Jul 30, 2026
@elharo elharo changed the title [MNG-7531] Prevent NPE in inheritance assembler Prevent NPE in inheritance assembler Jul 30, 2026
@elharo elharo added the bug Something isn't working label Jul 30, 2026
Removed assertNotNull checks for assembler in tests.
@elharo
elharo requested a review from Copilot July 30, 2026 14:02

@gnodet gnodet 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.

The production code fix for the NPE in getChildPathAdjustment() is correct and minimal — the getFileName() != null guard handles root paths where getFileName() returns null, with a sensible fallback to artifactId. However, the test rewrite has a regression issue.

Deleted existing test coverage (high severity)

The PR completely replaces the test file content, deleting testPluginConfiguration() and the entire XML-based test infrastructure (testInheritance helper, getModel, getPom, xmlunit comparison). These tests were added in commit d58c96bc36 by hboutemy specifically to validate reportSet inheritance in the Maven 4 (impl) model builder.

While the compat test at compat/maven-model-builder/.../DefaultInheritanceAssemblerTest.java still uses the XML resources, it exercises the compat (Maven 3) assembler, not the impl (Maven 4) DefaultInheritanceAssembler which uses InheritanceModelMerger — a different code path.

The new NPE regression tests are valuable and should be added alongside the existing tests, not used to replace them.

Minor: trivial assertions (low severity)

The assertNotNull(assembler) calls in each test method only verify that @BeforeEach ran — they don't test the assembler's behavior.

This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.

Claude Code on behalf of gnodet

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

This PR fixes a NullPointerException in DefaultInheritanceAssembler.getChildPathAdjustment() when the child model’s project directory is the filesystem root (so getFileName() returns null) and/or when artifactId is null, and adds regression tests to cover these scenarios.

Changes:

  • Added a null check for child.getProjectDirectory().getFileName() before calling .toString().
  • Replaced prior XML-based inheritance tests with focused regression tests asserting no NPE for the identified edge cases.
  • Introduced a test case covering a combined “null artifactId + root project directory” scenario with modules configured.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultInheritanceAssembler.java Adds a guard to prevent NPE when project directory is root and getFileName() is null.
impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultInheritanceAssemblerTest.java Adds regression tests asserting assembling inheritance does not throw for null artifactId and root project directory cases.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +40 to +55
void testAssembleWithNullArtifactIdDoesNotThrowNpe() {
Model parent = Model.newBuilder()
.modelVersion("4.0.0")
.groupId("test")
.artifactId("parent")
.version("1.0")
.build();

public void testInheritance(String baseName) throws Exception {
testInheritance(baseName, false);
testInheritance(baseName, true);
}
Model child = Model.newBuilder()
.modelVersion("4.0.0")
.groupId("test")
.version("1.0")
.build();

public void testInheritance(String baseName, boolean fromRepo) throws Exception {
Model parent = getModel(baseName + "-parent");
Model child = getModel(baseName + "-child");
assertDoesNotThrow(() -> assembler.assembleModelInheritance(child, parent, null, null));
}
Comment on lines +73 to +74
.pomFile(Paths.get("/pom.xml"))
.build();
Comment on lines +86 to +87
.modules(List.of("../child/pom.xml"))
.build();
@elharo
elharo marked this pull request as draft July 30, 2026 14:23
Restore XML-based inheritance tests alongside new NPE tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[maven-4.0.x] DefaultInheritanceAssembler: NPE from null artifactId

3 participants