Skip to content

Commit 6bdf239

Browse files
committed
Support public&private non-tagged fields in choice struct
1 parent 2a00bb5 commit 6bdf239

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

request.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,18 @@ func choiceStructMapping(choice reflect.Type) (result map[string]structFieldInde
193193
for i := 0; i < choice.NumField(); i++ {
194194
fieldType := choice.Field(i)
195195

196+
// Must be a pointer
196197
if fieldType.Type.Kind() != reflect.Ptr {
197198
continue
198199
}
199200

200201
subtype := fieldType.Type.Elem()
202+
203+
// Must be a pointer to struct
204+
if subtype.Kind() != reflect.Struct {
205+
continue
206+
}
207+
201208
if t, err := jsonapiTypeOfModel(subtype); err == nil {
202209
result[t] = structFieldIndex{
203210
Type: subtype,

request_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,10 @@ type Video struct {
618618
}
619619

620620
type OneOfMedia struct {
621-
Image *Image
622-
Video *Video
621+
Image *Image
622+
random int
623+
Video *Video
624+
RandomStuff *string
623625
}
624626

625627
func Test_UnmarshalPayload_polymorphicRelations(t *testing.T) {
@@ -774,8 +776,8 @@ func Test_choiceStructMapping(t *testing.T) {
774776
t.Errorf("expected \"images\" to be the first field, but got %d", imageField.FieldNum)
775777
}
776778
videoField, ok := result["videos"]
777-
if !ok || videoField.FieldNum != 1 {
778-
t.Errorf("expected \"videos\" to be the second field, but got %d", videoField.FieldNum)
779+
if !ok || videoField.FieldNum != 2 {
780+
t.Errorf("expected \"videos\" to be the third field, but got %d", videoField.FieldNum)
779781
}
780782
}
781783
}

0 commit comments

Comments
 (0)