| 1 |
From: Arthur Diniz <[email protected]> |
| 2 |
Date: Wed, 9 Sep 2026 23:25:43 +0100 |
| 3 |
Subject: [PATCH] Tolerate json/v2 invalid UTF-8 munging in YAMLToJSON tests |
| 4 |
|
| 5 |
Signed-off-by: Arthur Diniz <[email protected]> |
| 6 |
|
| 7 |
Description: Tolerate json/v2 invalid UTF-8 munging in YAMLToJSON tests |
| 8 |
TestYAMLToJSON's "binary data" case decodes invalid UTF-8 via a !!binary tag |
| 9 |
and asserts the JSON escapes the Unicode replacement character U+FFFD as |
| 10 |
"\ufffd". Go 1.27 reimplements encoding/json on top of encoding/json/v2 and |
| 11 |
emits U+FFFD as raw UTF-8 bytes rather than the "\ufffd" escape, so the test |
| 12 |
fails on Go 1.27 while passing on Go 1.26 and earlier. |
| 13 |
. |
| 14 |
Both forms are valid JSON that decode to the same value; only the string |
| 15 |
escaping representation changed in the standard library (see |
| 16 |
https://github.com/golang/go/issues/74551). Add a jsonAllow field listing |
| 17 |
accepted alternate representations and tolerate the raw form so the tests pass |
| 18 |
across Go versions. This is test-only and does not change library behavior. |
| 19 |
|
| 20 |
Origin: upstream, https://github.com/kubernetes-sigs/yaml/pull/141 |
| 21 |
Bug-Debian: https://bugs.debian.org/1146222 |
| 22 |
Forwarded: https://github.com/kubernetes-sigs/yaml/pull/141 |
| 23 |
Last-Update: 2026-09-09 |
| 24 |
--- |
| 25 |
yaml_test.go | 15 +++++++++++---- |
| 26 |
1 file changed, 11 insertions(+), 4 deletions(-) |
| 27 |
|
| 28 |
diff --git a/yaml_test.go b/yaml_test.go |
| 29 |
index 55b03bb..35ad6b3 100644 |
| 30 |
--- a/yaml_test.go |
| 31 |
+++ b/yaml_test.go |
| 32 |
@@ -22,6 +22,7 @@ import ( |
| 33 |
"fmt" |
| 34 |
"math" |
| 35 |
"reflect" |
| 36 |
+ "slices" |
| 37 |
"sort" |
| 38 |
"strconv" |
| 39 |
"testing" |
| 40 |
@@ -126,6 +127,10 @@ func testUnmarshal(t *testing.T, f testUnmarshalFunc, tests map[string]unmarshal |
| 41 |
type yamlToJSONTestcase struct { |
| 42 |
yaml string |
| 43 |
json string |
| 44 |
+ // jsonAllow provides allowed alternate JSON representations. |
| 45 |
+ // For the json v1->v2 transition, this is used to tolerate changes |
| 46 |
+ // in how malformed input in munged. |
| 47 |
+ jsonAllow []string |
| 48 |
// By default we test that reversing the output == input. But if there is a |
| 49 |
// difference in the reversed output, you can optionally specify it here. |
| 50 |
yamlReverseOverwrite *string |
| 51 |
@@ -162,8 +167,8 @@ func testYAMLToJSON(t *testing.T, f testYAMLToJSONFunc, tests map[string]yamlToJ |
| 52 |
} |
| 53 |
|
| 54 |
// Check it against the expected output. |
| 55 |
- if string(jsonBytes) != test.json { |
| 56 |
- t.Errorf("Failed to convert YAML to JSON, yaml: `%s`, expected json `%s`, got `%s`", test.yaml, test.json, string(jsonBytes)) |
| 57 |
+ if got := string(jsonBytes); got != test.json && !slices.Contains(test.jsonAllow, got) { |
| 58 |
+ t.Errorf("Failed to convert YAML to JSON, yaml: `%s`, expected json `%s`, got `%s`", test.yaml, test.json, got) |
| 59 |
} |
| 60 |
}) |
| 61 |
|
| 62 |
@@ -747,8 +752,10 @@ func TestYAMLToJSON(t *testing.T) { |
| 63 |
json: `[{"t":null}]`, |
| 64 |
}, |
| 65 |
"binary data": { |
| 66 |
- yaml: "a: !!binary gIGC", |
| 67 |
- json: `{"a":"\ufffd\ufffd\ufffd"}`, |
| 68 |
+ yaml: "a: !!binary gIGC", |
| 69 |
+ json: `{"a":"\ufffd\ufffd\ufffd"}`, |
| 70 |
+ // Go json/v2 emits U+FFFD as raw UTF-8 bytes rather than the \ufffd escape. |
| 71 |
+ jsonAllow: []string{"{\"a\":\"\ufffd\ufffd\ufffd\"}"}, |
| 72 |
yamlReverseOverwrite: strPtr("a: \ufffd\ufffd\ufffd\n"), |
| 73 |
}, |
| 74 |
|