From 980c889f8d9efe660ad6dc26698b1d9ba28bab99 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sat, 19 Feb 2022 14:26:34 +0200 Subject: [PATCH] minor fix --- context/context.go | 36 +++++++++++++++++++++++++++++------- go.mod | 1 - go.sum | 2 -- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/context/context.go b/context/context.go index 5c457b8c..7dea4f45 100644 --- a/context/context.go +++ b/context/context.go @@ -2371,7 +2371,18 @@ type internalJSONDecoder interface { DisallowUnknownFields() } -func (options JSONReader) getDecoder(r io.Reader) (internalJSONDecoder, DecodeFunc) { +type unmarshalerContext interface { + // UnmarshalJSON unmarshal json with context support. + UnmarshalJSON(stdContext.Context, []byte) error //lint:ignore stdmethods external pkg. +} + +func wrapDecodeFunc(decodeFunc func(interface{}) error) DecodeFunc { + return func(_ stdContext.Context, outPtr interface{}) error { + return decodeFunc(outPtr) + } +} + +func (options JSONReader) getDecoder(r io.Reader, outPtr interface{}) (internalJSONDecoder, DecodeFunc) { var ( decoder internalJSONDecoder decodeFunc DecodeFunc @@ -2379,13 +2390,24 @@ func (options JSONReader) getDecoder(r io.Reader) (internalJSONDecoder, DecodeFu if options.Optimize { dec := gojson.NewDecoder(r) - decodeFunc = dec.DecodeContext + + if outPtr != nil { + // If a custom type does not implement the unnmarshal json with context interface + // that is REQUIRED by the gojson, then fallback to the normal gojson decode without context support, + // so we protect compability against existing objects. + if _, supportsContext := outPtr.(unmarshalerContext); supportsContext { + decodeFunc = dec.DecodeContext + } else { + decodeFunc = wrapDecodeFunc(dec.Decode) + } + } else { + decodeFunc = dec.DecodeContext + } + decoder = dec } else { dec := json.NewDecoder(r) - decodeFunc = func(_ stdContext.Context, outPtr interface{}) error { - return dec.Decode(outPtr) - } + decodeFunc = wrapDecodeFunc(dec.Decode) decoder = dec } @@ -2407,7 +2429,7 @@ func (ctx *Context) ReadJSON(outPtr interface{}, opts ...JSONReader) error { options = opts[0] } - _, decodeFunc := options.getDecoder(ctx.request.Body) + _, decodeFunc := options.getDecoder(ctx.request.Body, outPtr) return decodeFunc(ctx.request.Context(), outPtr) /* @@ -2440,7 +2462,7 @@ func (ctx *Context) ReadJSONStream(onDecode func(DecodeFunc) error, opts ...JSON options = opts[0] } - decoder, decodeFunc := options.getDecoder(ctx.request.Body) + decoder, decodeFunc := options.getDecoder(ctx.request.Body, nil) if options.ArrayStream { _, err := decoder.Token() // read open bracket. diff --git a/go.mod b/go.mod index 393243c1..5f7dbcd4 100644 --- a/go.mod +++ b/go.mod @@ -86,7 +86,6 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect github.com/sergi/go-diff v1.1.0 // indirect - github.com/shiyanhui/hero v0.0.2 // indirect github.com/smartystreets/goconvey v1.7.2 // indirect github.com/stretchr/testify v1.7.0 // indirect github.com/tdewolff/parse/v2 v2.5.27 // indirect diff --git a/go.sum b/go.sum index 9a1c222c..709bec83 100644 --- a/go.sum +++ b/go.sum @@ -209,8 +209,6 @@ github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/shirou/gopsutil/v3 v3.22.1 h1:33y31Q8J32+KstqPfscvFwBlNJ6xLaBy4xqBXzlYV5w= github.com/shirou/gopsutil/v3 v3.22.1/go.mod h1:WapW1AOOPlHyXr+yOyw3uYx36enocrtSoSBy0L5vUHY= -github.com/shiyanhui/hero v0.0.2 h1:RF8fwiIeWbVsdki8LCS905pxLjCQbOz/NcKE0g1ZOJc= -github.com/shiyanhui/hero v0.0.2/go.mod h1:aBlyf5bmklQfvOmVQm5i04lIGFY7t2QYIJdqEMNGJZM= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs= github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=