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

encoding/json: UnmarshalTypeError is clobbered when using nested custom marshallers #61337

Closed
firelizzard18 opened this issue Jul 13, 2023 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@firelizzard18
Copy link
Contributor

firelizzard18 commented Jul 13, 2023

Summary

(*encoding.json.decodeState).addErrorContext sets UnmarshalTypeError's Field value based on the decode state's error context. However when a custom UnmarshalJSON implementation calls json.Unmarshal, this creates a new nested decode state with its own error context. When the nested unmarshal call returns an error, and that error is seen by the original decode state, the original decode state's error context's field stack is empty. Thus when the original decode state sets the error's Field value, it clobbers the field name from the nested call.

What version of Go are you using (go version)?

$ go version
go version go1.20.5 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="$HOME/.cache/go-build"
GOENV="$HOME/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="$HOME/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="$HOME/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="x86_64-pc-linux-gnu-gcc"
CXX="x86_64-pc-linux-gnu-g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build382302808=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://go.dev/play/p/Z1X4MEFdocS

type Alpha struct {
	Beta Beta `json:"beta"`
}

type Beta struct {
	Field string `json:"field"`
}

func (a *Alpha) UnmarshalJSON(data []byte) error {
	type AlphaWrapper Alpha
	if err := json.Unmarshal(data, (*AlphaWrapper)(a)); err != nil {
		return err
	}
	// Do some extra work
	return nil
}

func (b *Beta) UnmarshalJSON(data []byte) error {
	type BetaWrapper Beta
	if err := json.Unmarshal(data, (*BetaWrapper)(b)); err != nil {
		return err
	}
	// Do some extra work
	return nil
}

func TestJsonBug(t *testing.T) {
	var a Alpha
	err := json.Unmarshal([]byte(`{ "beta": { "field": 1 } }`), &a)
	const expect = "json: cannot unmarshal number into Go struct field BetaWrapper.beta.field of type string"
	if err.Error() != expect {
		t.Fatalf("Bad error\nExpected: %s\nGot:      %s", expect, err.Error())
	}
}

What did you expect to see?

json: cannot unmarshal number into Go struct field BetaWrapper.beta.field of type string

What did you see instead?

json: cannot unmarshal number into Go struct field AlphaWrapper.beta of type string

@ayang64
Copy link
Member

ayang64 commented Jul 13, 2023

You should probably update the What did you see instead? section with:

json: cannot unmarshal number into Go struct field AlphaWrapper.beta of type string

@seankhliao seankhliao added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 14, 2023
@cherrymui cherrymui added this to the Backlog milestone Jul 14, 2023
@cherrymui
Copy link
Member

cc @rsc @dsnet @bradfitz @mvdan

@seankhliao
Copy link
Member

Duplicate of #11858

@seankhliao seankhliao marked this as a duplicate of #11858 Jul 19, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jul 19, 2023
@golang golang locked and limited conversation to collaborators Jul 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants