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

Comments

@ailsky
Copy link

@ailsky 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 label Oct 20, 2017
@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Oct 20, 2017
@gopherbot
Copy link

@gopherbot gopherbot commented Oct 27, 2018

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

@cuonglm
Copy link
Contributor

@cuonglm cuonglm commented Nov 6, 2018

@mvdan Can you take a look at newest change?

@agnivade
Copy link
Contributor

@agnivade agnivade commented Dec 24, 2018

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

@mvdan
Copy link
Member

@mvdan 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.

@gopherbot gopherbot closed this in 29bc4f1 Mar 5, 2019
@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.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.