Skip to content

Commit

Permalink
fix: set default maximum nesting depth as 512
Browse files Browse the repository at this point in the history
  • Loading branch information
cleydyr committed Feb 2, 2023
1 parent 651511f commit eb56704
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/main/java/org/json/XMLParserConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public class XMLParserConfiguration {
*/
public static final int UNDEFINED_MAXIMUM_NESTING_DEPTH = -1;

/**
* The default maximum nesting depth when parsing a XML document to JSON.
*/
public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = 512;

/** Original Configuration of the XML Parser. */
public static final XMLParserConfiguration ORIGINAL
= new XMLParserConfiguration();
Expand Down Expand Up @@ -64,7 +69,7 @@ public class XMLParserConfiguration {
* When parsing the XML into JSON, specifies the tags whose values should be converted
* to arrays
*/
private int maxNestingDepth = UNDEFINED_MAXIMUM_NESTING_DEPTH;
private int maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH;

/**
* Default parser configuration. Does not keep strings (tries to implicitly convert
Expand Down Expand Up @@ -321,8 +326,8 @@ public int getMaxNestingDepth() {

/**
* Defines the maximum nesting depth that the parser will descend before throwing an exception
* when parsing the XML into JSON. The default max nesting depth is undefined, which means the
* parser will go as deep as the maximum call stack size allows. Using any negative value as a
* when parsing the XML into JSON. The default max nesting depth is 512, which means the parser
* will go as deep as the maximum call stack size allows. Using any negative value as a
* parameter is equivalent to setting no limit to the nesting depth.
* @param maxNestingDepth the maximum nesting depth allowed to the XML parser
* @return The existing configuration will not be modified. A new configuration is returned.
Expand Down
1 change: 0 additions & 1 deletion src/test/java/org/json/junit/JSONArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/json/junit/XMLConfigurationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ public void testEmptyTagForceList() {
public void testMaxNestingDepthIsSet() {
XMLParserConfiguration xmlParserConfiguration = XMLParserConfiguration.ORIGINAL;

assertEquals(xmlParserConfiguration.getMaxNestingDepth(), XMLParserConfiguration.UNDEFINED_MAXIMUM_NESTING_DEPTH);
assertEquals(xmlParserConfiguration.getMaxNestingDepth(), XMLParserConfiguration.DEFAULT_MAXIMUM_NESTING_DEPTH);

xmlParserConfiguration = xmlParserConfiguration.withMaxNestingDepth(42);

Expand Down

0 comments on commit eb56704

Please sign in to comment.