Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: omitempy regard non-empty-type interface as empty #539

Closed
AsterDY opened this issue Oct 18, 2023 · 1 comment · Fixed by #540
Closed

bug: omitempy regard non-empty-type interface as empty #539

AsterDY opened this issue Oct 18, 2023 · 1 comment · Fixed by #540

Comments

@AsterDY
Copy link
Collaborator

AsterDY commented Oct 18, 2023

Reproducible codes

type OmitEmptyInterface struct {
    ErrCode int32       `json:"code"`
    Data    interface{} `json:"data,omitempty"`
}

func TestOmitEmptyInterface(t *testing.T) {
	// non-enmpty type
	var data *string
    resp := &OmitEmptyInterface{
       ErrCode: 123,
       Data:    data,
    }
    eout, eerr := json.Marshal(resp)
	sout, serr := sonic.Marshal(resp)
	require.Equal(t, eerr == nil, serr == nil)
	require.Equal(t, string(eout), string(sout))

	// empty type and value
	resp = &OmitEmptyInterface{
		ErrCode: 123,
		Data:    nil,
	 }
	 eout, eerr = json.Marshal(resp)
	 sout, serr = sonic.Marshal(resp)
	 require.Equal(t, eerr == nil, serr == nil)
	 require.Equal(t, string(eout), string(sout))
}

output

--- FAIL: TestOmitEmptyInterface (0.00s)
    /Users/admin/GOPATH/work/sonic/issue_test/issuexx_test.go:40: 
        	Error Trace:	/Users/admin/GOPATH/work/sonic/issue_test/issuexx_test.go:40
        	Error:      	Not equal: 
        	            	expected: "{\"code\":123,\"data\":null}"
        	            	actual  : "{\"code\":123}"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-{"code":123,"data":null}
        	            	+{"code":123}
        	Test:       	TestOmitEmptyInterface

Explaination

sonic check if a interface is empty using second field emptyInterface.word, while std lib using first field emptyInterface.typ

@AsterDY
Copy link
Collaborator Author

AsterDY commented Oct 18, 2023

#540

@AsterDY AsterDY closed this as completed Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant