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] can't skip mismached-type value when it is a ,string-tagged field #686

Open
AsterDY opened this issue Aug 6, 2024 · 0 comments
Open

Comments

@AsterDY
Copy link
Collaborator

AsterDY commented Aug 6, 2024

Reproducible codes

  • codes
func TestIssueStringTagMismatchSkip_decode(t *testing.T) {
	// match
	eo := []byte(`{"D":"20","E":1}`)
	et := reflect.TypeOf(IssueStringTag{})
	testUnmarshal(t, eo, et, true)

	// mismatch
	eo = []byte(`{"D":11,"E":1}`)
	testUnmarshal(t, eo, et, true)

	// null
	eo = []byte(`{"D":null,"E":1}`)
	testUnmarshal(t, eo, et, true)
}

func testUnmarshal(t *testing.T, eo []byte, rt reflect.Type, checkobj bool) {
	obj := reflect.New(rt).Interface()
	println(string(eo))
	println("sonic")
	es := sonic.Unmarshal(eo, obj)
	obj2 := reflect.New(rt).Interface()
	println("std")
	ee := json.Unmarshal(eo, obj2)
	assert.Equal(t, ee ==nil, es == nil, es)
	if checkobj {
		assert.Equal(t, obj2, obj)
	}
	fmt.Printf("std: %v, obj: %#v", ee, obj2)
	fmt.Printf("sonic error: %v, obj: %#v", es, obj)
}


type IssueStringTag struct {
	D      int                 `form:"D" json:"D,string" query:"D"`
	E      int
}
  • error
                  Error:      	Not equal: 
        	            	expected: &issue_test.IssueStringTag{D:0, E:1}
        	            	actual  : &issue_test.IssueStringTag{D:0, E:0}
        	            	
        	            	Diff:
        	            	--- Expected
        	            	    Actual
        	            	@@ -2,3  2,3 @@
        	            	  D: (int) 0,
        	            	- E: (int) 1
        	            	  E: (int) 0
        	            	 })
        	Test:       	TestIssueStringTagMismatchSkip_decode
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

No branches or pull requests

1 participant