Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource quantity must support leading and trailing whitespace in JSON for back-compat #26907

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 3 additions & 6 deletions pkg/api/resource/quantity.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ Suffix:
switch str[i] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
default:
pos = i
break Suffix
}
}
Expand Down Expand Up @@ -619,21 +618,19 @@ func (q Quantity) MarshalJSON() ([]byte, error) {
}

// UnmarshalJSON implements the json.Unmarshaller interface.
// TODO: Remove support for leading/trailing whitespace
func (q *Quantity) UnmarshalJSON(value []byte) error {
l := len(value)
if l == 4 && bytes.Equal(value, []byte("null")) {
q.d.Dec = nil
q.i = int64Amount{}
return nil
}
if l < 2 {
return ErrFormatWrong
}
if value[0] == '"' && value[l-1] == '"' {
if l >= 2 && value[0] == '"' && value[l-1] == '"' {
value = value[1 : l-1]
}

parsed, err := ParseQuantity(string(value))
parsed, err := ParseQuantity(strings.TrimSpace(string(value)))
if err != nil {
return err
}
Expand Down
38 changes: 36 additions & 2 deletions pkg/api/resource/quantity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package resource
import (
"encoding/json"
"math/rand"
"strings"
"testing"
"unicode"

fuzz "github.com/google/gofuzz"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -231,6 +233,9 @@ func TestQuantityParse(t *testing.T) {
{"0Ti", decQuantity(0, 0, BinarySI)},
{"0T", decQuantity(0, 0, DecimalSI)},

// Quantity less numbers are allowed
{"1", decQuantity(1, 0, DecimalSI)},

// Binary suffixes
{"1Ki", decQuantity(1024, 0, BinarySI)},
{"8Ki", decQuantity(8*1024, 0, BinarySI)},
Expand Down Expand Up @@ -422,7 +427,7 @@ func TestQuantityParse(t *testing.T) {
desired := &inf.Dec{}
expect := Quantity{d: infDecAmount{Dec: desired}}
for _, item := range table {
got, err := ParseQuantity("-" + item.input)
got, err := ParseQuantity("-" + strings.TrimLeftFunc(item.input, unicode.IsSpace))
if err != nil {
t.Errorf("-%v: unexpected error: %v", item.input, err)
continue
Expand All @@ -444,7 +449,7 @@ func TestQuantityParse(t *testing.T) {

// Try everything with an explicit +
for _, item := range table {
got, err := ParseQuantity("+" + item.input)
got, err := ParseQuantity("+" + strings.TrimLeftFunc(item.input, unicode.IsSpace))
if err != nil {
t.Errorf("-%v: unexpected error: %v", item.input, err)
continue
Expand Down Expand Up @@ -472,6 +477,10 @@ func TestQuantityParse(t *testing.T) {
"1i",
"-3.01i",
"-3.01e-",

// trailing whitespace is forbidden
" 1",
"1 ",
}
for _, item := range invalid {
_, err := ParseQuantity(item)
Expand Down Expand Up @@ -815,6 +824,31 @@ func TestJSON(t *testing.T) {
}
}

func TestJSONWhitespace(t *testing.T) {
q := Quantity{}
testCases := []struct {
in string
expect string
}{
{`" 1"`, "1"},
{`"1 "`, "1"},
{`1`, "1"},
{` 1`, "1"},
{`1 `, "1"},
{`10`, "10"},
{`-1`, "-1"},
{` -1`, "-1"},
}
for _, test := range testCases {
if err := json.Unmarshal([]byte(test.in), &q); err != nil {
t.Errorf("%q: %v", test.in, err)
}
if q.String() != test.expect {
t.Errorf("unexpected string: %q", q.String())
}
}
}

func TestMilliNewSet(t *testing.T) {
table := []struct {
value int64
Expand Down