fix(10.1): WR-04 judge action fill values by their JSON encoding
isJSONScalar now encodes each value and keeps it only when the encoding is a string, number, boolean or null, so a named scalar whose MarshalJSON writes an array or object, NaN and the infinities are dropped. writeJSON encodes into a buffer before the status line, so an encode failure is a logged 500 with the generic envelope instead of a 200 with a truncated body.
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/bouncer"
|
||||
"git.golem15.com/golem15/summercms/modules/pact"
|
||||
@@ -219,25 +218,18 @@ func onlyFillScalars(fill []string, values map[string]any) map[string]any {
|
||||
}
|
||||
|
||||
// isJSONScalar reports whether v encodes as a JSON string, number, boolean or
|
||||
// null.
|
||||
// null. It decides by encoding the value, not by its reflect.Kind: a named
|
||||
// scalar whose MarshalJSON writes an array or object is not a scalar, and a
|
||||
// value that cannot be encoded (NaN, an infinity) is dropped rather than
|
||||
// failing the whole response.
|
||||
func isJSONScalar(v any) bool {
|
||||
if v == nil || nestedValue(v) {
|
||||
return v == nil
|
||||
}
|
||||
rv := reflect.ValueOf(v)
|
||||
for rv.Kind() == reflect.Pointer {
|
||||
if rv.IsNil() {
|
||||
return true
|
||||
}
|
||||
rv = rv.Elem()
|
||||
}
|
||||
switch rv.Kind() {
|
||||
case reflect.Bool, reflect.String,
|
||||
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
|
||||
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
|
||||
reflect.Float32, reflect.Float64:
|
||||
return true
|
||||
default:
|
||||
raw, err := json.Marshal(v)
|
||||
if err != nil || len(raw) == 0 {
|
||||
return false
|
||||
}
|
||||
switch raw[0] {
|
||||
case '{', '[':
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user