This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch maven-3.9.x
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/maven-3.9.x by this push:
     new 66d7241a9c [Backport maven-3.9.x] Report well-formed-but-invalid POMs 
as invalid, not malformed (#13953) (#13963)
66d7241a9c is described below

commit 66d7241a9cde7765af091f6fc9c7fa860cfb5323
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Fri Nov 13 09:22:32 2026 +0100

    [Backport maven-3.9.x] Report well-formed-but-invalid POMs as invalid, not 
malformed (#13953) (#13963)
    
    Backport of #13953, merged into apache:maven-3.10.x as 
fd233f4c2794e08926a82d93fc5168b4738674d8
    ("Fixes #8438" milestone 3.10.2).
    
    Fixes #8438
    
    DefaultModelBuilder.readModel() reaches this branch only when the
    strict parse fails but a non-strict retry succeeds. The modello-
    generated reader's strict flag only gates schema-shape checks
    (unrecognized/misplaced elements, unknown attributes, namespace); raw
    XML well-formedness is enforced by the underlying parser regardless of
    that flag, so a document that is not well-formed XML at all fails both
    attempts and is reported separately as "Non-parseable POM". Whenever
    this branch runs, the document is therefore well-formed XML that
    merely violates the POM schema - invalid, not malformed.
    
    Change the message to say so, matching what the reporter (a committer
    on this project) found confusing: "the POM was well-formed as all
    tags balanced... invalid, not malformed, because the tags weren't
    where they should be."
    
    Backport applies cleanly onto apache:maven-3.9.x (1ae1806d08); the
    strict-parse retry logic and message wording are identical on both
    lines.
    
    Co-authored-by: Arturo Bernal <[email protected]>
---
 .../maven/model/building/DefaultModelBuilder.java  |  6 ++-
 .../model/building/DefaultModelBuilderTest.java    | 57 ++++++++++++++++++++++
 2 files changed, 61 insertions(+), 2 deletions(-)

diff --git 
a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
 
b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
index 59161899ee..7e787f09e4 100644
--- 
a/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
+++ 
b/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelBuilder.java
@@ -607,13 +607,15 @@ private Model readModel(
                     throw e;
                 }
 
+                // the document parsed successfully once unknown/misplaced 
elements were tolerated,
+                // so it is well-formed XML that merely violates the POM 
schema - invalid, not malformed
                 if (pomFile != null) {
                     problems.add(new 
ModelProblemCollectorRequest(Severity.ERROR, Version.V20)
-                            .setMessage("Malformed POM " + 
modelSource.getLocation() + ": " + e.getMessage())
+                            .setMessage("Invalid POM " + 
modelSource.getLocation() + ": " + e.getMessage())
                             .setException(e));
                 } else {
                     problems.add(new 
ModelProblemCollectorRequest(Severity.WARNING, Version.V20)
-                            .setMessage("Malformed POM " + 
modelSource.getLocation() + ": " + e.getMessage())
+                            .setMessage("Invalid POM " + 
modelSource.getLocation() + ": " + e.getMessage())
                             .setException(e));
                 }
             }
diff --git 
a/maven-model-builder/src/test/java/org/apache/maven/model/building/DefaultModelBuilderTest.java
 
b/maven-model-builder/src/test/java/org/apache/maven/model/building/DefaultModelBuilderTest.java
index 9af64e4b6e..02d4f49ed6 100644
--- 
a/maven-model-builder/src/test/java/org/apache/maven/model/building/DefaultModelBuilderTest.java
+++ 
b/maven-model-builder/src/test/java/org/apache/maven/model/building/DefaultModelBuilderTest.java
@@ -28,6 +28,7 @@
 
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /**
  * @author Guillaume Nodet
@@ -90,6 +91,62 @@ public void testCycleInImports() throws Exception {
         });
     }
 
+    /**
+     * MNG-6864: a well-formed POM that merely has an unrecognized/misplaced 
element is invalid,
+     * not malformed - the "Malformed POM" message is reserved for documents 
that are not
+     * well-formed XML at all.
+     */
+    @Test
+    public void testInvalidPomIsNotReportedAsMalformed() throws Exception {
+        String pom = "<project>\n" + "  <modelVersion>4.0.0</modelVersion>\n"
+                + "  <groupId>thegroup</groupId>\n"
+                + "  <artifactId>invalid</artifactId>\n"
+                + "  <version>1</version>\n"
+                + "  <dependencies>\n"
+                + "    <project>\n"
+                + "    </project>\n"
+                + "  </dependencies>\n"
+                + "</project>\n";
+
+        ModelBuilder builder = new DefaultModelBuilderFactory().newInstance();
+
+        DefaultModelBuildingRequest request = new 
DefaultModelBuildingRequest();
+        request.setModelSource(new StringModelSource(pom));
+
+        ModelBuildingResult result = builder.build(request);
+
+        assertTrue(
+                result.getProblems().stream().anyMatch(p -> 
p.getMessage().startsWith("Invalid POM")),
+                () -> "Expected an \"Invalid POM\" problem, got: " + 
result.getProblems());
+        assertTrue(
+                result.getProblems().stream().noneMatch(p -> 
p.getMessage().startsWith("Malformed POM")),
+                () -> "Did not expect a \"Malformed POM\" problem, got: " + 
result.getProblems());
+    }
+
+    /**
+     * Contrast case for {@link #testInvalidPomIsNotReportedAsMalformed()}: a 
document that is not
+     * well-formed XML at all must still fail outright, unaffected by MNG-6864.
+     */
+    @Test
+    public void testNotWellFormedPomFailsBuild() {
+        String pom = "<project>\n" + "  <modelVersion>4.0.0</modelVersion>\n"
+                + "  <groupId>thegroup</groupId>\n"
+                + "  <artifactId>malformed</artifactId>\n"
+                + "  <version>1</version>\n"
+                + "  <name>mismatched</foo>\n"
+                + "</project>\n";
+
+        ModelBuilder builder = new DefaultModelBuilderFactory().newInstance();
+
+        DefaultModelBuildingRequest request = new 
DefaultModelBuildingRequest();
+        request.setModelSource(new StringModelSource(pom));
+
+        ModelBuildingException e = assertThrows(ModelBuildingException.class, 
() -> builder.build(request));
+        assertTrue(
+                e.getProblems().stream().anyMatch(p -> 
p.getMessage().startsWith("Non-parseable POM")),
+                () -> "Expected a \"Non-parseable POM\" problem, got: " + 
e.getProblems());
+    }
+
     static class CycleInImportsResolver extends BaseModelResolver {
         @Override
         public ModelSource resolveModel(Dependency dependency) throws 
UnresolvableModelException {

Reply via email to