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

proposal: encoding/json: require all fields #44138

jaykam opened this issue Feb 6, 2021 · 3 comments

proposal: encoding/json: require all fields #44138

jaykam opened this issue Feb 6, 2021 · 3 comments


Copy link

jaykam commented Feb 6, 2021

Hi @rsc @adg

I've noticed that encoding/json doesn't raise an error if any field is not present in the body payload.


type DecodeRequest struct{
     ID int   `json:"id"`
     Name string `json:"name"`

func DecodeRequest(g *gin.Context) error{
       var decodeRq DecodeRequest
       dec := json.NewDecoder(g.Request.Body)   //Body payload : {"id" : 123}
       err := dec.Decode(&decodeRq)
       fmt.Println(err) -> nil

Ideally, this function should give an error like missingfield name.

DisallowUnknownFields just take care of extra fields but not the missing field.

Golang version: 1.15.6

Let me know if I've missed anything to fix it?

Copy link

see also #17163 #19858

@seankhliao seankhliao changed the title encoding/json: do not raise an error if any field is missing from the defined struct proposal: encoding/json: require all fields Feb 6, 2021
@gopherbot gopherbot added this to the Proposal milestone Feb 6, 2021
Copy link

jaykam commented Feb 7, 2021

@seankhliao Thanks for the reply.
I see that this proposal is already on hold.
When can we expect this feature in the next release?


@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Feb 7, 2021
Copy link

mvdan commented Feb 8, 2021

Yes, this is a duplicate of #19858. We can't provide timeline estimates, sorry - taking a look at all JSON proposals is a big chunk of ongoing work.

@mvdan mvdan closed this as completed Feb 8, 2021
@rsc rsc moved this from Incoming to Declined in Proposals (old) Feb 10, 2021
@golang golang locked and limited conversation to collaborators Feb 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
No open projects

No branches or pull requests

4 participants