From: Arthur Diniz Date: Wed, 9 Sep 2026 23:25:43 +0100 Subject: [PATCH] Tolerate json/v2 invalid UTF-8 munging in YAMLToJSON tests Signed-off-by: Arthur Diniz Description: Tolerate json/v2 invalid UTF-8 munging in YAMLToJSON tests TestYAMLToJSON's "binary data" case decodes invalid UTF-8 via a !!binary tag and asserts the JSON escapes the Unicode replacement character U+FFFD as "\ufffd". Go 1.27 reimplements encoding/json on top of encoding/json/v2 and emits U+FFFD as raw UTF-8 bytes rather than the "\ufffd" escape, so the test fails on Go 1.27 while passing on Go 1.26 and earlier. . Both forms are valid JSON that decode to the same value; only the string escaping representation changed in the standard library (see https://github.com/golang/go/issues/74551). Add a jsonAllow field listing accepted alternate representations and tolerate the raw form so the tests pass across Go versions. This is test-only and does not change library behavior. Origin: upstream, https://github.com/kubernetes-sigs/yaml/pull/141 Bug-Debian: https://bugs.debian.org/1146222 Forwarded: https://github.com/kubernetes-sigs/yaml/pull/141 Last-Update: 2026-09-09 --- yaml_test.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/yaml_test.go b/yaml_test.go index 55b03bb..35ad6b3 100644 --- a/yaml_test.go +++ b/yaml_test.go @@ -22,6 +22,7 @@ import ( "fmt" "math" "reflect" + "slices" "sort" "strconv" "testing" @@ -126,6 +127,10 @@ func testUnmarshal(t *testing.T, f testUnmarshalFunc, tests map[string]unmarshal type yamlToJSONTestcase struct { yaml string json string + // jsonAllow provides allowed alternate JSON representations. + // For the json v1->v2 transition, this is used to tolerate changes + // in how malformed input in munged. + jsonAllow []string // By default we test that reversing the output == input. But if there is a // difference in the reversed output, you can optionally specify it here. yamlReverseOverwrite *string @@ -162,8 +167,8 @@ func testYAMLToJSON(t *testing.T, f testYAMLToJSONFunc, tests map[string]yamlToJ } // Check it against the expected output. - if string(jsonBytes) != test.json { - t.Errorf("Failed to convert YAML to JSON, yaml: `%s`, expected json `%s`, got `%s`", test.yaml, test.json, string(jsonBytes)) + if got := string(jsonBytes); got != test.json && !slices.Contains(test.jsonAllow, got) { + t.Errorf("Failed to convert YAML to JSON, yaml: `%s`, expected json `%s`, got `%s`", test.yaml, test.json, got) } }) @@ -747,8 +752,10 @@ func TestYAMLToJSON(t *testing.T) { json: `[{"t":null}]`, }, "binary data": { - yaml: "a: !!binary gIGC", - json: `{"a":"\ufffd\ufffd\ufffd"}`, + yaml: "a: !!binary gIGC", + json: `{"a":"\ufffd\ufffd\ufffd"}`, + // Go json/v2 emits U+FFFD as raw UTF-8 bytes rather than the \ufffd escape. + jsonAllow: []string{"{\"a\":\"\ufffd\ufffd\ufffd\"}"}, yamlReverseOverwrite: strPtr("a: \ufffd\ufffd\ufffd\n"), },