gnodet-bot commented on code in PR #14032:
URL: https://github.com/apache/maven/pull/14032#discussion_r4594820286
##########
impl/maven-xml/src/main/java/org/apache/maven/internal/xml/DefaultXmlService.java:
##########
@@ -348,79 +372,81 @@ public XmlNode doMerge(XmlNode dominant, XmlNode
recessive, Boolean childMergeOv
.filter(n -> n.name().equals(name))
.toList();
if (!dominantChildren.isEmpty()) {
- commonChildren.put(name, dominantChildren.iterator());
+ commonChildren.put(
+ name,
+ dominantChildren.stream()
+ .filter(n ->
!matchedChildren.contains(n))
+ .iterator());
}
}
- String keysValue =
recessive.attribute(KEYS_COMBINATION_MODE_ATTRIBUTE);
+ // the dominant children already merged with a recessive
child, and the result of that merge
+ Map<XmlNode, XmlNode> mergedChildren = new IdentityHashMap<>();
int recessiveChildIndex = 0;
for (XmlNode recessiveChild : recessive.children()) {
String idValue =
recessiveChild.attribute(ID_COMBINATION_MODE_ATTRIBUTE);
- XmlNode childDom = null;
- if (!isEmpty(idValue)) {
- for (XmlNode dominantChild : dominant.children()) {
- if
(idValue.equals(dominantChild.attribute(ID_COMBINATION_MODE_ATTRIBUTE))) {
- childDom = dominantChild;
- // we have a match, so don't append but merge
- mergeChildren = true;
- }
- }
- } else if (!isEmpty(keysValue)) {
- String[] keys = keysValue.split(",");
- Map<String, Optional<String>> recessiveKeyValues =
Stream.of(keys)
- .collect(Collectors.toMap(
- k -> k, k ->
Optional.ofNullable(recessiveChild.attribute(k))));
-
- for (XmlNode dominantChild : dominant.children()) {
- Map<String, Optional<String>> dominantKeyValues =
Stream.of(keys)
- .collect(Collectors.toMap(
- k -> k, k ->
Optional.ofNullable(dominantChild.attribute(k))));
-
- if (recessiveKeyValues.equals(dominantKeyValues)) {
- childDom = dominantChild;
- // we have a match, so don't append but merge
- mergeChildren = true;
- }
+ XmlNode childDom;
+ boolean matched = false;
+ if (!isEmpty(idValue) || keys != null) {
+ childDom = matches.get(recessiveChild);
+ if (childDom != null) {
+ matched = true;
+ // we have a match, so don't append but merge
+ mergeChildren = true;
}
} else {
childDom = dominant.child(recessiveChild.name());
}
if (mergeChildren && childDom != null) {
- String name = recessiveChild.name();
- Iterator<XmlNode> it = commonChildren.computeIfAbsent(
- name,
- n1 -> Stream.of(dominant.children().stream()
- .filter(n2 ->
n2.name().equals(n1))
- .collect(Collectors.toList()))
- .filter(l -> !l.isEmpty())
- .findFirst()
- .map(List::iterator)
- .orElse(null));
- if (it == null) {
- if (children == null) {
- children = new
ArrayList<>(dominant.children());
+ XmlNode dominantChild = null;
+ if (matched) {
+ // merge with the matching child, wherever it is
among its siblings
+ dominantChild = childDom;
+ } else {
+ String name = recessiveChild.name();
+ Iterator<XmlNode> it =
commonChildren.computeIfAbsent(
+ name,
+ n1 ->
Stream.of(dominant.children().stream()
+ .filter(n2 ->
n2.name().equals(n1))
+
.collect(Collectors.toList()))
+ .filter(l -> !l.isEmpty())
+ .findFirst()
+ .map(List::iterator)
+ .orElse(null));
+ if (it == null) {
+ if (children == null) {
+ children = new
ArrayList<>(dominant.children());
+ }
+ children.add(recessiveChild);
+ } else if (it.hasNext()) {
+ dominantChild = it.next();
}
- children.add(recessiveChild);
- } else if (it.hasNext()) {
- XmlNode dominantChild = it.next();
+ }
+ if (dominantChild != null) {
+ // several recessive children can match the same
dominant child
+ XmlNode current =
mergedChildren.getOrDefault(dominantChild, dominantChild);
- String dominantChildCombinationMode =
getSelfCombinationMode(dominantChild);
+ String dominantChildCombinationMode =
getSelfCombinationMode(current);
if
(SELF_COMBINATION_REMOVE.equals(dominantChildCombinationMode)) {
if (children == null) {
children = new
ArrayList<>(dominant.children());
}
- children.remove(dominantChild);
+ int idx = indexOfSame(children, current);
+ if (idx >= 0) {
+ children.remove(idx);
+ }
} else {
- int idx =
dominant.children().indexOf(dominantChild);
- XmlNode merged = merge(dominantChild,
recessiveChild, childMergeOverride);
- if (merged != dominantChild) {
+ XmlNode merged = merge(current,
recessiveChild, childMergeOverride);
+ if (merged != current) {
if (children == null) {
children = new
ArrayList<>(dominant.children());
}
- children.set(idx, merged);
+ // recessive children may have been added
before the dominant child
+ children.set(indexOfSame(children,
current), merged);
+ mergedChildren.put(dominantChild, merged);
Review Comment:
The `REMOVE` branch guards the `indexOfSame` return value with `if (idx >=
0)`, but this merge branch does not. If `current` is not found in `children` —
which can happen when a first recessive child triggered
`SELF_COMBINATION_REMOVE` on this dominant node and a second recessive child
(also matching the same dominant) reaches the merge path — `indexOfSame`
returns `-1` and `children.set(-1, merged)` throws `IndexOutOfBoundsException`.
Defensive fix — mirror the pattern from the `REMOVE` branch:
```suggestion
int mergeIdx = indexOfSame(children,
current);
if (mergeIdx >= 0) {
children.set(mergeIdx, merged);
mergedChildren.put(dominantChild,
merged);
}
```
##########
impl/maven-xml/src/test/java/org/apache/maven/internal/xml/XmlNodeImplTest.java:
##########
@@ -284,6 +284,109 @@ void testCombineKeys() throws Exception {
assertEquals("right", p2.child("value").inputLocation());
}
+ @Test
+ void testCombineIdMergesMatchingElement() throws Exception {
+ String lhs = "<props>" + "<property
combine.id='A'><name>A</name></property>"
+ + "<property combine.id='B'><name>B</name></property>" +
"</props>";
+
+ String rhs = "<props>" + "<property
combine.id='B'><value>RHS</value></property>" + "</props>";
+
+ assertRecessiveValueIsMergedIntoSecondProperty(lhs, rhs);
+ }
+
+ @Test
+ void testCombineKeysMergesMatchingElement() throws Exception {
+ String lhs = "<props combine.keys='key'>" + "<property
key='A'><name>A</name></property>"
+ + "<property key='B'><name>B</name></property>" + "</props>";
+
+ String rhs = "<props combine.keys='key'>" + "<property
key='B'><value>RHS</value></property>" + "</props>";
+
+ assertRecessiveValueIsMergedIntoSecondProperty(lhs, rhs);
+ }
+
+ @Test
+ void testCombineIdMergesMatchingElementAfterAppendedSibling() throws
Exception {
+ String lhs =
+ "<props combine.children='append'>" + "<property
combine.id='A'><name>A</name></property>" + "</props>";
+
+ String rhs = "<props>" + "<property
combine.id='X'><name>X</name></property>"
+ + "<property combine.id='A'><value>RHS</value></property>" +
"</props>";
+
+ XmlNode leftDom = XmlService.read(new StringReader(lhs), new
FixedInputLocationBuilder("left"));
+ XmlNode rightDom = XmlService.read(new StringReader(rhs), new
FixedInputLocationBuilder("right"));
+
+ XmlNode mergeResult = XmlService.merge(leftDom, rightDom);
+ assertEquals(2, getChildren(mergeResult, "property").size());
+
+ XmlNode p0 = getNthChild(mergeResult, "property", 0);
+ assertEquals("X", p0.child("name").value());
+ assertEquals("right", p0.child("name").inputLocation());
+
+ XmlNode p1 = getNthChild(mergeResult, "property", 1);
+ assertEquals("A", p1.child("name").value());
+ assertEquals("RHS", p1.child("value").value());
+ }
+
+ @Test
+ void testCombineIdLeavesOtherElementsMergedInOrder() throws Exception {
+ String lhs = "<props>" + "<property
combine.id='A'><name>A</name></property>"
+ + "<property><name>B</name></property>" + "</props>";
+
+ String rhs = "<props>" + "<property
combine.id='A'><other>RHS-A</other></property>"
+ + "<property><value>RHS</value></property>" + "</props>";
+
+ XmlNode leftDom = XmlService.read(new StringReader(lhs), new
FixedInputLocationBuilder("left"));
+ XmlNode rightDom = XmlService.read(new StringReader(rhs), new
FixedInputLocationBuilder("right"));
+
+ XmlNode mergeResult = XmlService.merge(leftDom, rightDom, true);
+ assertEquals(2, getChildren(mergeResult, "property").size());
+
+ XmlNode p0 = getNthChild(mergeResult, "property", 0);
+ assertEquals("A", p0.child("name").value());
+ assertEquals("RHS-A", p0.child("other").value());
+ assertNull(p0.child("value"));
+
+ XmlNode p1 = getNthChild(mergeResult, "property", 1);
+ assertEquals("B", p1.child("name").value());
+ assertEquals("RHS", p1.child("value").value());
+ }
+
+ @Test
+ void testCombineIdMergesEveryElementWithTheSameId() throws Exception {
+ String lhs = "<props>" + "<property
combine.id='A'><name>A</name></property>" + "</props>";
+
+ String rhs = "<props>" + "<property
combine.id='A'><value>RHS</value></property>"
+ + "<property combine.id='A'><other>RHS</other></property>" +
"</props>";
+
+ XmlNode leftDom = XmlService.read(new StringReader(lhs), new
FixedInputLocationBuilder("left"));
+ XmlNode rightDom = XmlService.read(new StringReader(rhs), new
FixedInputLocationBuilder("right"));
+
+ XmlNode mergeResult = XmlService.merge(leftDom, rightDom, true);
+ assertEquals(1, getChildren(mergeResult, "property").size());
+
+ XmlNode p0 = getNthChild(mergeResult, "property", 0);
+ assertEquals("A", p0.child("name").value());
+ assertEquals("RHS", p0.child("value").value());
+ assertEquals("RHS", p0.child("other").value());
+ }
+
+ private static void assertRecessiveValueIsMergedIntoSecondProperty(String
lhs, String rhs) throws Exception {
+ XmlNode leftDom = XmlService.read(new StringReader(lhs), new
FixedInputLocationBuilder("left"));
+ XmlNode rightDom = XmlService.read(new StringReader(rhs), new
FixedInputLocationBuilder("right"));
+
+ XmlNode mergeResult = XmlService.merge(leftDom, rightDom, true);
+ assertEquals(2, getChildren(mergeResult, "property").size());
+
+ XmlNode p0 = getNthChild(mergeResult, "property", 0);
+ assertEquals("A", p0.child("name").value());
+ assertNull(p0.child("value"));
+
+ XmlNode p1 = getNthChild(mergeResult, "property", 1);
+ assertEquals("B", p1.child("name").value());
+ assertEquals("RHS", p1.child("value").value());
+ assertEquals("right", p1.child("value").inputLocation());
+ }
Review Comment:
The five new tests cover the main scenarios, but there is no test for a
recessive child with `combine.id='NEW'` where no dominant child has that id —
verifying it gets appended rather than silently dropped. The code handles it
correctly (falls through to the append path), but an explicit test would
prevent regression.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]