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
3 changes: 2 additions & 1 deletion bson/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,8 @@ func TestDecoderConfiguration(t *testing.T) {

var got objectIDTest
err := dec.Decode(&got)
assert.EqualError(t, err, "error decoding key id: decoding an object ID to a non-hexadecimal string representation is not supported")
const want = "error decoding key id: decoding an object ID into a string is not supported by default (set Decoder.ObjectIDAsHexString to enable decoding as a hexadecimal string)"
assert.EqualError(t, err, want)
})
t.Run("DefaultDocumentM top-level", func(t *testing.T) {
t.Parallel()
Expand Down
13 changes: 8 additions & 5 deletions bson/string_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package bson

import (
"errors"
"fmt"
"reflect"
)
Expand Down Expand Up @@ -50,14 +51,16 @@ func (sc *stringCodec) decodeType(dc DecodeContext, vr ValueReader, t reflect.Ty
return emptyValue, err
}
case TypeObjectID:
oid, err := vr.ReadObjectID()
if err != nil {
return emptyValue, err
}
if dc.objectIDAsHexString {
oid, err := vr.ReadObjectID()
if err != nil {
return emptyValue, err
}
str = oid.Hex()
} else {
return emptyValue, fmt.Errorf("decoding an object ID to a non-hexadecimal string representation is not supported")
const msg = "decoding an object ID into a string is not supported by default " +
"(set Decoder.ObjectIDAsHexString to enable decoding as a hexadecimal string)"
return emptyValue, errors.New(msg)
}
case TypeSymbol:
str, err = vr.ReadSymbol()
Expand Down
Loading