Skip to content
This repository was archived by the owner on Sep 4, 2025. It is now read-only.

Commit ed08d4f

Browse files
committed
refactor, consistency, add test to ensure we don't need additional type check
1 parent d05fcd9 commit ed08d4f

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

request.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,7 @@ func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node)
253253
break
254254
}
255255

256-
// As a final catch-all, ensure types line up to avoid a runtime panic.
257-
if fieldValue.Kind() != value.Kind() {
258-
return ErrInvalidType
259-
}
260-
assignValue(fieldValue, value)
256+
assign(fieldValue, value)
261257
} else if annotation == annotationRelation {
262258
isSlice := fieldValue.Type().Kind() == reflect.Slice
263259

@@ -355,10 +351,11 @@ func assign(field, value reflect.Value) {
355351
// initialize pointer so it's value
356352
// can be set by assignValue
357353
field.Set(reflect.New(field.Type().Elem()))
358-
assignValue(field.Elem(), value)
359-
} else {
360-
assignValue(field, value)
354+
field = field.Elem()
355+
361356
}
357+
358+
assignValue(field, value)
362359
}
363360

364361
// assign assigns the specified value to the field,

request_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,38 @@ func TestUnmarshalCustomTypeAttributes(t *testing.T) {
822822
}
823823
}
824824

825+
func TestUnmarshalCustomTypeAttributes_ErrInvalidType(t *testing.T) {
826+
data := map[string]interface{}{
827+
"data": map[string]interface{}{
828+
"type": "customtypes",
829+
"id": "1",
830+
"attributes": map[string]interface{}{
831+
"int": "bad",
832+
"intptr": 5,
833+
"intptrnull": nil,
834+
835+
"float": 1.5,
836+
"string": "Test",
837+
},
838+
},
839+
}
840+
payload, err := json.Marshal(data)
841+
if err != nil {
842+
t.Fatal(err)
843+
}
844+
845+
// Parse JSON API payload
846+
customAttributeTypes := new(CustomAttributeTypes)
847+
err = UnmarshalPayload(bytes.NewReader(payload), customAttributeTypes)
848+
if err == nil {
849+
t.Fatal("Expected an error unmarshalling the payload due to type mismatch, got none")
850+
}
851+
852+
if err != ErrInvalidType {
853+
t.Fatalf("Expected error to be %v, was %v", ErrInvalidType, err)
854+
}
855+
}
856+
825857
func samplePayloadWithoutIncluded() map[string]interface{} {
826858
return map[string]interface{}{
827859
"data": map[string]interface{}{

0 commit comments

Comments
 (0)