Skip to content

Commit

Permalink
GODRIVER-963 canonical parameter is not respected in UnmarshalExtJSON (
Browse files Browse the repository at this point in the history
  • Loading branch information
joyjwang authored Sep 6, 2024
1 parent a4a5bc3 commit 7910023
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
24 changes: 12 additions & 12 deletions bson/extjson_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ type extJSONParser struct {
k string
v *extJSONValue

err error
canonical bool
depth int
maxDepth int
err error
canonicalOnly bool
depth int
maxDepth int

emptyObject bool
relaxedUUID bool
Expand All @@ -74,13 +74,13 @@ type extJSONParser struct {
// parsing from the first character of the argued json input. It will not
// perform any read-ahead and will therefore not report any errors about
// malformed JSON at this point.
func newExtJSONParser(r io.Reader, canonical bool) *extJSONParser {
func newExtJSONParser(r io.Reader, canonicalOnly bool) *extJSONParser {
return &extJSONParser{
js: &jsonScanner{r: r},
s: jpsStartState,
m: []jsonParseMode{},
canonical: canonical,
maxDepth: maxNestingDepth,
js: &jsonScanner{r: r},
s: jpsStartState,
m: []jsonParseMode{},
canonicalOnly: canonicalOnly,
maxDepth: maxNestingDepth,
}
}

Expand Down Expand Up @@ -413,12 +413,12 @@ func (ejp *extJSONParser) readValue(t Type) (*extJSONValue, error) {
}
v = &extJSONValue{t: TypeEmbeddedDocument, v: &extJSONObject{keys: keys, values: vals}}
case jpsSawValue:
if ejp.canonical {
if ejp.canonicalOnly {
return nil, invalidJSONError("{")
}
v = ejp.v
default:
if ejp.canonical {
if ejp.canonicalOnly {
return nil, invalidJSONErrorForType("object", t)
}
return nil, invalidJSONErrorForType("ISO-8601 Internet Date/Time Format as described in RFC-3339", t)
Expand Down
18 changes: 9 additions & 9 deletions bson/extjson_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ type extJSONValueReader struct {
frame int
}

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

func newExtJSONValueReader(r io.Reader, canonical bool) (*extJSONValueReader, error) {
func newExtJSONValueReader(r io.Reader, canonicalOnly bool) (*extJSONValueReader, error) {
ejvr := new(extJSONValueReader)
return ejvr.reset(r, canonical)
return ejvr.reset(r, canonicalOnly)
}

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

if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions bson/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,14 @@ func UnmarshalValue(t Type, data []byte, val interface{}) error {
// UnmarshalExtJSON parses the extended JSON-encoded data and stores the result
// in the value pointed to by val. If val is nil or not a pointer, UnmarshalExtJSON
// returns an error.
func UnmarshalExtJSON(data []byte, canonical bool, val interface{}) error {
ejvr, err := NewExtJSONValueReader(bytes.NewReader(data), canonical)
//
// If canonicalOnly is true, UnmarshalExtJSON returns an error if the Extended
// JSON was not marshaled in canonical mode.
//
// For more information about Extended JSON, see
// https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/
func UnmarshalExtJSON(data []byte, canonicalOnly bool, val interface{}) error {
ejvr, err := NewExtJSONValueReader(bytes.NewReader(data), canonicalOnly)
if err != nil {
return err
}
Expand Down

0 comments on commit 7910023

Please sign in to comment.