Skip to content

Commit

Permalink
fix: decoderx dropped errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alrs committed Nov 9, 2023
1 parent acad8b0 commit 79bfeb5
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions decoderx/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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()).
Expand All @@ -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{}:
Expand All @@ -456,20 +458,20 @@ 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()).
WithDetail("name", key).
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)
Expand All @@ -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)
Expand Down

0 comments on commit 79bfeb5

Please sign in to comment.