Skip to content

Commit 042376f

Browse files
benluddyk8s-publishing-bot
authored andcommitted
Encode byte array to CBOR as array of integer, not byte string.
This is structurally compatible with the JSON behavior. Kubernetes-commit: 037ba12551d9adc1222d1146f73f69912b6bfa94
1 parent e3238d4 commit 042376f

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

pkg/runtime/serializer/cbor/internal/modes/encode.go

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ var Encode cbor.EncMode = func() cbor.EncMode {
7575
// Encode struct field names to the byte string type rather than the text string
7676
// type.
7777
FieldName: cbor.FieldNameToByteString,
78+
79+
// Marshal Go byte arrays to CBOR arrays of integers (as in JSON) instead of byte
80+
// strings.
81+
ByteArray: cbor.ByteArrayToArray,
7882
}.EncMode()
7983
if err != nil {
8084
panic(err)

pkg/runtime/serializer/cbor/internal/modes/encode_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ func TestEncode(t *testing.T) {
6464
}
6565
}),
6666
},
67+
{
68+
name: "byte array encodes to array of integers",
69+
in: [3]byte{0x01, 0x02, 0x03},
70+
want: []byte{0x83, 0x01, 0x02, 0x03}, // [1, 2, 3]
71+
assertOnError: assertNilError,
72+
},
6773
} {
6874
encModes := tc.modes
6975
if len(encModes) == 0 {

pkg/runtime/serializer/cbor/internal/modes/roundtrip_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ func TestRoundtrip(t *testing.T) {
4747
name: "nil slice",
4848
obj: []interface{}(nil),
4949
},
50+
{
51+
name: "byte array",
52+
obj: [3]byte{0x01, 0x02, 0x03},
53+
},
5054
{
5155
name: "nil map",
5256
obj: map[string]interface{}(nil),

0 commit comments

Comments
 (0)