From b5fe9179183b81467a3d116b36d74083d1152e90 Mon Sep 17 00:00:00 2001 From: Owen Diehl Date: Mon, 10 May 2021 17:06:18 -0400 Subject: [PATCH] empty secret json marshals "" Signed-off-by: Owen Diehl --- config/config.go | 3 +++ config/config_test.go | 37 ++++++++++++++++++++++++++----------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/config/config.go b/config/config.go index e2c1d895..fffda4a7 100644 --- a/config/config.go +++ b/config/config.go @@ -42,6 +42,9 @@ func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error { // MarshalJSON implements the json.Marshaler interface for Secret. func (s Secret) MarshalJSON() ([]byte, error) { + if len(s) == 0 { + return json.Marshal("") + } return json.Marshal(secretToken) } diff --git a/config/config_test.go b/config/config_test.go index db2c7f7f..4c8248aa 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -23,18 +23,33 @@ import ( ) func TestJSONMarshalSecret(t *testing.T) { - test := struct { + type tmp struct { S Secret - }{ - S: Secret("test"), } - - c, err := json.Marshal(test) - if err != nil { - t.Fatal(err) + for _, tc := range []struct { + desc string + data tmp + expected string + }{ + { + desc: "inhabited", + // u003c -> "<" + // u003e -> ">" + data: tmp{"test"}, + expected: "{\"S\":\"\\u003csecret\\u003e\"}", + }, + { + desc: "empty", + data: tmp{}, + expected: "{\"S\":\"\"}", + }, + } { + t.Run(tc.desc, func(t *testing.T) { + c, err := json.Marshal(tc.data) + if err != nil { + t.Fatal(err) + } + require.Equal(t, tc.expected, string(c), "Secret not properly elided.") + }) } - - // u003c -> "<" - // u003e -> ">" - require.Equal(t, "{\"S\":\"\\u003csecret\\u003e\"}", string(c), "Secret not properly elided.") }