diff --git a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/encode.go b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/encode.go index ac839a53521..078a976c4a5 100644 --- a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/encode.go +++ b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/encode.go @@ -75,6 +75,10 @@ var Encode cbor.EncMode = func() cbor.EncMode { // Encode struct field names to the byte string type rather than the text string // type. FieldName: cbor.FieldNameToByteString, + + // Marshal Go byte arrays to CBOR arrays of integers (as in JSON) instead of byte + // strings. + ByteArray: cbor.ByteArrayToArray, }.EncMode() if err != nil { panic(err) diff --git a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/encode_test.go b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/encode_test.go index e9d8906e502..48273ede2e3 100644 --- a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/encode_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/encode_test.go @@ -64,6 +64,12 @@ func TestEncode(t *testing.T) { } }), }, + { + name: "byte array encodes to array of integers", + in: [3]byte{0x01, 0x02, 0x03}, + want: []byte{0x83, 0x01, 0x02, 0x03}, // [1, 2, 3] + assertOnError: assertNilError, + }, } { encModes := tc.modes if len(encModes) == 0 { diff --git a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/roundtrip_test.go b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/roundtrip_test.go index aa411532de3..187701e23dd 100644 --- a/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/roundtrip_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/runtime/serializer/cbor/internal/modes/roundtrip_test.go @@ -47,6 +47,10 @@ func TestRoundtrip(t *testing.T) { name: "nil slice", obj: []interface{}(nil), }, + { + name: "byte array", + obj: [3]byte{0x01, 0x02, 0x03}, + }, { name: "nil map", obj: map[string]interface{}(nil),