Skip to content

Panic when marshaling embedded aliased primitive #362

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

Closed
ericklaus-wf opened this issue Apr 13, 2022 · 2 comments · Fixed by #366
Closed

Panic when marshaling embedded aliased primitive #362

ericklaus-wf opened this issue Apr 13, 2022 · 2 comments · Fixed by #366
Labels
bug Something isn't working

Comments

@ericklaus-wf
Copy link

When marshaling a struct that has any field and also embeds an aliased primitive, go-json panics. Additionally, if the panic is resolved, the produced bytes are correct, but go-json ignores the aliased primitive when unmarshaling.

func TestEmbeddedPrimitiveAlias(t *testing.T) {
	type AliasedPrimitive int
	type Combiner struct {
		SomeField int
		AliasedPrimitive
	}
	originalCombiner := Combiner{AliasedPrimitive: 7}
	bytes, err := json.Marshal(originalCombiner)
	assertErr(t, err)
	newCombiner := Combiner{}
	err = json.Unmarshal(bytes, &newCombiner)
	assertErr(t, err)
	assertEq(t, "TestEmbeddedPrimitiveAlias", originalCombiner, newCombiner)
}
@ericklaus-wf
Copy link
Author

I can fix the panic and get correct bytes by modifying lastAnonymousFieldCode to add a nil check on firstField.Next, but I've gotten lost trying to find the issue in the Unmarshal path.

@orisano
Copy link
Contributor

orisano commented Apr 25, 2022

Unmarshal path's issue is maybe here:

if !(field.Type.Kind() == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct) && field.Type.Kind() != reflect.Struct {

@goccy goccy added the bug Something isn't working label Apr 28, 2022
orisano added a commit to orisano/go-json that referenced this issue Apr 28, 2022
goccy added a commit that referenced this issue Apr 28, 2022
Fix struct type processing with embedded primitive type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants