Skip to content

Commit 233bb4b

Browse files
committed
Merge branch 'pr/133'
GODRIVER-775 Change-Id: I190dd81087fde60c221c352d908409d2e196a80a
2 parents f456f72 + 50d163e commit 233bb4b

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

bson/bsonrw/extjson_parser.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,51 @@ func (ejp *extJSONParser) readValue(t bsontype.Type) (*extJSONValue, error) {
264264
return nil, err
265265
}
266266

267+
ejp.advanceState()
268+
if t == bsontype.Binary && ejp.s == jpsSawValue {
269+
// convert legacy $binary format
270+
base64 := ejp.v
271+
272+
ejp.advanceState()
273+
if ejp.s != jpsSawComma {
274+
return nil, invalidJSONErrorForType(",", bsontype.Binary)
275+
}
276+
277+
ejp.advanceState()
278+
key, t, err := ejp.readKey()
279+
if err != nil {
280+
return nil, err
281+
}
282+
if key != "$type" {
283+
return nil, invalidJSONErrorForType("$type", bsontype.Binary)
284+
}
285+
286+
subType, err := ejp.readValue(t)
287+
if err != nil {
288+
return nil, err
289+
}
290+
291+
ejp.advanceState()
292+
if ejp.s != jpsSawEndObject {
293+
return nil, invalidJSONErrorForType("2 key-value pairs and then }", bsontype.Binary)
294+
}
295+
296+
v = &extJSONValue{
297+
t: bsontype.EmbeddedDocument,
298+
v: &extJSONObject{
299+
keys: []string{"base64", "subType"},
300+
values: []*extJSONValue{base64, subType},
301+
},
302+
}
303+
break
304+
}
305+
267306
// read KV pairs
268-
keys, vals, err := ejp.readObject(2, false)
307+
if ejp.s != jpsSawBeginObject {
308+
return nil, invalidJSONErrorForType("{", t)
309+
}
310+
311+
keys, vals, err := ejp.readObject(2, true)
269312
if err != nil {
270313
return nil, err
271314
}
@@ -276,6 +319,7 @@ func (ejp *extJSONParser) readValue(t bsontype.Type) (*extJSONValue, error) {
276319
}
277320

278321
v = &extJSONValue{t: bsontype.EmbeddedDocument, v: &extJSONObject{keys: keys, values: vals}}
322+
279323
case bsontype.DateTime:
280324
switch ejp.s {
281325
case jpsSawValue:

bson/bsonrw/extjson_parser_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func TestExtJSONParserPeekType(t *testing.T) {
125125
makeValidPeekTypeTestCase(`{"$binary": {"base64": "AQIDBAU=", "subType": "80"}}`, bsontype.Binary, "Binary"),
126126
makeValidPeekTypeTestCase(`{"$code": "function() {}", "$scope": {}}`, bsontype.CodeWithScope, "Code With Scope"),
127127
makeValidPeekTypeTestCase(`{"$binary": {"base64": "o0w498Or7cijeBSpkquNtg==", "subType": "03"}}`, bsontype.Binary, "Binary"),
128+
makeValidPeekTypeTestCase(`{"$binary": "o0w498Or7cijeBSpkquNtg==", "$type": "03"}`, bsontype.Binary, "Binary"),
128129
makeValidPeekTypeTestCase(`{"$regularExpression": {"pattern": "foo*", "options": "ix"}}`, bsontype.Regex, "Regular expression"),
129130
makeValidPeekTypeTestCase(`{"$dbPointer": {"$ref": "db.collection", "$id": {"$oid": "57e193d7a9cc81b4027498b1"}}}`, bsontype.DBPointer, "DBPointer"),
130131
makeValidPeekTypeTestCase(`{"$ref": "collection", "$id": {"$oid": "57fd71e96e32ab4225b723fb"}, "$db": "database"}`, bsontype.EmbeddedDocument, "DBRef"),
@@ -344,8 +345,8 @@ func expectMultipleValues(t *testing.T, p *extJSONParser, expectedKey string, ex
344345
readKeyDiff(t, expectedKey, k, expectedType, typ, err, expectNoError, expectedKey)
345346

346347
v, err := p.readValue(typ)
347-
typDiff(t, bsontype.EmbeddedDocument, v.t, expectedKey)
348348
expectNoError(t, err, "")
349+
typDiff(t, bsontype.EmbeddedDocument, v.t, expectedKey)
349350

350351
actObj := v.v.(*extJSONObject)
351352
expObj := expectedValue.(*extJSONObject)
@@ -445,6 +446,7 @@ func TestExtJSONParserAllTypes(t *testing.T) {
445446
, "SpecialFloat" : { "$numberDouble": "NaN" }
446447
, "Decimal" : { "$numberDecimal": "1234" }
447448
, "Binary" : { "$binary": { "base64": "o0w498Or7cijeBSpkquNtg==", "subType": "03" } }
449+
, "BinaryLegacy" : { "$binary": "o0w498Or7cijeBSpkquNtg==", "$type": "03" }
448450
, "BinaryUserDefined" : { "$binary": { "base64": "AQIDBAU=", "subType": "80" } }
449451
, "Code" : { "$code": "function() {}" }
450452
, "CodeWithEmptyScope" : { "$code": "function() {}", "$scope": {} }
@@ -514,6 +516,17 @@ func TestExtJSONParserAllTypes(t *testing.T) {
514516
},
515517
},
516518
},
519+
{
520+
f: expectMultipleValues, p: ejp,
521+
k: "BinaryLegacy", t: bsontype.Binary,
522+
v: &extJSONObject{
523+
keys: []string{"base64", "subType"},
524+
values: []*extJSONValue{
525+
{t: bsontype.String, v: "o0w498Or7cijeBSpkquNtg=="},
526+
{t: bsontype.String, v: "03"},
527+
},
528+
},
529+
},
517530
{
518531
f: expectMultipleValues, p: ejp,
519532
k: "BinaryUserDefined", t: bsontype.Binary,

0 commit comments

Comments
 (0)