-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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: show nested fields path if DisallowUnknownFields leads to an error #58649
Comments
Any thoughts on this one? I have hit the same issue, where a failure from |
I still didn’t solve the issue, and I believe it can just be implemented straight forward without any further refactoring. I can invest time in implementing it, this is only a matter whether the feature will be accepted or not. |
… is set Fixes golang#58649 without relying on the mentioned refactoring/cleaning in golang#43126 With most efforts redirected towards encoding/json v2. I don't think golang#43126 will ever be addressed in v1. I don't think we should consider it as a requirement for this patch. This could have been added as part of golang@2596a0c encoding/json v2 is moving in the same direction by adding context by default as shown in https://github.com/go-json-experiment/json/blob/4e0381018ad68adc9c0cb7896d109c994429654e/errors.go#L364-L373 But we don't need to wait for v2 to become stable to improve UX and and save users some time, and help them keep their hair!
I opened #71638 |
Change https://go.dev/cl/648055 mentions this issue: |
Hello, Go community!
Problem
I tried to utilize the
DisallowUnknownFields
option forjson.Decoder
and faced a problem. It is not possible get the path of the problematic field from the error.Example:
The structure
and the following config
on decoding will throw an error
json: unknown field "unknown_token"
, yet it is not clear exactly which field caused it.https://go.dev/play/p/zzLCOBn3AI8
If the config is a little, it is fine to find the field, but it is not suitable to validate cumbersome configs.
Proposal
Decoder already has all the info. The only thing left is to return it to a user.
The following block
go/src/encoding/json/decode.go
Lines 747 to 749 in 06b6759
should be rewritten like this
or (to avoid Join)
The text was updated successfully, but these errors were encountered: