bytedance / sonic

A blazingly fast JSON serializing & deserializing library
Apache License 2.0
6.59k stars 327 forks source link

bug: `unset()` misbehavior on v1.11.0-x #600

Closed AsterDY closed 5 months ago

AsterDY commented 5 months ago

Reproducible code

// object
    obj := NewRaw("{\"x\":\"a\",\"y\":\"b\"}")
    if ok, err := obj.Unset("x"); !ok || err != nil {
        panic(fmt.Errorf("unset x fail, ok=%v, err=%v", ok, err))
    }
    if ok, err := obj.Unset("y"); !ok || err != nil {
        panic(fmt.Errorf("unset y fail, ok=%v, err=%v", ok, err))
    }
    result, err := obj.MarshalJSON()
    if err != nil {
        panic(fmt.Errorf("MarshalJSON fail: err=%v", err))
    }
    require.Equal(t, `{}`, string(result))

output

--- FAIL: TestIssueX (0.00s)
    /Users/bytedance/GOPATH/work/sonic/ast/node_test.go:1985: 
            Error Trace:    /Users/bytedance/GOPATH/work/sonic/ast/node_test.go:1985
            Error:          Not equal: 
                            expected: "{}"
                            actual  : "{\"y\":\"b\"}"

                            Diff:
                            --- Expected
                            +++ Actual
                            @@ -1 +1 @@
                            -{}
                            +{"y":"b"}
            Test:           TestIssueX

Reason

Node.Unset() calling self.removePair(i) use physical index instead of logic index.