From bddde89fee487aea7e29482e61bb1b2411db1364 Mon Sep 17 00:00:00 2001 From: Denis Glotov Date: Sun, 14 Mar 2021 18:44:49 +0300 Subject: [PATCH 1/2] GODRIVER-1919 Add object id decoder awareness of string hex --- bson/bsoncodec/default_value_decoders.go | 3 +++ bson/bsoncodec/default_value_decoders_test.go | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/bson/bsoncodec/default_value_decoders.go b/bson/bsoncodec/default_value_decoders.go index 0402265d93..dd96f260b0 100644 --- a/bson/bsoncodec/default_value_decoders.go +++ b/bson/bsoncodec/default_value_decoders.go @@ -740,6 +740,9 @@ func (dvd DefaultValueDecoders) objectIDDecodeType(dc DecodeContext, vr bsonrw.V if err != nil { return emptyValue, err } + if oid, err = primitive.ObjectIDFromHex(str); err == nil { + break + } if len(str) != 12 { return emptyValue, fmt.Errorf("an ObjectID string must be exactly 12 bytes long (got %v)", len(str)) } diff --git a/bson/bsoncodec/default_value_decoders_test.go b/bson/bsoncodec/default_value_decoders_test.go index b7fc2b5636..c1a0f6e0d9 100644 --- a/bson/bsoncodec/default_value_decoders_test.go +++ b/bson/bsoncodec/default_value_decoders_test.go @@ -1118,6 +1118,17 @@ func TestDefaultValueDecoders(t *testing.T) { bsonrwtest.ReadString, nil, }, + { + "success/string-hex", + primitive.ObjectID{0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62}, + nil, + &bsonrwtest.ValueReaderWriter{ + BSONType: bsontype.String, + Return: "303132333435363738396162", + }, + bsonrwtest.ReadString, + nil, + }, { "decode null", primitive.ObjectID{}, From 2b299f1d2165a447100d31783a6fd682abee2c62 Mon Sep 17 00:00:00 2001 From: Denis Glotov Date: Wed, 17 Mar 2021 21:00:20 +0300 Subject: [PATCH 2/2] GODRIVER-1919 object id decoder, clarifying comment --- bson/bsoncodec/default_value_decoders.go | 1 + 1 file changed, 1 insertion(+) diff --git a/bson/bsoncodec/default_value_decoders.go b/bson/bsoncodec/default_value_decoders.go index dd96f260b0..18919e3c40 100644 --- a/bson/bsoncodec/default_value_decoders.go +++ b/bson/bsoncodec/default_value_decoders.go @@ -718,6 +718,7 @@ func (dvd DefaultValueDecoders) UndefinedDecodeValue(dc DecodeContext, vr bsonrw return nil } +// Accept both 12-byte string and pretty-printed 24-byte hex string formats. func (dvd DefaultValueDecoders) objectIDDecodeType(dc DecodeContext, vr bsonrw.ValueReader, t reflect.Type) (reflect.Value, error) { if t != tOID { return emptyValue, ValueDecoderError{