Skip to content

Commit fd8148e

Browse files
authored
fix: compare numeric enum values numerically (#1050)
* fix: compare numeric enum values numerically (#1044) Enum validation used type-strict slices.Contains, but the runtime value type depends on its source: JSON bodies decode integers as float64 while query/path params parse as int64. Tag-based enums are stored as float64 (via json.Unmarshal), so an integer enum could never satisfy both paths - query/path params always failed with 'expected value to be one of'. Extract the numeric normalization already inlined in the type check into a shared toFloat64 helper (now also handling json.Number) and use it to compare integer/number enums numerically rather than by Go type. Non numeric enums keep strict comparison. * test: cover json.Number path in numeric validation
1 parent 93b5a32 commit fd8148e

2 files changed

Lines changed: 102 additions & 28 deletions

File tree

‎validate.go‎

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package huma
22

33
import (
4+
"encoding/json"
45
"errors"
56
"fmt"
67
"math"
@@ -373,6 +374,44 @@ func validateDiscriminator(r Registry, s *Schema, path *PathBuffer, mode Validat
373374
Validate(r, r.SchemaFromRef(ref), path, mode, v, res)
374375
}
375376

377+
// toFloat64 normalizes any supported numeric Go type to a float64. The second
378+
// return value is false when v is not a numeric type. This lets numeric
379+
// validation treat values uniformly regardless of whether they came from a
380+
// JSON body (float64), a query/path parameter (int64), or a custom schema.
381+
func toFloat64(v any) (float64, bool) {
382+
switch v := v.(type) {
383+
case float64:
384+
return v, true
385+
case float32:
386+
return float64(v), true
387+
case int:
388+
return float64(v), true
389+
case int8:
390+
return float64(v), true
391+
case int16:
392+
return float64(v), true
393+
case int32:
394+
return float64(v), true
395+
case int64:
396+
return float64(v), true
397+
case uint:
398+
return float64(v), true
399+
case uint8:
400+
return float64(v), true
401+
case uint16:
402+
return float64(v), true
403+
case uint32:
404+
return float64(v), true
405+
case uint64:
406+
return float64(v), true
407+
case json.Number:
408+
f, err := v.Float64()
409+
return f, err == nil
410+
default:
411+
return 0, false
412+
}
413+
}
414+
376415
// Validate an input value against a schema, collecting errors in the validation
377416
// result object. If successful, `res.Errors` will be empty. It is suggested
378417
// to use a `sync.Pool` to reuse the PathBuffer and ValidateResult objects,
@@ -432,34 +471,8 @@ func Validate(r Registry, s *Schema, path *PathBuffer, mode ValidateMode, v any,
432471
return
433472
}
434473
case TypeNumber, TypeInteger:
435-
var num float64
436-
437-
switch v := v.(type) {
438-
case float64:
439-
num = v
440-
case float32:
441-
num = float64(v)
442-
case int:
443-
num = float64(v)
444-
case int8:
445-
num = float64(v)
446-
case int16:
447-
num = float64(v)
448-
case int32:
449-
num = float64(v)
450-
case int64:
451-
num = float64(v)
452-
case uint:
453-
num = float64(v)
454-
case uint8:
455-
num = float64(v)
456-
case uint16:
457-
num = float64(v)
458-
case uint32:
459-
num = float64(v)
460-
case uint64:
461-
num = float64(v)
462-
default:
474+
num, ok := toFloat64(v)
475+
if !ok {
463476
if s.Type == TypeInteger {
464477
res.Add(path, v, validation.MsgExpectedInteger)
465478
} else {
@@ -582,6 +595,20 @@ func Validate(r Registry, s *Schema, path *PathBuffer, mode ValidateMode, v any,
582595

583596
if len(s.Enum) > 0 {
584597
found := slices.Contains(s.Enum, v)
598+
if !found && (s.Type == TypeInteger || s.Type == TypeNumber) {
599+
// Numeric enums may be stored with a different Go type than the
600+
// parsed value (e.g. float64 from JSON bodies vs int64 from query
601+
// or path params), so a strict type comparison is not enough.
602+
// Compare numerically instead. Note this uses float64 like the rest
603+
// of the numeric validation above, so integer values beyond 2^53
604+
// are subject to the same precision limits.
605+
if num, ok := toFloat64(v); ok {
606+
found = slices.ContainsFunc(s.Enum, func(e any) bool {
607+
ev, ok := toFloat64(e)
608+
return ok && ev == num
609+
})
610+
}
611+
}
585612
if !found {
586613
res.Add(path, v, s.msgEnum)
587614
}

‎validate_test.go‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,53 @@ var validateTests = []struct {
10911091
}](),
10921092
input: map[string]any{"value": []any{1.0}},
10931093
},
1094+
{
1095+
// Regression for #1044: enum tag values are stored as float64 while
1096+
// query/path params are parsed as int64; the two must compare equal.
1097+
name: "enum int success int64 value",
1098+
typ: reflect.TypeFor[struct {
1099+
Value int "json:\"value\" enum:\"1,5,9\""
1100+
}](),
1101+
input: map[string]any{"value": int64(5)},
1102+
},
1103+
{
1104+
name: "enum int int64 value not in enum",
1105+
typ: reflect.TypeFor[struct {
1106+
Value int "json:\"value\" enum:\"1,5,9\""
1107+
}](),
1108+
input: map[string]any{"value": int64(2)},
1109+
errs: []string{"expected value to be one of \"1, 5, 9\""},
1110+
},
1111+
{
1112+
// Custom Schema() emitting float64 enum values, validated against an
1113+
// int64 value (the query/path parameter path from #1044).
1114+
name: "enum integer float64 stored int64 value",
1115+
s: &huma.Schema{
1116+
Type: huma.TypeInteger,
1117+
Enum: []any{float64(1), float64(2)},
1118+
},
1119+
input: int64(2),
1120+
},
1121+
{
1122+
// json.Number is produced by decoders configured with UseNumber().
1123+
name: "number json.Number success",
1124+
s: &huma.Schema{Type: huma.TypeNumber},
1125+
input: json.Number("5.5"),
1126+
},
1127+
{
1128+
name: "enum integer json.Number value",
1129+
s: &huma.Schema{
1130+
Type: huma.TypeInteger,
1131+
Enum: []any{float64(1), float64(2)},
1132+
},
1133+
input: json.Number("2"),
1134+
},
1135+
{
1136+
name: "number invalid json.Number",
1137+
s: &huma.Schema{Type: huma.TypeNumber},
1138+
input: json.Number("not-a-number"),
1139+
errs: []string{"expected number"},
1140+
},
10941141
{
10951142
name: "expected enum",
10961143
typ: reflect.TypeFor[struct {

0 commit comments

Comments
 (0)