Skip to content

Commit b5fe917

Browse files
committed
empty secret json marshals ""
Signed-off-by: Owen Diehl <ow.diehl@gmail.com>
1 parent a237946 commit b5fe917

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error {
4242

4343
// MarshalJSON implements the json.Marshaler interface for Secret.
4444
func (s Secret) MarshalJSON() ([]byte, error) {
45+
if len(s) == 0 {
46+
return json.Marshal("")
47+
}
4548
return json.Marshal(secretToken)
4649
}
4750

config/config_test.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,33 @@ import (
2323
)
2424

2525
func TestJSONMarshalSecret(t *testing.T) {
26-
test := struct {
26+
type tmp struct {
2727
S Secret
28-
}{
29-
S: Secret("test"),
3028
}
31-
32-
c, err := json.Marshal(test)
33-
if err != nil {
34-
t.Fatal(err)
29+
for _, tc := range []struct {
30+
desc string
31+
data tmp
32+
expected string
33+
}{
34+
{
35+
desc: "inhabited",
36+
// u003c -> "<"
37+
// u003e -> ">"
38+
data: tmp{"test"},
39+
expected: "{\"S\":\"\\u003csecret\\u003e\"}",
40+
},
41+
{
42+
desc: "empty",
43+
data: tmp{},
44+
expected: "{\"S\":\"\"}",
45+
},
46+
} {
47+
t.Run(tc.desc, func(t *testing.T) {
48+
c, err := json.Marshal(tc.data)
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
require.Equal(t, tc.expected, string(c), "Secret not properly elided.")
53+
})
3554
}
36-
37-
// u003c -> "<"
38-
// u003e -> ">"
39-
require.Equal(t, "{\"S\":\"\\u003csecret\\u003e\"}", string(c), "Secret not properly elided.")
4055
}

0 commit comments

Comments
 (0)