Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions bson/src/main/org/bson/LazyBSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.bson;

import org.bson.codecs.DecoderContext;
import org.bson.codecs.UuidCodec;
import org.bson.io.ByteBufferBsonInput;
import org.bson.types.BSONTimestamp;
import org.bson.types.Binary;
Expand Down Expand Up @@ -169,9 +167,6 @@ Object readValue(final BsonBinaryReader reader) {
return reader.readString();
case BINARY:
byte binarySubType = reader.peekBinarySubType();
if (BsonBinarySubType.isUuid(binarySubType) && reader.peekBinarySize() == 16) {
return new UuidCodec(UuidRepresentation.JAVA_LEGACY).decode(reader, DecoderContext.builder().build());
}
BsonBinary binary = reader.readBinaryData();
if (binarySubType == BINARY.getValue() || binarySubType == OLD_BINARY.getValue()) {
return binary.getData();
Expand Down
4 changes: 2 additions & 2 deletions bson/src/main/org/bson/codecs/DocumentCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public DocumentCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTy
*/
public DocumentCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTypeClassMap, final Transformer valueTransformer) {
this(registry, new BsonTypeCodecMap(notNull("bsonTypeClassMap", bsonTypeClassMap), registry),
new ObjectIdGenerator(), valueTransformer, UuidRepresentation.JAVA_LEGACY);
new ObjectIdGenerator(), valueTransformer, UuidRepresentation.UNSPECIFIED);
}

private DocumentCodec(final CodecRegistry registry, final BsonTypeCodecMap bsonTypeCodecMap, final IdGenerator idGenerator,
Expand Down Expand Up @@ -242,7 +242,7 @@ private Object readValue(final BsonReader reader, final DecoderContext decoderCo
}
break;
case 4:
if (uuidRepresentation == UuidRepresentation.JAVA_LEGACY || uuidRepresentation == UuidRepresentation.STANDARD) {
if (uuidRepresentation == UuidRepresentation.STANDARD) {
codec = registry.get(UUID.class);
}
break;
Expand Down
4 changes: 2 additions & 2 deletions bson/src/main/org/bson/codecs/IterableCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public IterableCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTy
*/
public IterableCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTypeClassMap, final Transformer valueTransformer) {
this(registry, new BsonTypeCodecMap(notNull("bsonTypeClassMap", bsonTypeClassMap), registry), valueTransformer,
UuidRepresentation.JAVA_LEGACY);
UuidRepresentation.UNSPECIFIED);
}

