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

issue: nil pointer of struct which has supported-type-fields returns error #554

Open
AsterDY opened this issue Nov 8, 2023 · 0 comments
Labels
known-issue This issue is known to us, we are working on it

Comments

@AsterDY
Copy link
Collaborator

AsterDY commented Nov 8, 2023

reason

  • *http.Request has unsupported type fields like GetBody func() (io.ReadCloser, error), but the passed obj is nil, thus std think it is a nil pointer and encode it as nothing, while sonic report error when compiling the ptr type

codes

import (
	"encoding/json"
	"net/http"
	"testing"

	"github.com/bytedance/sonic"
	jsoniter "github.com/json-iterator/go"
	"github.com/stretchr/testify/assert"
)

type Context struct {
	*http.Request
}


func TestContext(t *testing.T) {
	var obj = new(Context)
	out, err := sonic.Marshal(obj)
	out1, err1 := jsoniter.Marshal(obj)
	out2, err2 := json.Marshal(obj)
	println(string(out2))
	assert.Equal(t, err2 ==nil, err1 ==nil)
	assert.Equal(t, err2 ==nil, err ==nil)
	assert.Equal(t, out2, out1)
	assert.Equal(t, out2, out)
	// err = sonic.Unmarshal(out, obj)
	// if err != nil {
	// 	t.Fatal(err)
	// }
}

result

--- FAIL: TestContext (0.02s)
        	Error:      	Not equal: 
        	            	expected: true
        	            	actual  : false
        	Test:       	TestContext
        	Error:      	Not equal: 
        	            	expected: []byte{0x7b, 0x7d}
        	            	actual  : []byte(nil)
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1,4 +1,2 @@
        	            	-([]uint8) (len=2) {
        	            	- 00000000  7b 7d                                             |{}|
        	            	-}
        	            	+([]uint8) <nil>
        	            	 
        	Test:       	TestContext
FAIL
@AsterDY AsterDY added the known-issue This issue is known to us, we are working on it label Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
known-issue This issue is known to us, we are working on it
Projects
None yet
Development

No branches or pull requests

1 participant