Issue
If a BsonBinary object is read multiple times by the UUID codec it returns a different result each time. The first read returns the correct data, all reads after are incorrect. From looking at the UuidCodec code it looks like the codec does operations on the underlying BsonBinary byte array which is causing it to change.
I originally found this issue in 3.8 and was able to reproduce it in 3.12.1. I also found that the issue only exists for the JAVA_LEGACY and C_SHARP_LEGACY UuidRepresentations.
Expected Behavior
When a UUID is read from a BsonBinary object, the BsonBinary object is left unchanged.
Steps to Reproduce
Simple test to recreate the issue. The second read's assert fails.
@Test public void testMultiReadUuid() { String key = "key"; UUID uuid = UUID.randomUUID(); UuidRepresentation uuidRepresentation = UuidRepresentation.JAVA_LEGACY; BsonDocument doc = new BsonDocument().append(key, new BsonBinary(uuid, uuidRepresentation)); try (BsonReader reader = new BsonDocumentReader(doc)) { reader.readStartDocument(); reader.readName(key); // First read success Assert.assertEquals(uuid, new UuidCodec(uuidRepresentation).decode(reader, DecoderContext.builder().build())); reader.readEndDocument(); } try (BsonReader reader = new BsonDocumentReader(doc)) { reader.readStartDocument(); reader.readName(key); // Second read fail Assert.assertEquals(uuid, new UuidCodec(uuidRepresentation).decode(reader, DecoderContext.builder().build())); reader.readEndDocument(); } }