@@ -31,3 +31,60 @@ func TestReplacingJSONMarshaller(t *testing.T) {
3131
3232 c .Assert (string (b ), qt .Equals , `{"baz":42,"foo":"bar"}` )
3333}
34+
35+ // See issue 14855.
36+ func TestReplacingJSONMarshallerOmitEmptySubMaps (t * testing.T ) {
37+ c := qt .New (t )
38+
39+ m := map [string ]any {
40+ "keep" : "yes" ,
41+ "target" : map [string ]any {
42+ "path" : "/x" ,
43+ "sites" : map [string ]any {
44+ "matrix" : map [string ]any {
45+ "languages" : []any {},
46+ "versions" : []any {},
47+ },
48+ "complements" : map [string ]any {
49+ "languages" : []any {},
50+ },
51+ },
52+ },
53+ "all_zero_branch" : map [string ]any {
54+ "inner" : map [string ]any {
55+ "empty" : "" ,
56+ "deep" : map [string ]any {
57+ "zero" : 0 ,
58+ },
59+ },
60+ },
61+ "keep_slice_of_maps" : []any {
62+ map [string ]any {
63+ "name" : "a" ,
64+ "empty" : "" ,
65+ "sub" : map [string ]any {"x" : 0 },
66+ },
67+ },
68+ }
69+
70+ marshaller := ReplacingJSONMarshaller {
71+ Value : m ,
72+ KeysToLower : true ,
73+ OmitEmpty : true ,
74+ }
75+
76+ b , err := marshaller .MarshalJSON ()
77+ c .Assert (err , qt .IsNil )
78+
79+ s := string (b )
80+ c .Assert (s , qt .Contains , `"keep":"yes"` )
81+ c .Assert (s , qt .Contains , `"path":"/x"` )
82+ c .Assert (s , qt .Not (qt .Contains ), `"sites"` )
83+ c .Assert (s , qt .Not (qt .Contains ), `"matrix"` )
84+ c .Assert (s , qt .Not (qt .Contains ), `"complements"` )
85+ c .Assert (s , qt .Not (qt .Contains ), `"all_zero_branch"` )
86+ c .Assert (s , qt .Not (qt .Contains ), `"inner"` )
87+ c .Assert (s , qt .Not (qt .Contains ), `"deep"` )
88+ c .Assert (s , qt .Contains , `"name":"a"` )
89+ c .Assert (s , qt .Not (qt .Contains ), `"sub"` )
90+ }
0 commit comments