From 0a75e5a07b858cbe6216c99fa12d582d063499d9 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Tue, 12 Aug 2025 01:45:44 -0700 Subject: [PATCH] encoding/json/v2: fix wrong type with cyclic marshal error in map[string]any The type reported in a ErrCycle is the wrong type due to a typo. This discrepency was detected by setting optimizeCommon to false and running the tests. This only modifies code that is compiled in under goexperiment.jsonv2. Change-Id: I68268f5c719d8b79a67424a35ed0647adf12288c Reviewed-on: https://go-review.googlesource.com/c/go/+/695277 Reviewed-by: Dmitri Shuralyov LUCI-TryBot-Result: Go LUCI Reviewed-by: Damien Neil --- src/encoding/json/v2/arshal_any.go | 2 +- src/encoding/json/v2/arshal_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/encoding/json/v2/arshal_any.go b/src/encoding/json/v2/arshal_any.go index 3fb679d553..c2b09bd918 100644 --- a/src/encoding/json/v2/arshal_any.go +++ b/src/encoding/json/v2/arshal_any.go @@ -104,7 +104,7 @@ func marshalObjectAny(enc *jsontext.Encoder, obj map[string]any, mo *jsonopts.St if xe.Tokens.Depth() > startDetectingCyclesAfter { v := reflect.ValueOf(obj) if err := visitPointer(&xe.SeenPointers, v); err != nil { - return newMarshalErrorBefore(enc, anyType, err) + return newMarshalErrorBefore(enc, mapStringAnyType, err) } defer leavePointer(&xe.SeenPointers, v) } diff --git a/src/encoding/json/v2/arshal_test.go b/src/encoding/json/v2/arshal_test.go index 764ce69007..5f5f072e25 100644 --- a/src/encoding/json/v2/arshal_test.go +++ b/src/encoding/json/v2/arshal_test.go @@ -3278,7 +3278,7 @@ func TestMarshal(t *testing.T) { return struct{ X any }{m} }(), want: `{"X"` + strings.Repeat(`:{""`, startDetectingCyclesAfter), - wantErr: EM(internal.ErrCycle).withPos(`{"X":`+strings.Repeat(`{"":`, startDetectingCyclesAfter), "/X"+jsontext.Pointer(strings.Repeat("/", startDetectingCyclesAfter))).withType(0, T[any]()), + wantErr: EM(internal.ErrCycle).withPos(`{"X":`+strings.Repeat(`{"":`, startDetectingCyclesAfter), "/X"+jsontext.Pointer(strings.Repeat("/", startDetectingCyclesAfter))).withType(0, T[map[string]any]()), }, { name: jsontest.Name("Interfaces/Any/Slices/Nil"), in: struct{ X any }{[]any(nil)}, -- 2.51.0