diff --git a/internal/patch/patch.go b/internal/patch/patch.go index e540784..b19711b 100644 --- a/internal/patch/patch.go +++ b/internal/patch/patch.go @@ -1,6 +1,7 @@ package patch import ( + "bytes" "encoding/json" "fmt" "strings" @@ -41,7 +42,11 @@ func NewValidator(patchReq string, s schema.Schema, extensions ...schema.Schema) Path string Value interface{} } - if err := json.Unmarshal([]byte(patchReq), &operation); err != nil { + + // Decode a number into a json.Number instead of floag64 + d := json.NewDecoder(bytes.NewBufferString(patchReq)) + d.UseNumber() + if err := d.Decode(&operation); err != nil { return OperationValidator{}, err } diff --git a/internal/patch/patch_test.go b/internal/patch/patch_test.go index 991f20b..8cd590b 100644 --- a/internal/patch/patch_test.go +++ b/internal/patch/patch_test.go @@ -2,9 +2,10 @@ package patch import ( "fmt" + "testing" + "github.com/elimity-com/scim/schema" "github.com/scim2/filter-parser/v2" - "testing" ) func TestNewPathValidator(t *testing.T) { @@ -31,6 +32,51 @@ func TestNewPathValidator(t *testing.T) { t.Error("expected JSON error, got none") } }) + t.Run("Valid integer", func(t *testing.T) { + op := `{"op":"add","path":"attr2","value":1234}` + validator, err := NewValidator(op, patchSchema) + if err != nil { + t.Errorf("unexpected error, got %v", err) + return + } + v, err := validator.Validate() + if err != nil { + t.Errorf("unexpected error, got %v", err) + return + } + n, ok := v.(int64) + if !ok { + t.Errorf("unexpected type, got %T", v) + return + } + if n != 1234 { + t.Errorf("unexpected integer, got %d", n) + return + } + }) + + t.Run("Valid float64", func(t *testing.T) { + op := `{"op":"add","path":"attr3","value":12.34}` + validator, err := NewValidator(op, patchSchema) + if err != nil { + t.Errorf("unexpected error, got %v", err) + return + } + v, err := validator.Validate() + if err != nil { + t.Errorf("unexpected error, got %v", err) + return + } + n, ok := v.(float64) + if !ok { + t.Errorf("unexpected type, got %T", v) + return + } + if n != 12.34 { + t.Errorf("unexpected integer, got %f", n) + return + } + }) } func TestOperationValidator_getRefAttribute(t *testing.T) { diff --git a/internal/patch/schema_test.go b/internal/patch/schema_test.go index 711864e..f5e0ee6 100644 --- a/internal/patch/schema_test.go +++ b/internal/patch/schema_test.go @@ -14,6 +14,14 @@ var ( schema.SimpleCoreAttribute(schema.SimpleStringParams(schema.StringParams{ Name: "attr1", })), + schema.SimpleCoreAttribute(schema.SimpleNumberParams(schema.NumberParams{ + Name: "attr2", + Type: schema.AttributeTypeInteger(), + })), + schema.SimpleCoreAttribute(schema.SimpleNumberParams(schema.NumberParams{ + Name: "attr3", + Type: schema.AttributeTypeDecimal(), + })), schema.SimpleCoreAttribute(schema.SimpleStringParams(schema.StringParams{ Name: "multiValued", MultiValued: true,