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: add error var to compare the returned error when using json.Decoder.DisallowUnknownFields() #29035

Open
jaswdr opened this issue Nov 30, 2018 · 9 comments

Comments

@jaswdr
Copy link

@jaswdr jaswdr commented Nov 30, 2018

What version of Go are you using (go version)?

$ go version

go version devel +ffc7bc55f3 Tue Oct 9 10:35:08 2018 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/jaswdr/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/jaswdr"
GOPROXY=""
GORACE=""
GOROOT="/home/jaswdr/go"
GOTMPDIR=""
GOTOOLDIR="/home/jaswdr/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build749620831=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"log"
	"bytes"
	"encoding/json"
)

type T struct {
	Bar string `json:"bar"`
}

func main() {
	str := []byte(`{"foo":"bar"}`)
	obj := new(T)
	
	dec := json.NewDecoder(bytes.NewReader(str))
	dec.DisallowUnknownFields()
	err := dec.Decode(&obj)
	if err != nil { // Whant to check if the specific unkown fields error happen
		log.Fatal(err)
	}
	
	log.Println(obj)
}

What did you expect to see?

Some way to check if the error happen.

...
if err == json.UnknownFieldsError {
 ...
}
...

What did you see instead?

No way to do this instead of checking the error string


If this is accepted I really want to work on it.

@gopherbot gopherbot added this to the Proposal milestone Nov 30, 2018
@gopherbot gopherbot added the Proposal label Nov 30, 2018
@ianlancetaylor ianlancetaylor changed the title proposal: add error var to compare to the returned error when using json.Decoder.DisallowUnknownFields() proposal: encoding/json: add error var to compare to the returned error when using json.Decoder.DisallowUnknownFields() Nov 30, 2018
@jaswdr jaswdr changed the title proposal: encoding/json: add error var to compare to the returned error when using json.Decoder.DisallowUnknownFields() proposal: encoding/json: add error var to compare the returned error when using json.Decoder.DisallowUnknownFields() Dec 1, 2018
@gopherbot
Copy link

@gopherbot gopherbot commented Dec 3, 2018

Change https://golang.org/cl/152297 mentions this issue: encoding/json: created json.UnknownFieldsError

@andybons
Copy link
Member

@andybons andybons commented Dec 5, 2018

Per discussion with @golang/proposal-review, putting on hold until we can review JSON issues en masse.

@marco-m
Copy link

@marco-m marco-m commented Oct 27, 2019

Hello @andybons, do you have any idea of when you will be able to review these JSON issues en masse? Thanks.

@s3rj1k
Copy link

@s3rj1k s3rj1k commented Dec 10, 2019

Looks absolutely ugly in retrospect to golang 1.13 with new error handling (errors.Is).

@jaswdr
Copy link
Author

@jaswdr jaswdr commented Dec 10, 2019

@s3rj1k please be more objective, this was the best solution in the time when I face this problem, instead of judge, please provide an example using errors.Is, so I can update this PR

@s3rj1k
Copy link

@s3rj1k s3rj1k commented Dec 10, 2019

@jaswdr I was talking about golang core library mess. not your solution :)

@jaswdr
Copy link
Author

@jaswdr jaswdr commented Dec 10, 2019

@s3rj1k ack

@jszroberto
Copy link

@jszroberto jszroberto commented May 29, 2020

Can we revisit this?
In my case, I am trying to detect if clients are sending JSON requests with unknown fields. I don't want to make the request fail, but I want to make it visible by printing some warnings.

Being able to handle the error properly would allow me to achieve that.

@markus-azer
Copy link

@markus-azer markus-azer commented Jun 25, 2020

the only solution i could think about without writing lot of code is

                 var p *entity.Product
		dec := json.NewDecoder(r.Body)
		dec.DisallowUnknownFields() //WARNNING return only one unknown field

		err := dec.Decode(&p)

		switch {
		case err == io.EOF:
			w.WriteHeader(http.StatusBadRequest)
			w.Write([]byte("Provide valid Body"))
			return
		case err != nil && strings.Contains(err.Error(), "json: unknown field"): // if the error string changed within the package then the below case will work `Provide valid JSON`
			m := regexp.MustCompile(`\".+\"`)
			field := m.FindString(err.Error())
			w.WriteHeader(http.StatusBadRequest)
			w.Write([]byte(field + " : Not Allowed"))
			return
		case err != nil:
			w.WriteHeader(http.StatusBadRequest)
			w.Write([]byte("Provide valid JSON"))
			return
		}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

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