encoding/json: merge FieldStack if the error's Field exists.

When people return UnmarshalTypeError in UnmarshalJSON, we should append error's Field to FieldStack.

Fixes #68750

Change-Id: I0a5a9b259a1b569de1bebc815ec936c913e10469
GitHub-Last-Rev: 18796addc3
GitHub-Pull-Request: golang/go#68870
Reviewed-on: https://go-review.googlesource.com/c/go/+/605455
Reviewed-by: Carlos Amedee <carlos@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
j2gg0s 2024-08-15 02:11:44 +00:00 committed by Gopher Robot
parent 970b1c042c
commit bd8977be0c
2 changed files with 21 additions and 1 deletions

View file

@ -255,7 +255,11 @@ func (d *decodeState) addErrorContext(err error) error {
switch err := err.(type) {
case *UnmarshalTypeError:
err.Struct = d.errorContext.Struct.Name()
err.Field = strings.Join(d.errorContext.FieldStack, ".")
fieldStack := d.errorContext.FieldStack
if err.Field != "" {
fieldStack = append(fieldStack, err.Field)
}
err.Field = strings.Join(fieldStack, ".")
}
}
return err