Skip to content

Commit 1493f8f

Browse files
authored
Change XmlWriteFeature defaults (#730)
1 parent 5b3c188 commit 1493f8f

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

release-notes/VERSION

+3
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ Version: 3.x (for earlier see VERSION-2.x)
1414
`ToXmlGenerator.Feature` as `XmlWriteFeature`
1515
#701: Change 3.0 to use `module-info.java` directly, remove use of Moditect
1616
#725: Change `XmlWriteFeature.UNWRAP_ROOT_OBJECT_NODE` default to `true`
17+
#727: Change `XmlWriteFeature.WRITE_NULLS_AS_XSI_NIL` default to `true`
18+
#728: Change `XmlWriteFeature.AUTO_DETECT_XSI_TYPE` default to `true`
19+
#729: Change `XmlWriteFeature.WRITE_XML_SCHEMA_CONFORMING_FLOATS` default to `true`
1720
- Add `XmlMapper.shared()`
1821
- Minimum Java baseline: Java 17

src/main/java/tools/jackson/dataformat/xml/XmlWriteFeature.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ public enum XmlWriteFeature implements FormatFeature
3636
* If enabled, `xsi:nil` attribute will be added to the empty element; if disabled,
3737
* it will not.
3838
*<p>
39-
* Feature is disabled by default for backwards compatibility.
39+
* Default setting is {@code true} (enabled) in Jackson 3.x:
40+
* it was {@code false} (disabled)in Jackson 2.x.
4041
*/
41-
WRITE_NULLS_AS_XSI_NIL(false),
42+
WRITE_NULLS_AS_XSI_NIL(true),
4243

4344
/**
4445
* Feature that determines writing of root values of type {@code ObjectNode}
@@ -51,8 +52,8 @@ public enum XmlWriteFeature implements FormatFeature
5152
* root element name is determined using normal logic (either explicitly
5253
* configured, or {@code ObjectNode} otherwise).
5354
*<p>
54-
* Default setting is {@code true} (enabled) in Jackson 3.0:
55-
* it was {@code false} in Jackson 2.x.
55+
* Default setting is {@code true} (enabled) in Jackson 3.x:
56+
* it was {@code false} (disabled)in Jackson 2.x.
5657
*/
5758
UNWRAP_ROOT_OBJECT_NODE(true),
5859

@@ -64,8 +65,11 @@ public enum XmlWriteFeature implements FormatFeature
6465
* and output is indicated to be done as XML Attribute.
6566
* This is mostly desirable for Polymorphic handling where it is difficult
6667
* to specify XML Namespace for type identifier
68+
*<p>
69+
* Default setting is {@code true} (enabled) in Jackson 3.0:
70+
* it was {@code false} (disabled)in Jackson 2.x.
6771
*/
68-
AUTO_DETECT_XSI_TYPE(false),
72+
AUTO_DETECT_XSI_TYPE(true),
6973

7074
/**
7175
* Feature that determines how floating-point infinity values are
@@ -92,9 +96,10 @@ public enum XmlWriteFeature implements FormatFeature
9296
* so there is no corresponding
9397
* {@link tools.jackson.dataformat.xml.XmlReadFeature}.
9498
*<p>
95-
* Feature is disabled by default for backwards compatibility.
99+
* Default setting is {@code true} (enabled) in Jackson 3.0:
100+
* it was {@code false} (disabled)in Jackson 2.x.
96101
*/
97-
WRITE_XML_SCHEMA_CONFORMING_FLOATS(false),
102+
WRITE_XML_SCHEMA_CONFORMING_FLOATS(true),
98103
;
99104

100105
private final boolean _defaultState;

src/main/java/tools/jackson/dataformat/xml/annotation/JacksonXmlRootElement.java

+3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
* {@link com.fasterxml.jackson.annotation.JsonRootName} instead.
2222
* About the only expected usage may be to have different root name for XML
2323
* content than other formats.
24+
*
25+
* @deprecated Since 2.4 use {@link com.fasterxml.jackson.annotation.JsonRootName} instead
2426
*/
2527
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE})
2628
@Retention(RetentionPolicy.RUNTIME)
29+
@Deprecated
2730
public @interface JacksonXmlRootElement
2831
{
2932
String namespace() default "";

src/test/java/tools/jackson/dataformat/xml/misc/RootNameTest.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package tools.jackson.dataformat.xml.misc;
22

3-
import java.io.IOException;
43
import java.util.ArrayList;
54
import java.util.Arrays;
65

@@ -10,15 +9,17 @@
109
import tools.jackson.databind.PropertyName;
1110

1211
import tools.jackson.dataformat.xml.XmlTestUtil;
12+
import tools.jackson.dataformat.xml.XmlWriteFeature;
1313
import tools.jackson.dataformat.xml.XmlMapper;
1414
import tools.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
1515

1616
import static org.junit.jupiter.api.Assertions.assertEquals;
1717
import static org.junit.jupiter.api.Assertions.fail;
1818

1919
// NOTE: even tho `@JacksonXmlRootElement` will be deprecated in near
20-
// future (possibly in 2.13) -- to be replaced by `@JsonRootName` -- this
20+
// future -- to be replaced by `@JsonRootName` -- this
2121
// test will use it to ensure we handle both annotations as expected
22+
@SuppressWarnings({ "serial" })
2223
public class RootNameTest extends XmlTestUtil
2324
{
2425
static class RootBeanBase
@@ -31,19 +32,21 @@ public RootBeanBase(String v) {
3132
}
3233
}
3334

35+
@SuppressWarnings("deprecation")
3436
@JacksonXmlRootElement(localName="root")
3537
static class RootBean extends RootBeanBase
3638
{
3739
protected RootBean() { super(); }
3840
}
3941

42+
@SuppressWarnings("deprecation")
4043
@JacksonXmlRootElement(localName="nsRoot", namespace="http://foo")
4144
static class NsRootBean
4245
{
4346
public String value = "abc";
4447
}
4548

46-
@SuppressWarnings("serial")
49+
@SuppressWarnings("deprecation")
4750
@JacksonXmlRootElement(localName="TheStrings")
4851
static class StringList extends ArrayList<String> {
4952
public StringList(String...strings) {
@@ -57,11 +60,13 @@ public StringList(String...strings) {
5760
/**********************************************************
5861
*/
5962

60-
protected XmlMapper _xmlMapper = new XmlMapper();
63+
protected XmlMapper _xmlMapper = mapperBuilder()
64+
.disable(XmlWriteFeature.WRITE_NULLS_AS_XSI_NIL)
65+
.build();
6166

6267
// Unit test to verify that root name is properly set
6368
@Test
64-
public void testRootNameAnnotation() throws IOException
69+
public void testRootNameAnnotation()
6570
{
6671
String xml = _xmlMapper.writeValueAsString(new StringBean());
6772

@@ -87,7 +92,7 @@ public void testRootNameAnnotation() throws IOException
8792
}
8893

8994
@Test
90-
public void testDynamicRootName() throws IOException
95+
public void testDynamicRootName()
9196
{
9297
String xml;
9398

@@ -105,7 +110,7 @@ public void testDynamicRootName() throws IOException
105110
}
106111

107112
@Test
108-
public void testDynamicRootNameForList() throws IOException
113+
public void testDynamicRootNameForList()
109114
{
110115
String xml;
111116

src/test/java/tools/jackson/dataformat/xml/stream/XmlGeneratorTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88

99
import tools.jackson.dataformat.xml.XmlMapper;
1010
import tools.jackson.dataformat.xml.XmlTestUtil;
11+
import tools.jackson.dataformat.xml.XmlWriteFeature;
1112
import tools.jackson.dataformat.xml.ser.ToXmlGenerator;
1213

1314
import static org.junit.jupiter.api.Assertions.assertEquals;
1415

1516
public class XmlGeneratorTest extends XmlTestUtil
1617
{
17-
private final XmlMapper MAPPER = xmlMapper(true);
18+
private final XmlMapper MAPPER = mapperBuilder(true)
19+
.disable(XmlWriteFeature.WRITE_NULLS_AS_XSI_NIL)
20+
.build();
1821

1922
@Test
2023
public void testSimpleElement() throws Exception

src/test/java/tools/jackson/dataformat/xml/stream/XmlParserTest.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
public class XmlParserTest extends XmlTestUtil
1818
{
1919
protected final ObjectMapper _jsonMapper = new JsonMapper();
20-
protected final XmlMapper _xmlMapper = newMapper();
20+
protected final XmlMapper _xmlMapper = mapperBuilder()
21+
// Test written for 2.x which does not unwrap nodes so
22+
.disable(XmlWriteFeature.UNWRAP_ROOT_OBJECT_NODE)
23+
.build();
2124

2225
/*
2326
/**********************************************************************

0 commit comments

Comments
 (0)