Skip to content

Commit 55ec089

Browse files
author
Isabella Siu
committed
GODRIVER-802 fix document fields corrupted in transformAndEnsureID
Change-Id: I20957caedda200b1ffd8db3738d562798abbf6f9
1 parent 9a7709f commit 55ec089

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

mongo/mongo.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ func transformAndEnsureID(registry *bsoncodec.Registry, val interface{}) (bsonx.
117117
d[0] = idElem
118118
}
119119

120-
t, data, err := idElem.Value.MarshalAppendBSONValue(buf[:0])
120+
idBuf := make([]byte, 0, 256)
121+
t, data, err := idElem.Value.MarshalAppendBSONValue(idBuf[:0])
121122
if err != nil {
122123
return nil, nil, err
123124
}

mongo/mongo_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ func TestTransformAndEnsureID(t *testing.T) {
131131
t.Errorf("Returned documents differ: (-got +want)\n%s", diff)
132132
}
133133
})
134+
t.Run("existing _id should not overwrite a first binary field", func(t *testing.T) {
135+
doc := bson.D{{"bin", []byte{0, 0, 0}}, {"_id", "LongEnoughIdentifier"}}
136+
want := bsonx.Doc{
137+
{"_id", bsonx.String("LongEnoughIdentifier")},
138+
{"bin", bsonx.Binary(0x00, []byte{0x00, 0x00, 0x00})},
139+
}
140+
got, id, err := transformAndEnsureID(bson.DefaultRegistry, doc)
141+
noerr(t, err)
142+
_, ok := id.(string)
143+
if !ok {
144+
t.Fatalf("Expected returned id to be a %T, but was %T", string(0), id)
145+
}
146+
if diff := cmp.Diff(got, want, cmp.AllowUnexported(bsonx.Elem{}, bsonx.Val{})); diff != "" {
147+
t.Errorf("Returned documents differ: (-got +want)\n%s", diff)
148+
}
149+
})
134150
}
135151

136152
func TestTransformAggregatePipeline(t *testing.T) {

0 commit comments

Comments
 (0)