-
Type:
Bug
-
Resolution: Unresolved
-
Priority:
Minor - P4
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
BsonReaderMark.reset() is documented as "Reset the BsonReader to its state at the
time the mark was created." This contract is violated by BsonDocumentReader whenever
more than one mark is outstanding: every reset() returns the reader to the earliest
mark rather than the specific mark being reset.
BsonBinaryReader (and JsonReader) behave correctly. The bug is specific to
BsonDocumentReader.
Reported via HELP-95701
Steps to reproduce
import org.bson.codecs.BsonDocumentCodec; import org.bson.codecs.EncoderContext; import org.bson.io.BasicOutputBuffer; import org.junit.jupiter.api.Test; import java.nio.ByteBuffer; import static org.junit.jupiter.api.Assertions.assertEquals; // Reproduction for the reported BsonDocumentReader mark/reset discrepancy. public class BsonReaderMarkReproTest { private static final BsonDocument DOCUMENT = BsonDocument.parse("{ 'a': 1, 'b': 2, 'c': 3 }"); @Test void binaryReaderResetsToSecondMark() { try (BsonBinaryReader reader = new BsonBinaryReader(ByteBuffer.wrap(toBytes(DOCUMENT)))) { assertEquals("b", fieldNameAfterResettingSecondMark(reader)); } } @Test void documentReaderResetsToSecondMark() { assertEquals("b", fieldNameAfterResettingSecondMark(DOCUMENT.asBsonReader())); } private static String fieldNameAfterResettingSecondMark(final BsonReader reader) { reader.readStartDocument(); final BsonReaderMark mark = reader.getMark(); //unused assertEquals("a", readNextInt32FieldName(reader)); BsonReaderMark secondMark = reader.getMark(); assertEquals("b", readNextInt32FieldName(reader)); secondMark.reset(); return readNextInt32FieldName(reader); } private static String readNextInt32FieldName(final BsonReader reader) { assertEquals(BsonType.INT32, reader.readBsonType()); String name = reader.readName(); reader.readInt32(); return name; } private static byte[] toBytes(final BsonDocument document) { BasicOutputBuffer buffer = new BasicOutputBuffer(); try (BsonBinaryWriter writer = new BsonBinaryWriter(buffer)) { new BsonDocumentCodec().encode(writer, document, EncoderContext.builder().build()); } return buffer.toByteArray(); } }