gnodet commented on code in PR #11823:
URL: https://github.com/apache/maven/pull/11823#discussion_r2983575214
##########
impl/maven-xml/src/test/java/org/apache/maven/internal/xml/XmlNodeImplTest.java:
##########
@@ -715,6 +717,198 @@ public Object toInputLocation(XMLStreamReader parser) {
}
}
+ /**
+ * Verifies that when an XmlNode has xmlns:prefix declarations and prefixed
+ * attributes, the write side produces valid XML with proper namespace
handling.
+ */
+ @Test
+ void testWriteWithNamespaceDeclarationsAndPrefixedAttributes() throws
Exception {
+ String xml = """
+ <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:mvn="http://maven.apache.org/POM/4.0.0">
+ <compilerArgs mvn:combine.children="append">
+ <arg>-Xlint:deprecation</arg>
+ </compilerArgs>
+ </project>
+ """;
+
+ XmlNode node = toXmlNode(xml);
+
+ // The xmlns:mvn declaration should be preserved in the parsed node
+ assertEquals("http://maven.apache.org/POM/4.0.0",
node.attribute("xmlns:mvn"));
+
+ // Write and re-read to verify round-trip produces valid XML
+ java.io.StringWriter writer = new java.io.StringWriter();
+ XmlService.write(node, writer);
+ String output = writer.toString();
+
+ // The output should be parseable XML (no undeclared namespace prefix
errors)
+ XmlNode reRead = toXmlNode(output);
+ assertNotNull(reRead);
+ }
+
+ /**
+ * Verifies that when a prefixed attribute has no corresponding xmlns
declaration
+ * (as happens in consumer POM transformation), the prefix is stripped on
write
+ * to produce valid XML.
+ */
+ @Test
+ void testWriteStripsOrphanedPrefixOnAttributes() throws Exception {
+ // Simulate a consumer POM scenario: mvn:combine.children exists
+ // but xmlns:mvn was lost during model transformation
+ XmlNode node = XmlNode.newBuilder()
+ .name("compilerArgs")
+ .attributes(java.util.Map.of("mvn:combine.children", "append"))
+ .children(java.util.List.of(XmlNode.newBuilder()
+ .name("arg")
+ .value("-Xlint:deprecation")
+ .build()))
+ .build();
+
+ java.io.StringWriter writer = new java.io.StringWriter();
+ XmlService.write(node, writer);
+ String output = writer.toString();
+
+ // Should not contain the undeclared mvn: prefix
+ assertFalse(output.contains("mvn:combine"), "Output should not contain
orphaned mvn: prefix");
+ // Should contain the unprefixed attribute instead
+ assertTrue(output.contains("combine.children=\"append\""), "Attribute
should be written unprefixed");
+
+ // The output should be parseable XML
+ XmlNode reRead = toXmlNode(output);
+ assertNotNull(reRead);
+ assertEquals("append", reRead.attribute("combine.children"));
+ }
+
+ /**
+ * Verifies that foreign namespace prefixed attributes round-trip correctly
+ * when the xmlns declaration is on the same element as the prefixed
attribute.
+ */
+ @Test
+ void testWriteForeignNamespaceAttributeRoundTrip() throws Exception {
+ // Build a node where xmlns:custom and custom:myattr are on the same
element
+ XmlNode node = XmlNode.newBuilder()
+ .name("compilerArgs")
+ .attributes(java.util.Map.of(
+ "xmlns:custom", "http://example.com/custom",
+ "custom:myattr", "value"))
+ .children(java.util.List.of(XmlNode.newBuilder()
+ .name("arg")
+ .value("-Xlint:deprecation")
+ .build()))
+ .build();
+
+ java.io.StringWriter writer = new java.io.StringWriter();
+ XmlService.write(node, writer);
+ String output = writer.toString();
+
+ XmlNode reRead = toXmlNode(output);
+ assertNotNull(reRead);
+ assertEquals("value", reRead.attribute("custom:myattr"));
+ assertEquals("http://example.com/custom",
reRead.attribute("xmlns:custom"));
+ }
+
+ /**
+ * Verifies that when a prefixed attribute's xmlns declaration is on a
parent
Review Comment:
_Claude Code on behalf of Guillaume Nodet_
You were right — the prefix should not be removed when the namespace URI is
known. We've now addressed this properly in the latest commit by adding a
`namespaces()` method to `XmlNode` that captures the full namespace context
(prefix → URI) inherited from ancestor elements during parsing.
When writing, if a prefixed attribute like `mvn:combine.children` appears
but `xmlns:mvn` is not in the local attribute map, the writer now looks up the
prefix in the namespace context, auto-declares the namespace, and writes the
attribute with its proper prefix and namespace URI.
The prefix is only stripped as a last resort when no namespace context is
available at all (e.g., a programmatically built `XmlNode` with no namespace
bindings). This produces valid XML in all cases.
--
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]