Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ytypes/leaf_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,11 @@ func TestUnmarshalLeafListJSONEncoding(t *testing.T) {
json: `{ "int32-leaf-list" : [42, 4294967296] }`,
wantErr: `error parsing 4.294967296e+09 for schema int32-leaf-list: value 4294967296 falls outside the int range [-2147483648, 2147483647]`,
},
{
desc: "non-integer array element",
json: `{ "int32-leaf-list" : [42, 1.5] }`,
wantErr: `error parsing 1.5 for schema int32-leaf-list: value 1.5 is not an integer`,
},
}

var jsonTree interface{}
Expand Down
15 changes: 15 additions & 0 deletions ytypes/leaf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,21 @@ func TestUnmarshalLeafJSONEncoding(t *testing.T) {
json: `{"uint32-leaf" : -42}`,
wantErr: `error parsing -42 for schema uint32-leaf: value -42 falls outside the int range [0, 4294967295]`,
},
{
desc: "int8 non-integer",
json: `{"int8-leaf" : -1.5}`,
wantErr: `error parsing -1.5 for schema int8-leaf: value -1.5 is not an integer`,
},
{
desc: "uint8 non-integer",
json: `{"uint8-leaf" : 3.9}`,
wantErr: `error parsing 3.9 for schema uint8-leaf: value 3.9 is not an integer`,
},
{
desc: "int32 non-integer",
json: `{"int32-leaf" : 42.1}`,
wantErr: `error parsing 42.1 for schema int32-leaf: value 42.1 is not an integer`,
},
{
desc: "int64 out of range",
json: `{"int64-leaf" : "-9223372036854775809"}`,
Expand Down
9 changes: 8 additions & 1 deletion ytypes/util_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,8 @@ func yangFloatIntToGoType(t yang.TypeKind, v float64) (interface{}, error) {
return nil, fmt.Errorf("unexpected YANG type %v", t)
}

// checkJSONFloat64Range checks whether f is in range for the given YANG type.
// checkJSONFloat64Range checks whether f is an integer that is in range for
// the given YANG type.
func checkJSONFloat64Range(t yang.TypeKind, f float64) error {
minMax := map[yang.TypeKind]struct {
min int64
Expand All @@ -453,6 +454,12 @@ func checkJSONFloat64Range(t yang.TypeKind, f float64) error {
if _, ok := minMax[t]; !ok {
return fmt.Errorf("checkJSONFloat64Range bad YANG type %v", t)
}
// A JSON number for an integer YANG type must not carry a fractional
// part; without this check int8(f)/uint8(f)/etc. below would silently
// truncate it (e.g. 3.9 would be accepted and stored as 3).
if f != math.Trunc(f) {
return fmt.Errorf("value %v is not an integer", f)
}
if int64(f) < minMax[t].min || int64(f) > minMax[t].max {
return fmt.Errorf("value %d falls outside the int range [%d, %d]", int64(f), minMax[t].min, minMax[t].max)
}
Expand Down
Loading