From e7f3a0ebbe2ba473054762b0dc05309380004aea Mon Sep 17 00:00:00 2001 From: Stuart Innes <37293383+obashistu@users.noreply.github.com> Date: Sun, 18 Nov 2018 19:27:57 +0000 Subject: [PATCH] Update json.go Current it is not possible to tell the default JSON binder to use DisallowUnknownFields (supported by jsoniter and default Go json decoder). Use package variable EnableDecoderKnownFieldsOnly as a switch. --- binding/json.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/binding/json.go b/binding/json.go index 310922c1..da92cb9d 100644 --- a/binding/json.go +++ b/binding/json.go @@ -17,6 +17,10 @@ import ( // interface{} as a Number instead of as a float64. var EnableDecoderUseNumber = false +// EnableDecoderKnownFieldsOnly is used to toggle DisallowUnknownFields +// which is disabled by default +var EnableDecoderKnownFieldsOnly = false + type jsonBinding struct{} func (jsonBinding) Name() string { @@ -36,6 +40,9 @@ func decodeJSON(r io.Reader, obj interface{}) error { if EnableDecoderUseNumber { decoder.UseNumber() } + if EnableDecoderKnownFieldsOnly { + decoder.DisallowUnknownFields() + } if err := decoder.Decode(obj); err != nil { return err }