Skip to content

Commit 684be53

Browse files
author
Divjot Arora
committed
Fix parsing of empty objects in EXT JSON
GODRIVER-630 Co-authored-by: Matthew Chiaravalloti <[email protected]> Co-authored-by: Divjot Arora <[email protected]> Change-Id: I718d50c318e45734b8aba7c08cc841ab4dbb81a4
1 parent 10afe00 commit 684be53

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

bson/bsonrw/extjson_parser.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ type extJSONParser struct {
6464
canonical bool
6565
depth int
6666
maxDepth int
67+
68+
emptyObject bool
6769
}
6870

6971
// newExtJSONParser returns a new extended JSON parser, ready to to begin
@@ -107,6 +109,7 @@ func (ejp *extJSONParser) peekType() (bsontype.Type, error) {
107109
switch ejp.s {
108110
case jpsSawEndObject: // empty embedded document
109111
t = bsontype.EmbeddedDocument
112+
ejp.emptyObject = true
110113
case jpsInvalidState:
111114
err = ejp.err
112115
case jpsSawKey:
@@ -143,6 +146,11 @@ func (ejp *extJSONParser) peekType() (bsontype.Type, error) {
143146

144147
// readKey parses the next key and its type and returns them
145148
func (ejp *extJSONParser) readKey() (string, bsontype.Type, error) {
149+
if ejp.emptyObject {
150+
ejp.emptyObject = false
151+
return "", 0, ErrEOD
152+
}
153+
146154
// advance to key (or return with error)
147155
switch ejp.s {
148156
case jpsStartState:

bson/bsonrw/extjson_parser_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ func TestExtJSONParserAllTypes(t *testing.T) {
447447
, "Code" : { "$code": "function() {}" }
448448
, "CodeWithEmptyScope" : { "$code": "function() {}", "$scope": {} }
449449
, "CodeWithScope" : { "$code": "function() {}", "$scope": { "x": 1 } }
450+
, "EmptySubdocument" : {}
450451
, "Subdocument" : { "foo": "bar", "baz": { "$numberInt": "42" } }
451452
, "Array" : [{"$numberInt": "1"}, {"$numberLong": "2"}, {"$numberDouble": "3"}, 4, 5.0]
452453
, "Timestamp" : { "$timestamp": { "t": 42, "i": 1 } }
@@ -544,6 +545,13 @@ func TestExtJSONParserAllTypes(t *testing.T) {
544545
},
545546
},
546547
},
548+
{
549+
f: expectSubDocument, p: ejp,
550+
k: "EmptySubdocument", t: bsontype.EmbeddedDocument,
551+
v: ejpSubDocumentTestValue{
552+
ktvs: []ejpKeyTypValTriple{},
553+
},
554+
},
547555
{
548556
f: expectSubDocument, p: ejp,
549557
k: "Subdocument", t: bsontype.EmbeddedDocument,

0 commit comments

Comments
 (0)