Skip to content

Commit

Permalink
Use json.Number to decode patch operations
Browse files Browse the repository at this point in the history
This commit fixes the decoding of integer attributes in patches. Without
this change, integer attributes are decoded as float64, causing a schema
validation error.

Signed-off-by: Mariano Cano <[email protected]>
  • Loading branch information
maraino committed Jan 17, 2024
1 parent 941a5ea commit fe3e7ed
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
7 changes: 6 additions & 1 deletion internal/patch/patch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package patch

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand Down Expand Up @@ -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
}

Expand Down
48 changes: 47 additions & 1 deletion internal/patch/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions internal/patch/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit fe3e7ed

Please sign in to comment.