Skip to content

Commit a9c90e3

Browse files
authored
Support StreamWriteConstraints (#602)
1 parent fc66611 commit a9c90e3

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,11 @@ public JsonGenerator overrideFormatFeatures(int values, int mask)
321321
/**********************************************************
322322
*/
323323

324+
@Override
325+
public StreamWriteConstraints streamWriteConstraints() {
326+
return _ioContext.streamWriteConstraints();
327+
}
328+
324329
public ToXmlGenerator enable(Feature f) {
325330
_formatFeatures |= f.getMask();
326331
return this;
@@ -536,6 +541,7 @@ public final void writeStartArray() throws IOException
536541
{
537542
_verifyValueWrite("start an array");
538543
_writeContext = _writeContext.createChildArrayContext();
544+
streamWriteConstraints().validateNestingDepth(_writeContext.getNestingDepth());
539545
if (_cfgPrettyPrinter != null) {
540546
_cfgPrettyPrinter.writeStartArray(this);
541547
} else {
@@ -562,6 +568,7 @@ public final void writeStartObject() throws IOException
562568
{
563569
_verifyValueWrite("start an object");
564570
_writeContext = _writeContext.createChildObjectContext();
571+
streamWriteConstraints().validateNestingDepth(_writeContext.getNestingDepth());
565572
if (_cfgPrettyPrinter != null) {
566573
_cfgPrettyPrinter.writeStartObject(this);
567574
} else {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fasterxml.jackson.dataformat.xml.ser.dos;
2+
3+
import com.fasterxml.jackson.core.StreamWriteConstraints;
4+
import com.fasterxml.jackson.databind.JsonMappingException;
5+
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
6+
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
/**
12+
* Simple unit tests to verify that we fail gracefully if you attempt to serialize
13+
* data that is cyclic (eg a list that contains itself).
14+
*/
15+
public class CyclicDataSerTest extends XmlTestBase
16+
{
17+
private final XmlMapper MAPPER = newMapper();
18+
19+
public void testListWithSelfReference() throws Exception {
20+
List<Object> list = new ArrayList<>();
21+
list.add(list);
22+
try {
23+
MAPPER.writeValueAsString(list);
24+
fail("expected JsonMappingException");
25+
} catch (JsonMappingException jmex) {
26+
String exceptionPrefix = String.format("Document nesting depth (%d) exceeds the maximum allowed",
27+
StreamWriteConstraints.DEFAULT_MAX_DEPTH + 1);
28+
assertTrue("JsonMappingException message is as expected?",
29+
jmex.getMessage().startsWith(exceptionPrefix));
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)