This is an automated email from the ASF dual-hosted git repository. gnodet pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/master by this push: new f49b95e806 Fix #10939: DefaultModelXmlFactory: make location tracking opt-in—disabled by def… (#11092) (#11198) f49b95e806 is described below commit f49b95e806dd42ecfae5a2c1eed856ffc0ac4e94 Author: Guillaume Nodet <gno...@gmail.com> AuthorDate: Mon Oct 6 14:49:36 2025 +0200 Fix #10939: DefaultModelXmlFactory: make location tracking opt-in—disabled by def… (#11092) (#11198) DefaultModelXmlFactory: make location tracking opt-in—disabled by default, enabled when an InputLocationFormatter is provided. Adapt formatter to Function<InputLocation,String> and write to the actually opened stream for path output. Fixes #10939. (cherry picked from commit 333847658134d9914ad5c01908c83fb5df6be64d) Co-authored-by: Arturo Bernal <aber...@apache.org> --- .../apache/maven/impl/DefaultModelXmlFactory.java | 22 +++++--- .../maven/impl/DefaultModelXmlFactoryTest.java | 62 ++++++++++++++++++++++ 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultModelXmlFactory.java b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultModelXmlFactory.java index 7e860ec273..e9ac14d848 100644 --- a/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultModelXmlFactory.java +++ b/impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultModelXmlFactory.java @@ -39,6 +39,7 @@ import org.apache.maven.api.annotations.Nonnull; import org.apache.maven.api.di.Named; import org.apache.maven.api.di.Singleton; +import org.apache.maven.api.model.InputLocation; import org.apache.maven.api.model.InputSource; import org.apache.maven.api.model.Model; import org.apache.maven.api.services.xml.ModelXmlFactory; @@ -156,22 +157,29 @@ public void write(XmlWriterRequest<Model> request) throws XmlWriterException { Path path = request.getPath(); OutputStream outputStream = request.getOutputStream(); Writer writer = request.getWriter(); - Function<Object, String> inputLocationFormatter = request.getInputLocationFormatter(); + if (writer == null && outputStream == null && path == null) { throw new IllegalArgumentException("writer, outputStream or path must be non null"); } + try { - MavenStaxWriter w = new MavenStaxWriter(); - if (inputLocationFormatter != null) { - w.setStringFormatter((Function) inputLocationFormatter); + MavenStaxWriter xmlWriter = new MavenStaxWriter(); + xmlWriter.setAddLocationInformation(false); + + Function<Object, String> formatter = request.getInputLocationFormatter(); + if (formatter != null) { + xmlWriter.setAddLocationInformation(true); + Function<InputLocation, String> adapter = formatter::apply; + xmlWriter.setStringFormatter(adapter); } + if (writer != null) { - w.write(writer, content); + xmlWriter.write(writer, content); } else if (outputStream != null) { - w.write(outputStream, content); + xmlWriter.write(outputStream, content); } else { try (OutputStream os = Files.newOutputStream(path)) { - w.write(os, content); + xmlWriter.write(os, content); } } } catch (Exception e) { diff --git a/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultModelXmlFactoryTest.java b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultModelXmlFactoryTest.java index 436d7b9798..badb8612da 100644 --- a/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultModelXmlFactoryTest.java +++ b/impl/maven-impl/src/test/java/org/apache/maven/impl/DefaultModelXmlFactoryTest.java @@ -19,14 +19,18 @@ package org.apache.maven.impl; import java.io.StringReader; +import java.io.StringWriter; +import java.util.function.Function; import org.apache.maven.api.model.Model; import org.apache.maven.api.services.xml.XmlReaderException; import org.apache.maven.api.services.xml.XmlReaderRequest; +import org.apache.maven.api.services.xml.XmlWriterRequest; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -122,4 +126,62 @@ void testMalformedModelVersion() throws Exception { Model model = factory.read(request); assertEquals("invalid.version", model.getModelVersion()); } + + @Test + void testWriteWithoutFormatterDisablesLocationTracking() throws Exception { + // minimal valid model we can round-trip + String xml = + """ + <project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>g</groupId> + <artifactId>a</artifactId> + <version>1</version> + </project>"""; + + Model model = factory.read(XmlReaderRequest.builder() + .reader(new StringReader(xml)) + .strict(true) + .build()); + + StringWriter out = new StringWriter(); + factory.write(XmlWriterRequest.<Model>builder() + .writer(out) + .content(model) + // no formatter -> tracking should be OFF + .build()); + + String result = out.toString(); + assertFalse(result.contains("LOC_MARK"), "Unexpected marker found in output"); + } + + @Test + void testWriteWithFormatterEnablesLocationTracking() throws Exception { + String xml = + """ + <project xmlns="http://maven.apache.org/POM/4.0.0"> + <modelVersion>4.0.0</modelVersion> + <groupId>g</groupId> + <artifactId>a</artifactId> + <version>1</version> + </project>"""; + + Model model = factory.read(XmlReaderRequest.builder() + .reader(new StringReader(xml)) + .strict(true) + .build()); + + StringWriter out = new StringWriter(); + Function<Object, String> formatter = o -> "LOC_MARK"; + + factory.write(XmlWriterRequest.<Model>builder() + .writer(out) + .content(model) + .inputLocationFormatter(formatter) + .build()); + + String result = out.toString(); + // Presence of our formatter's output proves tracking was enabled and formatter applied + assertTrue(result.contains("LOC_MARK"), "Expected formatter marker in output when tracking is enabled"); + } }