@@ -622,45 +622,6 @@ type OneOfMedia struct {
622
622
Video * Video
623
623
}
624
624
625
- var polySamplePayload = `{
626
- "data": {
627
- "type": "blogs",
628
- "id": "3",
629
- "attributes": {
630
- "title": "Hello, World"
631
- },
632
- "relationships": {
633
- "hero-media": {
634
- "data": {
635
- "type": "videos",
636
- "id": "1",
637
- "attributes": {
638
- "captions": "It's Awesome!"
639
- }
640
- }
641
- },
642
- "media": {
643
- "data": [
644
- {
645
- "type": "images",
646
- "id": "1",
647
- "attributes": {
648
- "src": "/media/clear1x1.gif"
649
- }
650
- },
651
- {
652
- "type": "videos",
653
- "id": "2",
654
- "attributes": {
655
- "captions": "Oh, I didn't see you there"
656
- }
657
- }
658
- ]
659
- }
660
- }
661
- }
662
- }`
663
-
664
625
func Test_UnmarshalPayload_polymorphicRelations (t * testing.T ) {
665
626
type pointerToOne struct {
666
627
ID int `jsonapi:"primary,blogs"`
@@ -669,7 +630,58 @@ func Test_UnmarshalPayload_polymorphicRelations(t *testing.T) {
669
630
Media []* OneOfMedia `jsonapi:"polyrelation,media,omitempty"`
670
631
}
671
632
672
- in := bytes .NewReader ([]byte (polySamplePayload ))
633
+ in := bytes .NewReader ([]byte (`{
634
+ "data": {
635
+ "type": "blogs",
636
+ "id": "3",
637
+ "attributes": {
638
+ "title": "Hello, World"
639
+ },
640
+ "relationships": {
641
+ "hero-media": {
642
+ "data": {
643
+ "type": "videos",
644
+ "id": "1"
645
+ }
646
+ },
647
+ "media": {
648
+ "data": [
649
+ {
650
+ "type": "images",
651
+ "id": "1"
652
+ },
653
+ {
654
+ "type": "videos",
655
+ "id": "2"
656
+ }
657
+ ]
658
+ }
659
+ }
660
+ },
661
+ "included": [
662
+ {
663
+ "type": "videos",
664
+ "id": "1",
665
+ "attributes": {
666
+ "captions": "It's Awesome!"
667
+ }
668
+ },
669
+ {
670
+ "type": "images",
671
+ "id": "1",
672
+ "attributes": {
673
+ "src": "/media/clear1x1.gif"
674
+ }
675
+ },
676
+ {
677
+ "type": "videos",
678
+ "id": "2",
679
+ "attributes": {
680
+ "captions": "Oh, I didn't see you there"
681
+ }
682
+ }
683
+ ]
684
+ }` ))
673
685
out := new (pointerToOne )
674
686
675
687
if err := UnmarshalPayload (in , out ); err != nil {
@@ -688,12 +700,59 @@ func Test_UnmarshalPayload_polymorphicRelations(t *testing.T) {
688
700
t .Errorf ("expected Hero to be the expected video relation but got %+v" , out .Hero .Video )
689
701
}
690
702
703
+ // Unmarshals included records
691
704
if out .Media [0 ].Image == nil || out .Media [0 ].Image .Src != "/media/clear1x1.gif" {
692
- t .Errorf ("expected Media 0 to be the expected image relation but got %+v" , out .Media [0 ])
705
+ t .Errorf ("expected Media 0 to be the expected image relation but got %+v" , out .Media [0 ]. Image )
693
706
}
694
707
695
708
if out .Media [1 ].Video == nil || out .Media [1 ].Video .Captions != "Oh, I didn't see you there" {
696
- t .Errorf ("expected Media 0 to be the expected video relation but got %+v" , out .Media [1 ])
709
+ t .Errorf ("expected Media 1 to be the expected video relation but got %+v" , out .Media [1 ].Video )
710
+ }
711
+ }
712
+
713
+ func Test_UnmarshalPayload_polymorphicRelations_no_choice (t * testing.T ) {
714
+ type pointerToOne struct {
715
+ ID int `jsonapi:"primary,blogs"`
716
+ Title string `jsonapi:"attr,title"`
717
+ Hero * OneOfMedia `jsonapi:"polyrelation,hero-media,omitempty"`
718
+ }
719
+
720
+ in := bytes .NewReader ([]byte (`{
721
+ "data": {
722
+ "type": "blogs",
723
+ "id": "3",
724
+ "attributes": {
725
+ "title": "Hello, World"
726
+ },
727
+ "relationships": {
728
+ "hero-media": {
729
+ "data": {
730
+ "type": "absolutely-not",
731
+ "id": "1",
732
+ "attributes": {
733
+ "captions": "It's Awesome!"
734
+ }
735
+ }
736
+ }
737
+ }
738
+ }
739
+ }` ))
740
+ out := new (pointerToOne )
741
+
742
+ if err := UnmarshalPayload (in , out ); err != nil {
743
+ t .Fatal (err )
744
+ }
745
+
746
+ if out .Title != "Hello, World" {
747
+ t .Errorf ("expected Title %q but got %q" , "Hello, World" , out .Title )
748
+ }
749
+
750
+ if out .Hero == nil {
751
+ t .Fatal ("expected Hero to not be nil" )
752
+ }
753
+
754
+ if out .Hero .Image != nil || out .Hero .Video != nil {
755
+ t .Fatal ("expected both Hero fields to be nil" )
697
756
}
698
757
}
699
758
0 commit comments