Skip to content

Commit

Permalink
add cyclic data serialization tests (#4016)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning authored Jul 10, 2023
1 parent 2939cb8 commit a4a3eae
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class CyclicTypeSerTest
extends BaseMapTest
{
static class Bean
public static class Bean
{
Bean _next;
final String _name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.fasterxml.jackson.databind.ser.dos;

import com.fasterxml.jackson.core.StreamWriteConstraints;
import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.InvalidDefinitionException;
import com.fasterxml.jackson.databind.ser.CyclicTypeSerTest;

import java.util.ArrayList;
import java.util.List;

/**
* Simple unit tests to verify that we fail gracefully if you attempt to serialize
* data that is cyclic (eg a list that contains itself).
*/
public class CyclicDataSerTest
extends BaseMapTest
{
private final ObjectMapper MAPPER = newJsonMapper();

public void testLinkedAndCyclic() throws Exception {
CyclicTypeSerTest.Bean bean = new CyclicTypeSerTest.Bean(null, "last");
bean.assignNext(bean);
try {
writeAndMap(MAPPER, bean);
fail("expected InvalidDefinitionException");
} catch (InvalidDefinitionException idex) {
assertTrue("InvalidDefinitionException message is as expected?",
idex.getMessage().startsWith("Direct self-reference leading to cycle"));
}
}

public void testListWithSelfReference() throws Exception {
List<Object> list = new ArrayList<>();
list.add(list);
try {
writeAndMap(MAPPER, list);
fail("expected JsonMappingException");
} catch (JsonMappingException jmex) {
String exceptionPrefix = String.format("Document nesting depth (%d) exceeds the maximum allowed",
StreamWriteConstraints.DEFAULT_MAX_DEPTH + 1);
assertTrue("JsonMappingException message is as expected?",
jmex.getMessage().startsWith(exceptionPrefix));
}
}
}

0 comments on commit a4a3eae

Please sign in to comment.