Skip to content

Commit

Permalink
Merge pull request #129 from devinrsmith/fix-current-token-after-null
Browse files Browse the repository at this point in the history
Ensures that _currToken is set to null if it has been returned from nextToken()
  • Loading branch information
michel-kraemer authored Feb 16, 2024
2 parents d8bc2f4 + 7f5c4ff commit 2c1a3b0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/de/undercouch/bson4jackson/BsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public JsonToken nextToken() throws IOException {
if (ctx == null) {
if (_currToken == JsonToken.END_OBJECT) {
// end of input
return null;
return (_currToken = null);
}
throw new JsonParseException("Found element outside the document",
getTokenLocation());
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/de/undercouch/bson4jackson/BsonParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -691,4 +691,22 @@ public void parseWrappedFloat() throws Exception {

assertEquals(clone.inner.floatValue, outer.inner.floatValue, 0);
}

/**
* Parse empty object, specifically checking getCurrentToken() along the way. See issue #128
* @throws Exception if something goes wrong
*/
@Test
public void parseEmptyObject() throws Exception {
byte[] emptyBsonBytes = new BasicBSONEncoder().encode(new BasicBSONObject());
try (BsonParser dec = new BsonFactory().createJsonParser(emptyBsonBytes)) {
assertNull(dec.getCurrentToken());
assertEquals(JsonToken.START_OBJECT, dec.nextToken());
assertEquals(JsonToken.START_OBJECT, dec.getCurrentToken());
assertEquals(JsonToken.END_OBJECT, dec.nextToken());
assertEquals(JsonToken.END_OBJECT, dec.getCurrentToken());
assertNull(dec.nextToken());
assertNull(dec.getCurrentToken());
}
}
}

0 comments on commit 2c1a3b0

Please sign in to comment.