Skip to content

Commit 7910023

Browse files
authored
GODRIVER-963 canonical parameter is not respected in UnmarshalExtJSON (#1793)
1 parent a4a5bc3 commit 7910023

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

bson/extjson_parser.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ type extJSONParser struct {
6161
k string
6262
v *extJSONValue
6363

64-
err error
65-
canonical bool
66-
depth int
67-
maxDepth int
64+
err error
65+
canonicalOnly bool
66+
depth int
67+
maxDepth int
6868

6969
emptyObject bool
7070
relaxedUUID bool
@@ -74,13 +74,13 @@ type extJSONParser struct {
7474
// parsing from the first character of the argued json input. It will not
7575
// perform any read-ahead and will therefore not report any errors about
7676
// malformed JSON at this point.
77-
func newExtJSONParser(r io.Reader, canonical bool) *extJSONParser {
77+
func newExtJSONParser(r io.Reader, canonicalOnly bool) *extJSONParser {
7878
return &extJSONParser{
79-
js: &jsonScanner{r: r},
80-
s: jpsStartState,
81-
m: []jsonParseMode{},
82-
canonical: canonical,
83-
maxDepth: maxNestingDepth,
79+
js: &jsonScanner{r: r},
80+
s: jpsStartState,
81+
m: []jsonParseMode{},
82+
canonicalOnly: canonicalOnly,
83+
maxDepth: maxNestingDepth,
8484
}
8585
}
8686

@@ -413,12 +413,12 @@ func (ejp *extJSONParser) readValue(t Type) (*extJSONValue, error) {
413413
}
414414
v = &extJSONValue{t: TypeEmbeddedDocument, v: &extJSONObject{keys: keys, values: vals}}
415415
case jpsSawValue:
416-
if ejp.canonical {
416+
if ejp.canonicalOnly {
417417
return nil, invalidJSONError("{")
418418
}
419419
v = ejp.v
420420
default:
421-
if ejp.canonical {
421+
if ejp.canonicalOnly {
422422
return nil, invalidJSONErrorForType("object", t)
423423
}
424424
return nil, invalidJSONErrorForType("ISO-8601 Internet Date/Time Format as described in RFC-3339", t)

bson/extjson_reader.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ type extJSONValueReader struct {
2626
frame int
2727
}
2828

29-
// NewExtJSONValueReader creates a new ValueReader from a given io.Reader
30-
// It will interpret the JSON of r as canonical or relaxed according to the
31-
// given canonical flag
32-
func NewExtJSONValueReader(r io.Reader, canonical bool) (ValueReader, error) {
33-
return newExtJSONValueReader(r, canonical)
29+
// NewExtJSONValueReader returns a ValueReader that reads Extended JSON values
30+
// from r. If canonicalOnly is true, reading values from the ValueReader returns
31+
// an error if the Extended JSON was not marshaled in canonical mode.
32+
func NewExtJSONValueReader(r io.Reader, canonicalOnly bool) (ValueReader, error) {
33+
return newExtJSONValueReader(r, canonicalOnly)
3434
}
3535

36-
func newExtJSONValueReader(r io.Reader, canonical bool) (*extJSONValueReader, error) {
36+
func newExtJSONValueReader(r io.Reader, canonicalOnly bool) (*extJSONValueReader, error) {
3737
ejvr := new(extJSONValueReader)
38-
return ejvr.reset(r, canonical)
38+
return ejvr.reset(r, canonicalOnly)
3939
}
4040

41-
func (ejvr *extJSONValueReader) reset(r io.Reader, canonical bool) (*extJSONValueReader, error) {
42-
p := newExtJSONParser(r, canonical)
41+
func (ejvr *extJSONValueReader) reset(r io.Reader, canonicalOnly bool) (*extJSONValueReader, error) {
42+
p := newExtJSONParser(r, canonicalOnly)
4343
typ, err := p.peekType()
4444

4545
if err != nil {

bson/unmarshal.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,14 @@ func UnmarshalValue(t Type, data []byte, val interface{}) error {
5858
// UnmarshalExtJSON parses the extended JSON-encoded data and stores the result
5959
// in the value pointed to by val. If val is nil or not a pointer, UnmarshalExtJSON
6060
// returns an error.
61-
func UnmarshalExtJSON(data []byte, canonical bool, val interface{}) error {
62-
ejvr, err := NewExtJSONValueReader(bytes.NewReader(data), canonical)
61+
//
62+
// If canonicalOnly is true, UnmarshalExtJSON returns an error if the Extended
63+
// JSON was not marshaled in canonical mode.
64+
//
65+
// For more information about Extended JSON, see
66+
// https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/
67+
func UnmarshalExtJSON(data []byte, canonicalOnly bool, val interface{}) error {
68+
ejvr, err := NewExtJSONValueReader(bytes.NewReader(data), canonicalOnly)
6369
if err != nil {
6470
return err
6571
}

0 commit comments

Comments
 (0)