You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2025/10/03 07:54:42 UTC
(maven) branch maven-4.0.x updated: Fix #10939: DefaultModelXmlFactory: make location tracking opt-in—disabled by def… (#11092)
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch maven-4.0.x
in repository https://gitbox.apache.org/repos/asf/maven.git
The following commit(s) were added to refs/heads/maven-4.0.x by this push:
new 3338476581 Fix #10939: DefaultModelXmlFactory: make location tracking opt-in—disabled by def… (#11092)
3338476581 is described below
commit 333847658134d9914ad5c01908c83fb5df6be64d
Author: Arturo Bernal <ab...@apache.org>
AuthorDate: Fri Oct 3 09:54:36 2025 +0200
Fix #10939: DefaultModelXmlFactory: make location tracking opt-in—disabled by def… (#11092)
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.
---
.../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 f447627cc7..e7a699e3b0 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
@@ -30,6 +30,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;
@@ -121,22 +122,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");
+ }
}