Skip to content

encoding/json: nested unmarshaling mangles errors #68750

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
dominikh opened this issue Aug 6, 2024 · 6 comments
Closed

encoding/json: nested unmarshaling mangles errors #68750

dominikh opened this issue Aug 6, 2024 · 6 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@dominikh
Copy link
Member

dominikh commented Aug 6, 2024

Go version

go version devel go1.24-96f7159705 Tue Jul 30 20:12:39 2024 +0000 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/dominikh/.cache/go-build'
GOENV='/home/dominikh/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/dominikh/prj/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/dominikh/prj'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/dominikh/prj/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/dominikh/prj/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='devel go1.24-96f7159705 Tue Jul 30 20:12:39 2024 +0000'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/dominikh/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/dominikh/prj/src/example.com/go.mod'
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 -ffile-prefix-map=/tmp/nix-shell.pip13R/go-build4163698412=/tmp/go-build -gno-record-gcc-switches'

What did you do?

https://play.golang.org/p/Ypv2xaFDAOs

package main

import (
	"encoding/json"
	"fmt"
)

type Foo struct {
	Bar Bar
}

type Bar struct{}

func (*Bar) UnmarshalJSON(data []byte) error {
	var dst struct {
		K int `json:"k"`
	}
	err := json.Unmarshal(data, &dst)
	fmt.Println(err)
	return err
}

func main() {
	var f Foo
	fmt.Println(json.Unmarshal([]byte(`{"Bar": {"k": "v"}}`), &f))
}

What did you see happen?

json: cannot unmarshal string into Go struct field .k of type int
json: cannot unmarshal string into Go struct field Foo.Bar of type int

What did you expect to see?

A less misleading error returned by the call to json.Unmarshal in main. The type of Foo.Bar is not int and we no longer know where the error actually happened.

@dominikh
Copy link
Member Author

dominikh commented Aug 6, 2024

Thanks bot. This is #61337, which was closed as a duplicate of #11858. I disagree with that decision. While they're related by what causes them, they're two different problems, and this issue will probably be easier to fix than #11858.

@dr2chase
Copy link
Contributor

dr2chase commented Aug 6, 2024

@dsnet @mvdan

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 12, 2024
@cagedmantis
Copy link
Contributor

cc @rsc

@j2gg0s
Copy link
Contributor

j2gg0s commented Aug 14, 2024

We should join err.Field with d.errContext.FieldContext if err.Field is not empty?

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/605455 mentions this issue: encoding/json: optimize UnmarshalTypeError's field stack for nested unmarshal

j2gg0s added a commit to j2gg0s/go that referenced this issue Aug 14, 2024
When people return UnmarshalTypeError in UnmarshalJSON,
we should append error's Field to FieldStack.

Fixes golang#68750
j2gg0s added a commit to j2gg0s/go that referenced this issue Aug 15, 2024
When people return UnmarshalTypeError in UnmarshalJSON,
we should append error's Field to FieldStack.

Fixes golang#68750
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants