From 5ddb8c3d351c155250891968dfc4a1e8442ae14f Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 30 Dec 2023 16:30:19 -0600 Subject: [PATCH 1/2] cleanup-and-merge-tests: fix warnings, set gradlew permissions, enable unchecked warnings in maven --- gradlew | 0 pom.xml | 3 +++ src/main/java/org/json/JSONArray.java | 5 +++-- src/main/java/org/json/JSONMLParserConfiguration.java | 2 ++ src/main/java/org/json/JSONParserConfiguration.java | 1 + src/main/java/org/json/ParserConfiguration.java | 2 ++ src/main/java/org/json/XMLParserConfiguration.java | 4 +++- src/test/java/org/json/junit/data/WeirdList.java | 6 +++--- 8 files changed, 17 insertions(+), 6 deletions(-) mode change 100644 => 100755 gradlew diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/pom.xml b/pom.xml index 927a989..d01283c 100644 --- a/pom.xml +++ b/pom.xml @@ -104,6 +104,9 @@ 1.8 1.8 + + -Xlint:unchecked + diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 50146a8..38b0b31 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -1359,7 +1359,8 @@ public class JSONArray implements Iterable { * The subscript. * @param value * The Map value. - * @return this. + * @return + * reference to self * @throws JSONException * If the index is negative or if the value is an invalid * number. @@ -1381,7 +1382,7 @@ public class JSONArray implements Iterable { * The Map value. * @param jsonParserConfiguration * Configuration object for the JSON parser - * @return + * @return reference to self * @throws JSONException * If the index is negative or if the value is an invalid * number. diff --git a/src/main/java/org/json/JSONMLParserConfiguration.java b/src/main/java/org/json/JSONMLParserConfiguration.java index b2514ab..43ba0db 100644 --- a/src/main/java/org/json/JSONMLParserConfiguration.java +++ b/src/main/java/org/json/JSONMLParserConfiguration.java @@ -55,11 +55,13 @@ public class JSONMLParserConfiguration extends ParserConfiguration { ); } + @SuppressWarnings("unchecked") @Override public JSONMLParserConfiguration withKeepStrings(final boolean newVal) { return super.withKeepStrings(newVal); } + @SuppressWarnings("unchecked") @Override public JSONMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) { return super.withMaxNestingDepth(maxNestingDepth); diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index 1ec1710..f95e244 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -17,6 +17,7 @@ public class JSONParserConfiguration extends ParserConfiguration { return new JSONParserConfiguration(); } + @SuppressWarnings("unchecked") @Override public JSONParserConfiguration withMaxNestingDepth(final int maxNestingDepth) { return super.withMaxNestingDepth(maxNestingDepth); diff --git a/src/main/java/org/json/ParserConfiguration.java b/src/main/java/org/json/ParserConfiguration.java index 36fa508..ede2fc5 100644 --- a/src/main/java/org/json/ParserConfiguration.java +++ b/src/main/java/org/json/ParserConfiguration.java @@ -75,6 +75,7 @@ public class ParserConfiguration { * * @return The existing configuration will not be modified. A new configuration is returned. */ + @SuppressWarnings("unchecked") public T withKeepStrings(final boolean newVal) { T newConfig = (T)this.clone(); newConfig.keepStrings = newVal; @@ -101,6 +102,7 @@ public class ParserConfiguration { * * @return The existing configuration will not be modified. A new configuration is returned. */ + @SuppressWarnings("unchecked") public T withMaxNestingDepth(int maxNestingDepth) { T newConfig = (T)this.clone(); diff --git a/src/main/java/org/json/XMLParserConfiguration.java b/src/main/java/org/json/XMLParserConfiguration.java index 2e4907f..0ac7834 100644 --- a/src/main/java/org/json/XMLParserConfiguration.java +++ b/src/main/java/org/json/XMLParserConfiguration.java @@ -192,6 +192,7 @@ public class XMLParserConfiguration extends ParserConfiguration { * * @return The existing configuration will not be modified. A new configuration is returned. */ + @SuppressWarnings("unchecked") @Override public XMLParserConfiguration withKeepStrings(final boolean newVal) { return super.withKeepStrings(newVal); @@ -309,6 +310,7 @@ public class XMLParserConfiguration extends ParserConfiguration { * @param maxNestingDepth the maximum nesting depth allowed to the XML parser * @return The existing configuration will not be modified. A new configuration is returned. */ + @SuppressWarnings("unchecked") @Override public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) { return super.withMaxNestingDepth(maxNestingDepth); @@ -316,7 +318,7 @@ public class XMLParserConfiguration extends ParserConfiguration { /** * To enable explicit end tag with empty value. - * @param closeEmptyTag + * @param closeEmptyTag new value for the closeEmptyTag property * @return same instance of configuration with empty tag config updated */ public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){ diff --git a/src/test/java/org/json/junit/data/WeirdList.java b/src/test/java/org/json/junit/data/WeirdList.java index 834b81e..3560586 100644 --- a/src/test/java/org/json/junit/data/WeirdList.java +++ b/src/test/java/org/json/junit/data/WeirdList.java @@ -12,7 +12,7 @@ import java.util.List; */ public class WeirdList { /** */ - private final List list = new ArrayList(); + private final List list = new ArrayList<>(); /** * @param vals @@ -25,14 +25,14 @@ public class WeirdList { * @return a copy of the list */ public List get() { - return new ArrayList(this.list); + return new ArrayList<>(this.list); } /** * @return a copy of the list */ public List getALL() { - return new ArrayList(this.list); + return new ArrayList<>(this.list); } /** From 86bb0a1a02d49d2d1799153a45f7b8bcf2ed7015 Mon Sep 17 00:00:00 2001 From: Sean Leary Date: Sat, 30 Dec 2023 17:00:02 -0600 Subject: [PATCH 2/2] cleanup-and-merge-tests: pull in unit tests from #809 --- .../java/org/json/junit/JSONObjectTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 96f3673..053f17a 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -3760,6 +3760,48 @@ public class JSONObjectTest { String jsonString = object.toString(); } + @Test(expected = JSONException.class) + public void testCircleReferenceFirstLevel() { + Map jsonObject = new HashMap<>(); + + jsonObject.put("test", jsonObject); + + new JSONObject(jsonObject, new JSONParserConfiguration()); + } + + @Test(expected = StackOverflowError.class) + public void testCircleReferenceMultiplyLevel_notConfigured_expectedStackOverflow() { + Map inside = new HashMap<>(); + + Map jsonObject = new HashMap<>(); + inside.put("test", jsonObject); + jsonObject.put("test", inside); + + new JSONObject(jsonObject, new JSONParserConfiguration().withMaxNestingDepth(99999)); + } + + @Test(expected = JSONException.class) + public void testCircleReferenceMultiplyLevel_configured_expectedJSONException() { + Map inside = new HashMap<>(); + + Map jsonObject = new HashMap<>(); + inside.put("test", jsonObject); + jsonObject.put("test", inside); + + new JSONObject(jsonObject, new JSONParserConfiguration()); + } + + @Test + public void testDifferentKeySameInstanceNotACircleReference() { + Map map1 = new HashMap<>(); + Map map2 = new HashMap<>(); + + map1.put("test1", map2); + map1.put("test2", map2); + + new JSONObject(map1); + } + /** * Method to build nested map of max maxDepth *