Skip to content

Commit c610f18

Browse files
committed
JAVA-2363: Fix logic that verifies the contents of a string when reading JSON
1 parent 9a26062 commit c610f18

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

bson/src/main/org/bson/json/JsonReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ private void verifyString(final String expected) {
505505
JsonToken token = popToken();
506506
JsonTokenType type = token.getType();
507507

508-
if ((type != JsonTokenType.STRING && type != JsonTokenType.UNQUOTED_STRING) && !expected.equals(token.getValue())) {
508+
if ((type != JsonTokenType.STRING && type != JsonTokenType.UNQUOTED_STRING) || !expected.equals(token.getValue())) {
509509
throw new JsonParseException("JSON reader expected '%s' but found '%s'.", expected, token.getValue());
510510
}
511511
}

bson/src/test/unit/org/bson/json/JsonReaderTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,19 @@ public void testTimestampStrict() {
628628
assertEquals(AbstractBsonReader.State.DONE, bsonReader.getState());
629629
}
630630

631+
@Test
632+
public void testTimestampStrictWithOutOfOrderFields() {
633+
String json = "{ \"$timestamp\" : { \"i\" : 1, \"t\" : 1234 } }";
634+
bsonReader = new JsonReader(json);
635+
636+
try {
637+
bsonReader.readBsonType();
638+
fail("Should have failed to read timestamp with fields not in expected order");
639+
} catch (JsonParseException e) {
640+
// all good
641+
}
642+
}
643+
631644
@Test
632645
public void testTimestampShell() {
633646
String json = "Timestamp(1234, 1)";

0 commit comments

Comments
 (0)