From 95e10ae73f9601199d0fae67be06fddebaa0da6c Mon Sep 17 00:00:00 2001 From: Aditya030207 <230317630+Aditya030207@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:33:04 +0530 Subject: [PATCH] fix: Keep non-finite API fields as strings --- internal/cmd/api.go | 5 +++-- internal/cmd/cmd_test.go | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/internal/cmd/api.go b/internal/cmd/api.go index d108563..f7b950f 100644 --- a/internal/cmd/api.go +++ b/internal/cmd/api.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "io" + "math" "net/http" "net/url" "os" @@ -176,7 +177,7 @@ func parseFields() (map[string]any, error) { return fields, nil } -// typedFieldValue infers a JSON type for a -F value: bool, null, number, else +// typedFieldValue infers a JSON type for a -F value: bool, null, finite number, else // string. func typedFieldValue(raw string) any { switch raw { @@ -190,7 +191,7 @@ func typedFieldValue(raw string) any { if n, err := strconv.Atoi(raw); err == nil { return n } - if f, err := strconv.ParseFloat(raw, 64); err == nil { + if f, err := strconv.ParseFloat(raw, 64); err == nil && !math.IsNaN(f) && !math.IsInf(f, 0) { return f } return raw diff --git a/internal/cmd/cmd_test.go b/internal/cmd/cmd_test.go index 8e6b338..b69e53d 100644 --- a/internal/cmd/cmd_test.go +++ b/internal/cmd/cmd_test.go @@ -8193,6 +8193,29 @@ func TestAPI(t *testing.T) { } }) + t.Run("JSON body keeps non-finite typed fields as strings", func(t *testing.T) { + // Given + flagUpdateEnv(t) + + // When + e := echoJSON(t, "", "-F", "nan=NaN", "-F", "positive=+Inf", + "-F", "negative=-Infinity", "-F", "decimal=1.25") + + // Then + body, _ := e["body"].(string) + var fields map[string]any + if err := json.Unmarshal([]byte(body), &fields); err != nil { + t.Fatalf("parse body %q: %v", body, err) + } + for key, want := range map[string]any{ + "nan": "NaN", "positive": "+Inf", "negative": "-Infinity", "decimal": 1.25, + } { + if got := fields[key]; got != want { + t.Errorf("field %s = %#v, want %#v", key, got, want) + } + } + }) + t.Run("fields join a path that already has a query string", func(t *testing.T) { // Given flagUpdateEnv(t)