From 79bfeb5badc6471984bae20801219b768e9f208a Mon Sep 17 00:00:00 2001 From: Lars Lehtonen Date: Tue, 31 Oct 2023 09:29:48 -0700 Subject: [PATCH] fix: decoderx dropped errors --- decoderx/http.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/decoderx/http.go b/decoderx/http.go index 1debd2ec..e9916fe9 100644 --- a/decoderx/http.go +++ b/decoderx/http.go @@ -399,6 +399,8 @@ func (t *HTTP) decodeForm(r *http.Request, destination interface{}, o *httpDecod } func (t *HTTP) decodeURLValues(values url.Values, paths []jsonschemax.Path, o *httpDecoderOptions) (json.RawMessage, error) { + var f float64 + var b bool raw := json.RawMessage(`{}`) for key := range values { for _, path := range paths { @@ -409,7 +411,7 @@ func (t *HTTP) decodeURLValues(values url.Values, paths []jsonschemax.Path, o *h raw, err = sjson.SetBytes(raw, path.Name, values[key]) case []float64: for k, v := range values[key] { - if f, err := strconv.ParseFloat(v, 64); err != nil { + if f, err = strconv.ParseFloat(v, 64); err != nil { switch o.handleParseErrors { case ParseErrorIgnoreConversionErrors: raw, err = sjson.SetBytes(raw, path.Name+"."+strconv.Itoa(k), v) @@ -428,12 +430,12 @@ func (t *HTTP) decodeURLValues(values url.Values, paths []jsonschemax.Path, o *h } case []bool: for k, v := range values[key] { - if f, err := strconv.ParseBool(v); err != nil { + if b, err = strconv.ParseBool(v); err != nil { switch o.handleParseErrors { case ParseErrorIgnoreConversionErrors: raw, err = sjson.SetBytes(raw, path.Name+"."+strconv.Itoa(k), v) case ParseErrorUseEmptyValueOnConversionErrors: - raw, err = sjson.SetBytes(raw, path.Name+"."+strconv.Itoa(k), f) + raw, err = sjson.SetBytes(raw, path.Name+"."+strconv.Itoa(k), b) case ParseErrorReturnOnConversionErrors: return nil, errors.WithStack(herodot.ErrBadRequest.WithReasonf("Expected value to be a boolean."). WithDetail("parse_error", err.Error()). @@ -442,7 +444,7 @@ func (t *HTTP) decodeURLValues(values url.Values, paths []jsonschemax.Path, o *h WithDetail("value", v)) } } else { - raw, err = sjson.SetBytes(raw, path.Name+"."+strconv.Itoa(k), f) + raw, err = sjson.SetBytes(raw, path.Name+"."+strconv.Itoa(k), b) } } case []interface{}: @@ -456,12 +458,12 @@ func (t *HTTP) decodeURLValues(values url.Values, paths []jsonschemax.Path, o *h v = "false" } - if f, err := strconv.ParseBool(v); err != nil { + if b, err = strconv.ParseBool(v); err != nil { switch o.handleParseErrors { case ParseErrorIgnoreConversionErrors: raw, err = sjson.SetBytes(raw, path.Name, v) case ParseErrorUseEmptyValueOnConversionErrors: - raw, err = sjson.SetBytes(raw, path.Name, f) + raw, err = sjson.SetBytes(raw, path.Name, b) case ParseErrorReturnOnConversionErrors: return nil, errors.WithStack(herodot.ErrBadRequest.WithReasonf("Expected value to be a boolean."). WithDetail("parse_error", err.Error()). @@ -469,7 +471,7 @@ func (t *HTTP) decodeURLValues(values url.Values, paths []jsonschemax.Path, o *h WithDetail("value", values.Get(key))) } } else { - raw, err = sjson.SetBytes(raw, path.Name, f) + raw, err = sjson.SetBytes(raw, path.Name, b) } case float64: v := values.Get(key) @@ -480,7 +482,7 @@ func (t *HTTP) decodeURLValues(values url.Values, paths []jsonschemax.Path, o *h v = "0.0" } - if f, err := strconv.ParseFloat(v, 64); err != nil { + if f, err = strconv.ParseFloat(v, 64); err != nil { switch o.handleParseErrors { case ParseErrorIgnoreConversionErrors: raw, err = sjson.SetBytes(raw, path.Name, v)