fix(cli): preserve integer precision in jq output (#3991)

main
LeonSGP 2026-10-08 01:28:25 +08:00 committed by GitHub
parent cefd5920f8
commit ff33184433
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 1 deletions

View File

@ -0,0 +1,32 @@
package auth
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
"github.com/Tencent/WeKnora/cli/internal/config"
"github.com/Tencent/WeKnora/cli/internal/iostreams"
sdk "github.com/Tencent/WeKnora/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStatusJQPreservesTenantID(t *testing.T) {
out, _ := iostreams.SetForTest(t)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/api/v1/auth/me", r.URL.Path)
_, _ = w.Write([]byte(`{"success":true,"data":{"user":{"id":"user-test","tenant_id":9007199254740993}}}`))
}))
defer server.Close()
f := &cmdutil.Factory{Config: func() (*config.Config, error) {
return &config.Config{CurrentProfile: "test", Profiles: map[string]config.Profile{
"test": {Host: server.URL},
}}, nil
}}
require.NoError(t, runStatus(context.Background(),
&cmdutil.FormatOptions{Mode: cmdutil.FormatJSON, JQ: ".data.tenant_id"}, f, sdk.NewClient(server.URL)))
assert.Equal(t, "9007199254740993\n", out.String())
}

View File

@ -69,7 +69,9 @@ func writeJQ(w io.Writer, raw []byte, expr string) error {
return &JQError{Err: fmt.Errorf("jq parse: %w", err)}
}
var input any
if err := json.Unmarshal(raw, &input); err != nil {
decoder := json.NewDecoder(bytes.NewReader(raw))
decoder.UseNumber()
if err := decoder.Decode(&input); err != nil {
return fmt.Errorf("jq input parse: %w", err)
}
iter := query.Run(input)