private IterableCodec(final CodecRegistry registry, final BsonTypeCodecMap bsonTypeCodecMap, final Transformer valueTransformer,
Expand Down Expand Up @@ -138,7 +138,7 @@ private Object readValue(final BsonReader reader, final DecoderContext decoderCo
}
break;
case 4:
if (uuidRepresentation == UuidRepresentation.JAVA_LEGACY || uuidRepresentation == UuidRepresentation.STANDARD) {
if (uuidRepresentation == UuidRepresentation.STANDARD) {
codec = registry.get(UUID.class);
}
break;
Expand Down
4 changes: 2 additions & 2 deletions bson/src/main/org/bson/codecs/MapCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public MapCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTypeCla
*/
public MapCodec(final CodecRegistry registry, final BsonTypeClassMap bsonTypeClassMap, final Transformer valueTransformer) {
this(registry, new BsonTypeCodecMap(notNull("bsonTypeClassMap", bsonTypeClassMap), registry), valueTransformer,
UuidRepresentation.JAVA_LEGACY);
UuidRepresentation.UNSPECIFIED);
}

private MapCodec(final CodecRegistry registry, final BsonTypeCodecMap bsonTypeCodecMap, final Transformer valueTransformer,
Expand Down Expand Up @@ -153,7 +153,7 @@ private Object readValue(final BsonReader reader, final DecoderContext decoderCo
}
break;
case 4:
if (uuidRepresentation == UuidRepresentation.JAVA_LEGACY || uuidRepresentation == UuidRepresentation.STANDARD) {
if (uuidRepresentation == UuidRepresentation.STANDARD) {
codec = registry.get(UUID.class);
}
break;
Expand Down
2 changes: 1 addition & 1 deletion bson/src/main/org/bson/codecs/UuidCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public UuidCodec(final UuidRepresentation uuidRepresentation) {
* The constructor for UUIDCodec, default is JAVA_LEGACY
*/
public UuidCodec() {
this.uuidRepresentation = UuidRepresentation.JAVA_LEGACY;
this.uuidRepresentation = UuidRepresentation.UNSPECIFIED;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion bson/src/main/org/bson/internal/CodecRegistryHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public final class CodecRegistryHelper {

public static CodecRegistry createRegistry(final CodecRegistry codecRegistry, final UuidRepresentation uuidRepresentation) {
CodecRegistry retVal = codecRegistry;
if (uuidRepresentation != UuidRepresentation.JAVA_LEGACY) {
if (uuidRepresentation != UuidRepresentation.UNSPECIFIED) {
if (codecRegistry instanceof CodecProvider) {
retVal = new OverridableUuidRepresentationCodecRegistry((CodecProvider) codecRegistry, uuidRepresentation);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ class LazyBSONObjectSpecification extends Specification {
new Binary((byte) 0x01, (byte[]) [115, 116, 11]) | [16, 0, 0, 0, 5, 102, 0, 3, 0, 0, 0, 1, 115, 116, 11, 0]
new Binary((byte) 0x03, (byte[]) [115, 116, 11]) | [16, 0, 0, 0, 5, 102, 0, 3, 0, 0, 0, 3, 115, 116, 11, 0]
new Binary((byte) 0x04, (byte[]) [115, 116, 11]) | [16, 0, 0, 0, 5, 102, 0, 3, 0, 0, 0, 4, 115, 116, 11, 0]
UUID.fromString('08070605-0403-0201-100f-0e0d0c0b0a09') | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
UUID.fromString('01020304-0506-0708-090a-0b0c0d0e0f10') | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
[13, 12] as byte[] | [15, 0, 0, 0, 5, 102, 0, 2, 0, 0, 0, 0, 13, 12, 0]
[102, 111, 111] as byte[] | [16, 0, 0, 0, 5, 102, 0, 3, 0, 0, 0, 0, 102, 111, 111, 0]
new ObjectId('50d3332018c6a1d8d1662b61') | [20, 0, 0, 0, 7, 102, 0, 80, -45, 51, 32, 24, -58, -95, -40, -47, 102, 43, 97, 0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import org.bson.BsonUndefined
import org.bson.BsonWriter
import org.bson.ByteBufNIO
import org.bson.Document
import org.bson.codecs.configuration.CodecRegistry
import org.bson.io.BasicOutputBuffer
import org.bson.io.ByteBufferBsonInput
import org.bson.json.JsonReader
Expand Down Expand Up @@ -57,8 +58,12 @@ import static org.bson.UuidRepresentation.STANDARD
import static org.bson.UuidRepresentation.UNSPECIFIED
import static org.bson.codecs.configuration.CodecRegistries.fromCodecs
import static org.bson.codecs.configuration.CodecRegistries.fromProviders
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries

class DocumentCodecSpecification extends Specification {
static final CodecRegistry REGISTRY = fromRegistries(fromCodecs(new UuidCodec(STANDARD)),
fromProviders(asList(new ValueCodecProvider(), new BsonValueCodecProvider(), new DocumentCodecProvider())))

@Shared
BsonDocument bsonDoc = new BsonDocument()
@Shared
Expand Down Expand Up @@ -97,7 +102,8 @@ class DocumentCodecSpecification extends Specification {
}

when:
new DocumentCodec().encode(writer, originalDocument, EncoderContext.builder().build())
new DocumentCodec(REGISTRY).withUuidRepresentation(STANDARD)
.encode(writer, originalDocument, EncoderContext.builder().build())
BsonReader reader
if (writer instanceof BsonDocumentWriter) {
reader = new BsonDocumentReader(bsonDoc)
Expand All @@ -108,7 +114,7 @@ class DocumentCodecSpecification extends Specification {
} else {
reader = new JsonReader(stringWriter.toString())
}
def decodedDoc = new DocumentCodec().decode(reader, DecoderContext.builder().build())
def decodedDoc = new DocumentCodec(REGISTRY).withUuidRepresentation(STANDARD).decode(reader, DecoderContext.builder().build())

then:
decodedDoc.get('null') == originalDocument.get('null')
Expand Down Expand Up @@ -192,16 +198,17 @@ class DocumentCodecSpecification extends Specification {
def reader = new BsonBinaryReader(ByteBuffer.wrap(bytes as byte[]))

when:
def document = new DocumentCodec().withUuidRepresentation(representation)
def document = new DocumentCodec(fromCodecs(new UuidCodec(representation), new BinaryCodec()))
.withUuidRepresentation(representation)
.decode(reader, DecoderContext.builder().build())

then:
value == document.get('f')

where:
representation | value | bytes
STANDARD | UUID.fromString('01020304-0506-0708-090a-0b0c0d0e0f10') | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
JAVA_LEGACY | UUID.fromString('01020304-0506-0708-090a-0b0c0d0e0f10') | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
representation | value | bytes
STANDARD | UUID.fromString('01020304-0506-0708-090a-0b0c0d0e0f10') | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
JAVA_LEGACY | new Binary((byte) 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] as byte[]) | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
C_SHARP_LEGACY | new Binary((byte) 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] as byte[]) | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
PYTHON_LEGACY | new Binary((byte) 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] as byte[]) | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
UNSPECIFIED | new Binary((byte) 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] as byte[]) | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.bson.codecs


import org.bson.BsonDocument
import org.bson.BsonDocumentReader
import org.bson.BsonDocumentWriter
Expand All @@ -32,11 +31,12 @@ import static org.bson.UuidRepresentation.STANDARD
import static org.bson.UuidRepresentation.UNSPECIFIED
import static org.bson.codecs.configuration.CodecRegistries.fromCodecs
import static org.bson.codecs.configuration.CodecRegistries.fromProviders
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries

class IterableCodecSpecification extends Specification {

static final REGISTRY = fromProviders(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(),
new IterableCodecProvider())
static final REGISTRY = fromRegistries(fromCodecs(new UuidCodec(JAVA_LEGACY)),
fromProviders(new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider(), new IterableCodecProvider()))

def 'should have Iterable encoding class'() {
given:
Expand Down Expand Up @@ -108,28 +108,6 @@ class IterableCodecSpecification extends Specification {
iterable == ['1', '2', '3']
}

def 'should decode binary subtypes for UUID'() {
given:
def codec = new IterableCodec(REGISTRY, new BsonTypeClassMap(), null)
def reader = new BsonDocumentReader(parse(document))

when:
reader.readStartDocument()
reader.readName('array')
def iterable = codec.decode(reader, DecoderContext.builder().build())
reader.readEndDocument()

then:
iterable == value

where:
document | value
'{"array": [{ "$binary" : "c3QL", "$type" : "3" }]}' | [new Binary((byte) 0x03, (byte[]) [115, 116, 11])]
'{"array": [{ "$binary" : "c3QL", "$type" : "4" }]}' | [new Binary((byte) 0x04, (byte[]) [115, 116, 11])]
'{"array": [{ "$binary" : "AQIDBAUGBwgJCgsMDQ4PEA==", "$type" : "3" }]}' | [UUID.fromString('08070605-0403-0201-100f-0e0d0c0b0a09')]
'{"array": [{ "$binary" : "CAcGBQQDAgEQDw4NDAsKCQ==", "$type" : "3" }]}' | [UUID.fromString('01020304-0506-0708-090a-0b0c0d0e0f10')]
}

@SuppressWarnings(['LineLength'])
@Unroll
def 'should decode binary subtype 3 for UUID'() {
Expand Down Expand Up @@ -176,7 +154,7 @@ class IterableCodecSpecification extends Specification {
where:
representation | value | document
STANDARD | [UUID.fromString('01020304-0506-0708-090a-0b0c0d0e0f10')] | '{"array": [{ "$binary" : "AQIDBAUGBwgJCgsMDQ4PEA==", "$type" : "4" }]}'
JAVA_LEGACY | [UUID.fromString('01020304-0506-0708-090a-0b0c0d0e0f10')] | '{"array": [{ "$binary" : "AQIDBAUGBwgJCgsMDQ4PEA==", "$type" : "4" }]}'
JAVA_LEGACY | [UUID.fromString('01020304-0506-0708-090a-0b0c0d0e0f10')] | '{"array": [{ "$binary" : "CAcGBQQDAgEQDw4NDAsKCQ==", "$type" : "3" }]}'
C_SHARP_LEGACY | [new Binary((byte) 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] as byte[])] | '{"array": [{ "$binary" : "AQIDBAUGBwgJCgsMDQ4PEA==", "$type" : "4" }]}'
PYTHON_LEGACY | [new Binary((byte) 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] as byte[])] | '{"array": [{ "$binary" : "AQIDBAUGBwgJCgsMDQ4PEA==", "$type" : "4" }]}'
UNSPECIFIED | [new Binary((byte) 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] as byte[])] | '{"array": [{ "$binary" : "AQIDBAUGBwgJCgsMDQ4PEA==", "$type" : "4" }]}'
Expand Down
38 changes: 30 additions & 8 deletions bson/src/test/unit/org/bson/codecs/MapCodecSpecification.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,14 @@ import static org.bson.UuidRepresentation.STANDARD
import static org.bson.UuidRepresentation.UNSPECIFIED
import static org.bson.codecs.configuration.CodecRegistries.fromCodecs
import static org.bson.codecs.configuration.CodecRegistries.fromProviders
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries

class MapCodecSpecification extends Specification {

static final REGISTRY = fromRegistries(fromCodecs(new UuidCodec(JAVA_LEGACY)),
fromProviders(asList(new ValueCodecProvider(), new BsonValueCodecProvider(),
new DocumentCodecProvider(), new IterableCodecProvider(), new MapCodecProvider())))

@Shared
BsonDocument bsonDoc = new BsonDocument()
@Shared
Expand Down Expand Up @@ -88,7 +94,6 @@ class MapCodecSpecification extends Specification {
put('undefined', new BsonUndefined())
put('binary', new Binary((byte) 0x80, [5, 4, 3, 2, 1] as byte[]))
put('array', asList(1, 1L, true, [1, 2, 3], new Document('a', 1), null))
put('uuid', new UUID(1L, 2L))
put('document', new Document('a', 2))
put('map', [a:1, b:2])
put('atomicLong', new AtomicLong(1))
Expand All @@ -97,7 +102,7 @@ class MapCodecSpecification extends Specification {
}

when:
new MapCodec().encode(writer, originalDocument, EncoderContext.builder().build())
new MapCodec(REGISTRY).encode(writer, originalDocument, EncoderContext.builder().build())
BsonReader reader
if (writer instanceof BsonDocumentWriter) {
reader = new BsonDocumentReader(bsonDoc)
Expand All @@ -108,7 +113,7 @@ class MapCodecSpecification extends Specification {
} else {
reader = new JsonReader(stringWriter.toString())
}
def decodedDoc = new MapCodec().decode(reader, DecoderContext.builder().build())
def decodedDoc = new MapCodec(REGISTRY).decode(reader, DecoderContext.builder().build())

then:
decodedDoc.get('null') == originalDocument.get('null')
Expand All @@ -130,7 +135,6 @@ class MapCodecSpecification extends Specification {
decodedDoc.get('timestamp') == originalDocument.get('timestamp')
decodedDoc.get('undefined') == originalDocument.get('undefined')
decodedDoc.get('binary') == originalDocument.get('binary')
decodedDoc.get('uuid') == originalDocument.get('uuid')
decodedDoc.get('array') == originalDocument.get('array')
decodedDoc.get('document') == originalDocument.get('document')
decodedDoc.get('map') == originalDocument.get('map')
Expand All @@ -145,6 +149,22 @@ class MapCodecSpecification extends Specification {
]
}

def 'should decode binary subtypes for UUID that are not 16 bytes into Binary'() {
given:
def reader = new BsonBinaryReader(ByteBuffer.wrap(bytes as byte[]))

when:
def document = new DocumentCodec().decode(reader, DecoderContext.builder().build())

then:
value == document.get('f')

where:
value | bytes
new Binary((byte) 0x03, (byte[]) [115, 116, 11]) | [16, 0, 0, 0, 5, 102, 0, 3, 0, 0, 0, 3, 115, 116, 11, 0]
new Binary((byte) 0x04, (byte[]) [115, 116, 11]) | [16, 0, 0, 0, 5, 102, 0, 3, 0, 0, 0, 4, 115, 116, 11, 0]
}

@SuppressWarnings(['LineLength'])
@Unroll
def 'should decode binary subtype 3 for UUID'() {
Expand Down Expand Up @@ -175,21 +195,23 @@ class MapCodecSpecification extends Specification {
def reader = new BsonBinaryReader(ByteBuffer.wrap(bytes as byte[]))

when:
def map = new MapCodec().withUuidRepresentation(representation)
def map = new MapCodec(fromCodecs(new UuidCodec(representation), new BinaryCodec()))
.withUuidRepresentation(representation)
.decode(reader, DecoderContext.builder().build())

then:
value == map.get('f')

where:
representation | value | bytes
STANDARD | UUID.fromString('01020304-0506-0708-090a-0b0c0d0e0f10') | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
JAVA_LEGACY | UUID.fromString('01020304-0506-0708-090a-0b0c0d0e0f10') | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
representation | value | bytes
STANDARD | UUID.fromString('01020304-0506-0708-090a-0b0c0d0e0f10') | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
JAVA_LEGACY | new Binary((byte) 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] as byte[]) | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
C_SHARP_LEGACY | new Binary((byte) 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] as byte[]) | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
PYTHON_LEGACY | new Binary((byte) 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] as byte[]) | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
UNSPECIFIED | new Binary((byte) 4, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] as byte[]) | [29, 0, 0, 0, 5, 102, 0, 16, 0, 0, 0, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0]
}


def 'should apply transformer to decoded values'() {
given:
def codec = new MapCodec(fromProviders([new ValueCodecProvider(), new DocumentCodecProvider(), new BsonValueCodecProvider()]),
Expand Down
Loading