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: add the full path to the field in UnmarshalTypeError #22369

Closed
ailsky opened this issue Oct 20, 2017 · 4 comments
Closed

encoding/json: add the full path to the field in UnmarshalTypeError #22369

ailsky opened this issue Oct 20, 2017 · 4 comments
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done. Suggested Issues that may be good for new contributors looking for work to do.
Milestone

Comments

@ailsky
Copy link

ailsky commented Oct 20, 2017

go1.9.1 linux/amd64

I parse some JSON with nested objects on the server, and I cannot find the actual path to a nested field in UnmarshalTypeError. An example below.

Body payload:

{
	"Child": {
		"Field": "string" <--- here's the  invalid type
	}
}
type ParentStruct struct {
  Child ChildStruct
}

type ChildStruct struct {
  Field int
}

d := Parent{}

decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&d)

What I expect to see

s

What I see instead

s

Such field like Path may be used to render detailed errors for some REST API.

@mvdan mvdan changed the title encoding/json add the actual path to the field to UnmarshalTypeError encoding/json: add the full path to the field in UnmarshalTypeError Oct 20, 2017
@mvdan mvdan added the Suggested Issues that may be good for new contributors looking for work to do. label Oct 20, 2017
@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Oct 20, 2017
@mvdan mvdan added help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Oct 26, 2018
@gopherbot
Copy link

Change https://golang.org/cl/145218 mentions this issue: encoding/json: add Path to UnmarshalTypeError

@cuonglm
Copy link
Member

cuonglm commented Nov 6, 2018

@mvdan Can you take a look at newest change?

@agnivade
Copy link
Contributor

@mvdan - Is this same as #27464 or different ?

@mvdan
Copy link
Member

mvdan commented Dec 25, 2018

Different - #27464 was about a regression where an input's error no longer had the struct context. This is about improving the context to also include the full path to the field.

@golang golang locked and limited conversation to collaborators Mar 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted NeedsFix The path to resolution is known, but the work has not been done. Suggested Issues that may be good for new contributors looking for work to do.
Projects
None yet
Development

No branches or pull requests

6 participants