From 0a0e7cf90c20d1492db7328a9eca6cc41e48a148 Mon Sep 17 00:00:00 2001 From: Deathstroke <776022703@qq.com> Date: Sat, 11 May 2024 14:30:48 +0800 Subject: [PATCH 1/2] Explicitly convert inMediaType json to outMediaType yaml Signed-off-by: Deathstroke <776022703@qq.com> --- pkg/encoding/encoding.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/encoding/encoding.go b/pkg/encoding/encoding.go index 14d8606..592709f 100644 --- a/pkg/encoding/encoding.go +++ b/pkg/encoding/encoding.go @@ -95,6 +95,15 @@ func Convert(codecs serializer.CodecFactory, inMediaType, outMediaType string, i if outMediaType == JsonMediaType { encoded = append(encoded, '\n') } + } else if inMediaType == JsonMediaType && outMediaType == YamlMediaType { + val := map[string]interface{}{} + if err := json.Unmarshal(in, &val); err != nil { + return nil, fmt.Errorf("error decoding from %s: %s", inMediaType, err) + } + encoded, err = yaml.Marshal(val) + if err != nil { + return nil, fmt.Errorf("error encoding from %s: %s", outMediaType, err) + } } else { inCodec, err := newCodec(codecs, typeMeta, inMediaType) if err != nil { From c7cfb3d3a303d89683e986dfcc93a087000f27bf Mon Sep 17 00:00:00 2001 From: Deathstroke <776022703@qq.com> Date: Sun, 12 May 2024 17:17:25 +0800 Subject: [PATCH 2/2] Fix the return value problem Signed-off-by: Deathstroke <776022703@qq.com> --- pkg/encoding/encoding.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/encoding/encoding.go b/pkg/encoding/encoding.go index 592709f..0507b89 100644 --- a/pkg/encoding/encoding.go +++ b/pkg/encoding/encoding.go @@ -98,11 +98,11 @@ func Convert(codecs serializer.CodecFactory, inMediaType, outMediaType string, i } else if inMediaType == JsonMediaType && outMediaType == YamlMediaType { val := map[string]interface{}{} if err := json.Unmarshal(in, &val); err != nil { - return nil, fmt.Errorf("error decoding from %s: %s", inMediaType, err) + return nil, nil, fmt.Errorf("error decoding from %s: %s", inMediaType, err) } encoded, err = yaml.Marshal(val) if err != nil { - return nil, fmt.Errorf("error encoding from %s: %s", outMediaType, err) + return nil, nil, fmt.Errorf("error encoding from %s: %s", outMediaType, err) } } else { inCodec, err := newCodec(codecs, typeMeta, inMediaType)