Skip to content

Commit

Permalink
fixes #526, encode empty list as empty list when using RFC7951 encodi…
Browse files Browse the repository at this point in the history
…ng (#528)

* fixes #526, encode empty list as empty list when using RFC7951 encoding
  • Loading branch information
cyrilMargaria authored May 12, 2021
1 parent 13d6af5 commit 9e9160f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ygot/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,10 @@ func mapJSON(field reflect.Value, parentMod string, args jsonOutputConfig) (inte
sort.Strings(mapKeys)

if len(mapKeys) == 0 {
// empty list should be encoded as empty list
if args.jType == RFC7951 {
return []interface{}{}, nil
}
return nil, nil
}

Expand Down
19 changes: 18 additions & 1 deletion ygot/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1988,6 +1988,22 @@ func TestConstructJSON(t *testing.T) {
},
List: map[uint32]*renderExampleList{},
},
wantIETF: map[string]interface{}{
"ch": map[string]interface{}{"val": "42"},
/// RFC7951 Section 5.4 defines a YANG list as an JSON array. Per RFC 8259 Section 5 an empty array should be [] rather than 'null'.
"list": []interface{}{},
},
wantInternal: map[string]interface{}{
"ch": map[string]interface{}{"val": 42},
},
}, {
name: "empty map nil",
in: &renderExample{
Ch: &renderExampleChild{
Val: Uint64(42),
},
List: nil,
},
wantIETF: map[string]interface{}{
"ch": map[string]interface{}{"val": "42"},
},
Expand Down Expand Up @@ -3370,7 +3386,8 @@ func TestMarshal7951(t *testing.T) {
}, {
desc: "empty map",
in: map[string]*renderExample{},
want: `null`,
// null as empty array is not valid, RFC7951 section 5.4 specify that the array must be an array, and JSON empty arrays are not null value
want: `[]`,
}, {
desc: "nil string pointer",
in: (*string)(nil),
Expand Down

0 comments on commit 9e9160f

Please sign in to comment.