Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support configureForJackson2() with XmlMapper #731

Merged
merged 6 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/tools/jackson/dataformat/xml/XmlFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,18 @@ public static XmlFactoryBuilder builder() {
return new XmlFactoryBuilder();
}

/**
* The builder returned uses default settings more closely
* matching the default configs used in Jackson 2.x versions.
* <p>
* This method is still a work in progress and may not yet fully replicate the
* default settings of Jackson 2.x.
* </p>
*/
public static XmlFactoryBuilder builderWithJackson2Defaults() {
return builder().configureForJackson2();
}

/**
* Note: compared to base implementation by {@link TokenStreamFactory},
* here the copy will actually share underlying XML input and
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/tools/jackson/dataformat/xml/XmlMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,24 @@ public Builder configure(XmlWriteFeature feature, boolean state)
return this;
}

/**
* The builder returned uses default settings more closely
* matching the default configs used in Jackson 2.x versions.
* <p>
* This method is still a work in progress and may not yet fully replicate the
* default settings of Jackson 2.x.
* </p>
*/
@Override
public Builder configureForJackson2() {
return super.configureForJackson2()
.disable(XmlWriteFeature.WRITE_NULLS_AS_XSI_NIL)
.disable(XmlWriteFeature.UNWRAP_ROOT_OBJECT_NODE)
.disable(XmlWriteFeature.AUTO_DETECT_XSI_TYPE)
.disable(XmlWriteFeature.WRITE_XML_SCHEMA_CONFORMING_FLOATS)
.disable(XmlReadFeature.AUTO_DETECT_XSI_TYPE);
}

/*
/******************************************************************
/* XML specific additional config
Expand Down Expand Up @@ -340,6 +358,19 @@ public static XmlMapper.Builder builder(XmlFactory streamFactory) {
return new XmlMapper.Builder(streamFactory);
}

/**
* The builder returned uses default settings more closely
* matching the default configs used in Jackson 2.x versions.
* <p>
* This method is still a work in progress and may not yet fully replicate the
* default settings of Jackson 2.x.
* </p>
*/
public static XmlMapper.Builder builderWithJackson2Defaults() {
return builder(XmlFactory.builderWithJackson2Defaults().build())
.configureForJackson2();
}

@SuppressWarnings("unchecked")
@Override
public XmlMapper.Builder rebuild() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import tools.jackson.core.io.NumberInput;
import tools.jackson.core.util.ByteArrayBuilder;
import tools.jackson.core.util.JacksonFeatureSet;
import tools.jackson.dataformat.xml.XmlWriteFeature;
import tools.jackson.dataformat.xml.util.CaseInsensitiveNameSet;
import tools.jackson.dataformat.xml.util.StaxUtil;

Expand Down Expand Up @@ -283,6 +284,10 @@ public void addVirtualWrapping(Set<String> namesToWrap0, boolean caseInsensitive
_streamReadContext.setNamesToWrap(namesToWrap);
}

public final boolean isEnabled(XmlReadFeature f) {
return (_formatFeatures & f.getMask()) != 0;
}

/*
/**********************************************************************
/* JsonParser impl, closing etc
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/tools/jackson/dataformat/xml/XmlMapperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package tools.jackson.dataformat.xml;

import org.junit.jupiter.api.Test;
import tools.jackson.core.StreamReadFeature;
import tools.jackson.dataformat.xml.deser.FromXmlParser;
import tools.jackson.dataformat.xml.ser.ToXmlGenerator;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertFalse;

public class XmlMapperTest extends XmlTestUtil
{

@Test
public void testBuilderWithJackson2Defaults() throws Exception
{
XmlMapper mapper = XmlMapper.builderWithJackson2Defaults().build();
assertFalse(mapper.isEnabled(StreamReadFeature.USE_FAST_DOUBLE_PARSER));
assertFalse(mapper.isEnabled(StreamReadFeature.USE_FAST_BIG_NUMBER_PARSER));

XMLOutputFactory outputFactory = mapper.tokenStreamFactory().getXMLOutputFactory();
try (
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ToXmlGenerator gen =
mapper.createGenerator(
outputFactory.createXMLStreamWriter(bos))
) {
assertFalse(gen.isEnabled(XmlWriteFeature.WRITE_NULLS_AS_XSI_NIL));
assertFalse(gen.isEnabled(XmlWriteFeature.UNWRAP_ROOT_OBJECT_NODE));
assertFalse(gen.isEnabled(XmlWriteFeature.AUTO_DETECT_XSI_TYPE));
assertFalse(gen.isEnabled(XmlWriteFeature.WRITE_XML_SCHEMA_CONFORMING_FLOATS));
// need to write something to the generator to avoid exception
final Point p = new Point(1, 2);
mapper.writeValue(gen, p);
}

final byte[] xml = "<root/>".getBytes(StandardCharsets.UTF_8);
XMLInputFactory inputFactory = mapper.tokenStreamFactory().getXMLInputFactory();
try (
ByteArrayInputStream bis = new ByteArrayInputStream(xml);
FromXmlParser parser =
mapper.createParser(
inputFactory.createXMLStreamReader(bis))
) {
assertFalse(parser.isEnabled(XmlReadFeature.AUTO_DETECT_XSI_TYPE));
}
}
}