Skip to content

Commit db4e5ef

Browse files
author
Divjot Arora
authored
GODRIVER-1407 Fix enocding of non-string keys (#250)
1 parent fe5e475 commit db4e5ef

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

bson/bsoncodec/default_value_encoders_test.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/json"
1111
"errors"
1212
"fmt"
13+
"math"
1314
"net/url"
1415
"reflect"
1516
"testing"
@@ -21,7 +22,6 @@ import (
2122
"go.mongodb.org/mongo-driver/bson/bsontype"
2223
"go.mongodb.org/mongo-driver/bson/primitive"
2324
"go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
24-
"math"
2525
)
2626

2727
type myInterface interface {
@@ -217,7 +217,7 @@ func TestDefaultValueEncoders(t *testing.T) {
217217
},
218218
{
219219
"MapEncodeValue",
220-
ValueEncoderFunc(dve.MapEncodeValue),
220+
defaultMapCodec,
221221
[]subtest{
222222
{
223223
"wrong kind",
@@ -227,18 +227,6 @@ func TestDefaultValueEncoders(t *testing.T) {
227227
bsonrwtest.Nothing,
228228
ValueEncoderError{Name: "MapEncodeValue", Kinds: []reflect.Kind{reflect.Map}, Received: reflect.ValueOf(wrong)},
229229
},
230-
{
231-
"wrong kind (non-string key)",
232-
map[int]interface{}{},
233-
nil,
234-
nil,
235-
bsonrwtest.Nothing,
236-
ValueEncoderError{
237-
Name: "MapEncodeValue",
238-
Kinds: []reflect.Kind{reflect.Map},
239-
Received: reflect.ValueOf(map[int]interface{}{}),
240-
},
241-
},
242230
{
243231
"WriteDocument Error",
244232
map[string]interface{}{},
@@ -295,6 +283,16 @@ func TestDefaultValueEncoders(t *testing.T) {
295283
bsonrwtest.WriteDocumentEnd,
296284
nil,
297285
},
286+
{
287+
"non-string key success",
288+
map[int]interface{}{
289+
1: "foobar",
290+
},
291+
&EncodeContext{Registry: buildDefaultRegistry()},
292+
&bsonrwtest.ValueReaderWriter{},
293+
bsonrwtest.WriteDocumentEnd,
294+
nil,
295+
},
298296
},
299297
},
300298
{

bson/bsoncodec/map_codec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (mc *MapCodec) mapEncodeValue(ec EncodeContext, dw bsonrw.DocumentWriter, v
8585
return lookupErr
8686
}
8787

88-
vw, err := dw.WriteDocumentElement(key.String())
88+
vw, err := dw.WriteDocumentElement(keyStr)
8989
if err != nil {
9090
return err
9191
}

0 commit comments

Comments
 (0)