You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ss...@apache.org on 2015/11/09 11:32:41 UTC
svn commit: r1713371 - in /sling/trunk/bundles/commons/json/src:
main/java/org/apache/sling/commons/json/util/Validator.java
test/java/org/apache/sling/commons/json/util/ValidatorTest.java
Author: sseifert
Date: Mon Nov 9 10:32:41 2015
New Revision: 1713371
URL: http://svn.apache.org/viewvc?rev=1713371&view=rev
Log:
SLING-5276 validation fails on "[" json string
Modified:
sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/util/Validator.java
sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/util/ValidatorTest.java
Modified: sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/util/Validator.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/util/Validator.java?rev=1713371&r1=1713370&r2=1713371&view=diff
==============================================================================
--- sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/util/Validator.java (original)
+++ sling/trunk/bundles/commons/json/src/main/java/org/apache/sling/commons/json/util/Validator.java Mon Nov 9 10:32:41 2015
@@ -54,9 +54,13 @@ public class Validator {
// no tokens at all - we consider this valid
return;
} else if (c == '[') {
- if (x.nextClean() == ']') {
+ char nextChar = x.nextClean();
+ if (nextChar == ']') {
return;
}
+ else if (nextChar == 0) {
+ throw x.syntaxError("Detected unclosed array.");
+ }
x.back();
for (;;) {
if (x.nextClean() == ',') {
@@ -66,7 +70,7 @@ public class Validator {
c = x.nextClean();
x.back();
if ( c == '{' || c == '[') {
- // recursiv validation for object and array
+ // recursive validation for object and array
validate(x);
} else {
x.nextValue();
Modified: sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/util/ValidatorTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/util/ValidatorTest.java?rev=1713371&r1=1713370&r2=1713371&view=diff
==============================================================================
--- sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/util/ValidatorTest.java (original)
+++ sling/trunk/bundles/commons/json/src/test/java/org/apache/sling/commons/json/util/ValidatorTest.java Mon Nov 9 10:32:41 2015
@@ -17,7 +17,6 @@
package org.apache.sling.commons.json.util;
import org.apache.sling.commons.json.JSONException;
-import org.junit.Ignore;
import org.junit.Test;
/**
@@ -26,21 +25,37 @@ import org.junit.Test;
public class ValidatorTest {
@Test
- public void testSimpleJSON() throws JSONException {
+ public void testEmptyString() throws JSONException {
Validator.validate("");
+ }
+
+ @Test
+ public void testEmptyArray() throws JSONException {
Validator.validate("[]");
+ }
+
+ @Test
+ public void testEmptyObject() throws JSONException {
Validator.validate("{}");
}
@Test
- public void testBasicJSON() throws JSONException {
+ public void testSimpleArray() throws JSONException {
Validator.validate("[1,true,\"hallo\"]");
+ }
+
+ @Test
+ public void testSimpleObject() throws JSONException {
Validator.validate("{a:\"you\", b:2, c:true}");
}
@Test
- public void testNestedJSON() throws JSONException {
+ public void testNestedJSONArray() throws JSONException {
Validator.validate("[1,true,\"hallo\", {a:1}, [1,2]]");
+ }
+
+ @Test
+ public void testNestedJSONObject() throws JSONException {
Validator.validate("{a:\"you\", b:2, c:true, d: {d:1}, e: []}");
}
@@ -117,9 +132,23 @@ public class ValidatorTest {
}
@Test(expected=JSONException.class)
- @Ignore
- public void testSLING_5276() throws JSONException {
+ public void testOpeningBrackedOnlyArray() throws JSONException {
Validator.validate("[");
}
+ @Test(expected=JSONException.class)
+ public void testOpeningBrackedOnlyObject() throws JSONException {
+ Validator.validate("{");
+ }
+
+ @Test(expected=JSONException.class)
+ public void testUnclosedArray() throws JSONException {
+ Validator.validate("[1,true,\"hallo\"");
+ }
+
+ @Test(expected=JSONException.class)
+ public void testUnclosedObject() throws JSONException {
+ Validator.validate("{a:\"you\", b:2, c:true");
+ }
+
}