Skip to content

Commit ec10aec

Browse files
committed
Fix decoding BSON binary UUID values in Iterables.
JAVA-1900
1 parent 8ece395 commit ec10aec

File tree

2 files changed

+14
-24
lines changed

2 files changed

+14
-24
lines changed

bson/src/main/org/bson/codecs/IterableCodec.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,8 @@ private Object readValue(final BsonReader reader, final DecoderContext decoderCo
112112
if (bsonType == BsonType.NULL) {
113113
reader.readNull();
114114
return null;
115-
} else if (bsonType == BsonType.BINARY) {
116-
byte bsonSubType = reader.peekBinarySubType();
117-
if (bsonSubType == BsonBinarySubType.UUID_STANDARD.getValue() || bsonSubType == BsonBinarySubType.UUID_LEGACY.getValue()) {
118-
return registry.get(UUID.class).decode(reader, decoderContext);
119-
}
115+
} else if (bsonType == BsonType.BINARY && BsonBinarySubType.isUuid(reader.peekBinarySubType()) && reader.peekBinarySize() == 16) {
116+
return registry.get(UUID.class).decode(reader, decoderContext);
120117
}
121118
return valueTransformer.transform(bsonTypeCodecMap.get(bsonType).decode(reader, decoderContext));
122119
}

bson/src/test/unit/org/bson/codecs/IterableCodecSpecification.groovy

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package org.bson.codecs
2020
import org.bson.BsonDocument
2121
import org.bson.BsonDocumentReader
2222
import org.bson.BsonDocumentWriter
23+
import org.bson.types.Binary
2324
import spock.lang.Specification
2425

2526
import static org.bson.BsonDocument.parse
@@ -85,10 +86,10 @@ class IterableCodecSpecification extends Specification {
8586
iterable == ['1', '2', '3']
8687
}
8788

88-
def 'should decode a BSON Binary with subtype of UIID_LEGACY to a UUID'() {
89+
def 'should decode binary subtypes for UUID'() {
8990
given:
9091
def codec = new IterableCodec(REGISTRY, new BsonTypeClassMap(), null)
91-
def reader = new BsonDocumentReader(parse('{array : [{ "$binary" : "D0dqZ20GeYvWzXdt0gkSlA==", "$type" : "3" }]}'))
92+
def reader = new BsonDocumentReader(parse(document))
9293

9394
when:
9495
reader.readStartDocument()
@@ -97,22 +98,14 @@ class IterableCodecSpecification extends Specification {
9798
reader.readEndDocument()
9899

99100
then:
100-
iterable == [UUID.fromString('8b79066d-676a-470f-9412-09d26d77cdd6')]
101+
iterable == value
102+
103+
where:
104+
document | value
105+
'{"array": [{ "$binary" : "c3QL", "$type" : "3" }]}' | [new Binary((byte) 0x03, (byte[]) [115, 116, 11])]
106+
'{"array": [{ "$binary" : "c3QL", "$type" : "4" }]}' | [new Binary((byte) 0x04, (byte[]) [115, 116, 11])]
107+
'{"array": [{ "$binary" : "AQIDBAUGBwgJCgsMDQ4PEA==", "$type" : "3" }]}' | [UUID.fromString('08070605-0403-0201-100f-0e0d0c0b0a09')]
108+
'{"array": [{ "$binary" : "CAcGBQQDAgEQDw4NDAsKCQ==", "$type" : "3" }]}' | [UUID.fromString('01020304-0506-0708-090a-0b0c0d0e0f10')]
101109
}
102110

103-
def 'should decode a BSON Binary with subtype of UIID_STANDARD to a UUID'() {
104-
given:
105-
def codec = new IterableCodec(REGISTRY, new BsonTypeClassMap(), null)
106-
def reader = new BsonDocumentReader(parse('{array : [{ "$binary" : "i3kGbWdqRw+UEgnSbXfN1g==", "$type" : "4" }]}'))
107-
108-
when:
109-
reader.readStartDocument()
110-
reader.readName('array')
111-
def iterable = codec.decode(reader, DecoderContext.builder().build())
112-
reader.readEndDocument()
113-
114-
then:
115-
iterable == [UUID.fromString('8b79066d-676a-470f-9412-09d26d77cdd6')]
116-
}
117-
118-
}
111+
}

0 commit comments

Comments
 (0)