forked from FasterXML/jackson-databind
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Throw JsonMappingException for deeply nested JSON (FasterXML#2816, CV…
- Loading branch information
1 parent
0ede935
commit fcfc499
Showing
2 changed files
with
133 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/test/java/com/fasterxml/jackson/databind/deser/DeepNestingUntypedDeserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.fasterxml.jackson.databind.deser; | ||
|
||
import com.fasterxml.jackson.core.JsonParseException; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class DeepNestingUntypedDeserTest extends BaseMapTest | ||
{ | ||
// 28-Mar-2021, tatu: Currently 3000 fails for untyped/Object, | ||
// 4000 for untyped/Array | ||
private final static int TOO_DEEP_NESTING = 4000; | ||
private final static int NOT_TOO_DEEP = 1000; | ||
|
||
private final ObjectMapper MAPPER = newJsonMapper(); | ||
|
||
public void testTooDeepUntypedWithArray() throws Exception | ||
{ | ||
final String doc = _nestedDoc(TOO_DEEP_NESTING, "[ ", "] "); | ||
try { | ||
MAPPER.readValue(doc, Object.class); | ||
fail("Should have thrown an exception."); | ||
} catch (JsonParseException jpe) { | ||
assertTrue(jpe.getMessage().startsWith("JSON is too deeply nested.")); | ||
} | ||
} | ||
|
||
public void testUntypedWithArray() throws Exception | ||
{ | ||
final String doc = _nestedDoc(NOT_TOO_DEEP, "[ ", "] "); | ||
Object ob = MAPPER.readValue(doc, Object.class); | ||
assertTrue(ob instanceof List<?>); | ||
} | ||
|
||
public void testTooDeepUntypedWithObject() throws Exception | ||
{ | ||
final String doc = "{"+_nestedDoc(TOO_DEEP_NESTING, "\"x\":{", "} ") + "}"; | ||
try { | ||
MAPPER.readValue(doc, Object.class); | ||
fail("Should have thrown an exception."); | ||
} catch (JsonParseException jpe) { | ||
assertTrue(jpe.getMessage().startsWith("JSON is too deeply nested.")); | ||
} | ||
} | ||
|
||
public void testUntypedWithObject() throws Exception | ||
{ | ||
final String doc = "{"+_nestedDoc(NOT_TOO_DEEP, "\"x\":{", "} ") + "}"; | ||
Object ob = MAPPER.readValue(doc, Object.class); | ||
assertTrue(ob instanceof Map<?, ?>); | ||
} | ||
|
||
private String _nestedDoc(int nesting, String open, String close) { | ||
StringBuilder sb = new StringBuilder(nesting * (open.length() + close.length())); | ||
for (int i = 0; i < nesting; ++i) { | ||
sb.append(open); | ||
if ((i & 31) == 0) { | ||
sb.append("\n"); | ||
} | ||
} | ||
for (int i = 0; i < nesting; ++i) { | ||
sb.append(close); | ||
if ((i & 31) == 0) { | ||
sb.append("\n"); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
} |