terraform/provider: keep large JSON numbers exact when normalizing

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 committed 2026-12-05 14:18:29 -08:00
1 parent fae64ed81e
commit 80aeefaec0
2 files changed
+37 -4

No files matched your search

@@ -503,3 +503,22 @@ func TestIAMErrorHelpers(t *testing.T) {
t.Fatalf("expected ServiceFailure to be retryable")
}
}
func TestJSONNormalizersPreserveLargeNumbers(t *testing.T) {
t.Parallel()
raw := `{"current-snapshot-id":9007199254740993}`
normalized, err := normalizeJSONString(raw)
if err != nil {
t.Fatalf("normalize: %v", err)
}
if normalized != raw {
t.Fatalf("normalized = %s, want %s", normalized, raw)
}
if policiesSemanticallyEqual(raw, `{"current-snapshot-id":9007199254740992}`) {
t.Fatal("expected distinct integers to compare different")
}
if _, err := normalizeJSONString(`{"a":1} {"b":2}`); err == nil {
t.Fatal("expected trailing JSON to be rejected")
}
}
+18 -4
View File
@@ -3,12 +3,26 @@ package seaweedfs
import (
"encoding/json"
"errors"
"io"
"strings"
)
func normalizeJSONString(raw string) (string, error) {
func decodeJSONValue(raw string) (any, error) {
decoder := json.NewDecoder(strings.NewReader(raw))
decoder.UseNumber()
var value any
if err := json.Unmarshal([]byte(raw), &value); err != nil {
if err := decoder.Decode(&value); err != nil {
return nil, err
}
if _, err := decoder.Token(); err != io.EOF {
return nil, errors.New("invalid JSON: unexpected data after the top-level value")
}
return value, nil
}
func normalizeJSONString(raw string) (string, error) {
value, err := decodeJSONValue(raw)
if err != nil {
return "", err
}
@@ -31,8 +45,8 @@ func policiesSemanticallyEqual(a string, b string) bool {
}
func normalizePolicyDocument(raw string) (string, error) {
var value any
if err := json.Unmarshal([]byte(raw), &value); err != nil {
value, err := decodeJSONValue(raw)
if err != nil {
return "", err
}