[GODRIVER-2017] UnmarshalExtJSON with error Created: 20/May/21  Updated: 28/Oct/23  Resolved: 28/May/21

Status: Closed
Project: Go Driver
Component/s: JSON & ExtJSON
Affects Version/s: None
Fix Version/s: 1.5.3

Type: Bug Priority: Minor - P4
Reporter: 杰 王 Assignee: Benji Rewis (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes: Not Needed

 Comments   
Comment by Benji Rewis (Inactive) [ 28/May/21 ]

feys1992@gmail.com this fix is now merged and should available in the next patch of the Go driver.

Comment by Githook User [ 28/May/21 ]

Author:

{'name': 'Benjamin Rewis', 'email': '32186188+benjirewis@users.noreply.github.com', 'username': 'benjirewis'}

Message: GODRIVER-2017 Skip empty extJSON objects correctly in Unmarshal (#677)
Branch: release/1.5
https://github.com/mongodb/mongo-go-driver/commit/5c6874b17171700111e5c51f943c88e35dd0dee0

Comment by Githook User [ 28/May/21 ]

Author:

{'name': 'Benjamin Rewis', 'email': '32186188+benjirewis@users.noreply.github.com', 'username': 'benjirewis'}

Message: GODRIVER-2017 Skip empty extJSON objects correctly in Unmarshal (#677)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/0cd0d3c8e0b1dc249b7638f259d5f9e2a7e7762b

Comment by Benji Rewis (Inactive) [ 26/May/21 ]

Thanks again for the report, feys1992@gmail.com!

We've determined this is a bug. If a field with an empty object ("y" in your case) is undefined in the destination struct and is followed by another object ("rule_group_id" in your case), that following object will be unmarshaled as empty. This was because we were not setting a flag for empty objects back to false when skipping with skipObject.

A fix is in review now: https://github.com/mongodb/mongo-go-driver/pull/677.

Comment by Benji Rewis (Inactive) [ 24/May/21 ]

Hello feys1992@gmail.com! Thanks so much for your report; we're trying to reproduce this error now.

Comment by 杰 王 [ 20/May/21 ]

package mainpackage main
import ( "testing"
"github.com/stretchr/testify/require" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive")
func TestUnmarshalExtJSON(t *testing.T) { doc := `{"_id":

{"$oid":"5ff5b89ecbc9590001210d5b"}

,"x":"1006007-154305-18344992668-17136650599","y":{},"z":0,"rule_group_id":{"$oid":"5ff599d4fa3f7f0012d5d3d0"},"m":""}`
t.Run("ignore field y", func(t *testing.T) { foo := struct

{ ID          primitive.ObjectID `bson:"_id"` RuleGroupID primitive.ObjectID `bson:"rule_group_id"` }

{} err := bson.UnmarshalExtJSON([]byte(doc), false, &foo) require.NoError(t, err) require.Equal(t, "5ff5b89ecbc9590001210d5b", foo.ID.Hex()) require.Equal(t, "000000000000000000000000", foo.RuleGroupID.Hex()) })
t.Run("with field y", func(t *testing.T) { foo := struct { ID          primitive.ObjectID `bson:"_id"` RuleGroupID primitive.ObjectID `bson:"rule_group_id"` Y           interface{}        `bson:"y"` }{} err := bson.UnmarshalExtJSON([]byte(doc), false, &foo) require.NoError(t, err) require.Equal(t, "5ff5b89ecbc9590001210d5b", foo.ID.Hex()) require.Equal(t, "5ff599d4fa3f7f0012d5d3d0", foo.RuleGroupID.Hex()) })}

Generated at Thu Feb 08 08:37:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.