Visitar URL original
Fix NPE on a recursive property reference in a list field (#13145) · apache/maven@dbe961c · GitHub
Skip to content

Commit dbe961c

Browse files
gnodetrenechoi
andauthored
Fix NPE on a recursive property reference in a list field (#13145)
DefaultModelInterpolator records an ERROR and returns null when the interpolator throws on a recursive variable reference. The generated transformer stores that null in List<String> model fields such as build/filters, whose Properties sibling branch already guards against it, and the immutable model constructor then copies the list with List.copyOf. The build fails with a bare NullPointerException instead of the "recursive variable reference" error that was just collected. Return the original text instead, which is what the Properties branch and StringVisitorModelInterpolator (MNG-8174) already do. The returned instance is identical, so the transformer's newVal != oldVal check stays false and unaffected models are still returned unchanged. Co-authored-by: renechoi <115696395+renechoi@users.noreply.github.com>
1 parent 3f0ed3f commit dbe961c

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelInterpolator.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ private InnerInterpolator createInterpolator(
139139
return di.interpolate(value, null, cycleMap, cb, postprocessor, false);
140140
} catch (InterpolatorException e) {
141141
problems.add(BuilderProblem.Severity.ERROR, ModelProblem.Version.BASE, e.getMessage(), e);
142-
return null;
142+
// Keep the original text rather than handing null back to the transformer. A null
143+
// ends up as an element of a List<String> model field such as build/filters, and
144+
// the immutable model constructor copies that list with List.copyOf, which fails
145+
// with a bare NullPointerException that hides the problem just collected above.
146+
return value;
143147
}
144148
};
145149
}

‎impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelInterpolatorTest.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,26 @@ public void testRecursiveExpressionCycleNPE() throws Exception {
632632
assertTrue(collector.getErrors().get(0).contains("recursive variable reference"));
633633
}
634634

635+
@Test
636+
public void testRecursiveExpressionCycleInListField() throws Exception {
637+
Map<String, String> props = new HashMap<>();
638+
props.put("aa", "${bb}");
639+
props.put("bb", "${aa}");
640+
641+
Model model = Model.newBuilder()
642+
.properties(props)
643+
.build(Build.newBuilder().filters(List.of("${aa}")).build())
644+
.build();
645+
646+
SimpleProblemCollector collector = new SimpleProblemCollector();
647+
648+
ModelBuilderRequest request = createModelBuildingRequest(Map.of()).build();
649+
Model out = interpolator.interpolateModel(model, null, request, collector);
650+
651+
assertEquals(List.of("${aa}"), out.getBuild().getFilters());
652+
assertTrue(collector.getErrors().get(0).contains("recursive variable reference"));
653+
}
654+
635655
@Disabled("per def cannot be recursive: ${basedir} is immediately going for project.basedir")
636656
@Test
637657
public void testRecursiveExpressionCycleBaseDir() throws Exception {

0 commit comments

Comments
 (0)