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
lfox7 opened this issue Oct 20, 2017 · 4 comments

Comments

Projects
None yet
6 participants
@lfox7
Copy link

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

This comment has been minimized.

Copy link

commented Oct 27, 2018

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

@cuonglm

This comment has been minimized.

Copy link
Contributor

commented Nov 6, 2018

@mvdan Can you take a look at newest change?

@agnivade

This comment has been minimized.

Copy link
Member

commented Dec 24, 2018

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

@mvdan

This comment has been minimized.

Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.