jsonapi-resources 0.4.4 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,9 +11,11 @@ class SerializerTest < ActionDispatch::IntegrationTest
11
11
 
12
12
  JSONAPI.configuration.json_key_format = :camelized_key
13
13
  JSONAPI.configuration.route_format = :camelized_route
14
+ JSONAPI.configuration.always_include_to_one_linkage_data = false
14
15
  end
15
16
 
16
17
  def after_teardown
18
+ JSONAPI.configuration.always_include_to_one_linkage_data = false
17
19
  JSONAPI.configuration.json_key_format = :underscored_key
18
20
  end
19
21
 
@@ -42,17 +44,12 @@ class SerializerTest < ActionDispatch::IntegrationTest
42
44
  links: {
43
45
  self: 'http://example.com/posts/1/relationships/section',
44
46
  related: 'http://example.com/posts/1/section'
45
- },
46
- data: nil
47
+ }
47
48
  },
48
49
  author: {
49
50
  links: {
50
51
  self: 'http://example.com/posts/1/relationships/author',
51
52
  related: 'http://example.com/posts/1/author'
52
- },
53
- data: {
54
- type: 'people',
55
- id: '1'
56
53
  }
57
54
  },
58
55
  tags: {
@@ -102,17 +99,12 @@ class SerializerTest < ActionDispatch::IntegrationTest
102
99
  links:{
103
100
  self: 'http://example.com/api/v1/posts/1/relationships/section',
104
101
  related: 'http://example.com/api/v1/posts/1/section'
105
- },
106
- data: nil
102
+ }
107
103
  },
108
104
  writer: {
109
105
  links:{
110
106
  self: 'http://example.com/api/v1/posts/1/relationships/writer',
111
107
  related: 'http://example.com/api/v1/posts/1/writer'
112
- },
113
- data: {
114
- type: 'writers',
115
- id: '1'
116
108
  }
117
109
  },
118
110
  comments: {
@@ -148,10 +140,6 @@ class SerializerTest < ActionDispatch::IntegrationTest
148
140
  links: {
149
141
  self: '/posts/1/relationships/author',
150
142
  related: '/posts/1/author'
151
- },
152
- data: {
153
- type: 'people',
154
- id: '1'
155
143
  }
156
144
  }
157
145
  }
@@ -186,8 +174,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
186
174
  links: {
187
175
  self: '/posts/1/relationships/section',
188
176
  related: '/posts/1/section'
189
- },
190
- data: nil
177
+ }
191
178
  },
192
179
  author: {
193
180
  links: {
@@ -242,18 +229,13 @@ class SerializerTest < ActionDispatch::IntegrationTest
242
229
  links: {
243
230
  self: '/people/1/relationships/preferences',
244
231
  related: '/people/1/preferences'
245
- },
246
- data: {
247
- type: 'preferences',
248
- id: '1'
249
232
  }
250
233
  },
251
234
  hairCut: {
252
235
  links: {
253
236
  self: "/people/1/relationships/hairCut",
254
237
  related: "/people/1/hairCut"
255
- },
256
- data: nil
238
+ }
257
239
  },
258
240
  vehicles: {
259
241
  links: {
@@ -294,8 +276,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
294
276
  links: {
295
277
  self: '/posts/1/relationships/section',
296
278
  related: '/posts/1/section'
297
- },
298
- data: nil
279
+ }
299
280
  },
300
281
  author: {
301
282
  links: {
@@ -350,18 +331,13 @@ class SerializerTest < ActionDispatch::IntegrationTest
350
331
  links: {
351
332
  self: '/people/1/relationships/preferences',
352
333
  related: '/people/1/preferences'
353
- },
354
- data: {
355
- type: 'preferences',
356
- id: '1'
357
334
  }
358
335
  },
359
336
  hair_cut: {
360
337
  links: {
361
338
  self: '/people/1/relationships/hairCut',
362
339
  related: '/people/1/hairCut'
363
- },
364
- data: nil
340
+ }
365
341
  },
366
342
  vehicles: {
367
343
  links: {
@@ -397,17 +373,12 @@ class SerializerTest < ActionDispatch::IntegrationTest
397
373
  links: {
398
374
  self: '/posts/1/relationships/section',
399
375
  related: '/posts/1/section'
400
- },
401
- data: nil
376
+ }
402
377
  },
403
378
  author: {
404
379
  links: {
405
380
  self: '/posts/1/relationships/author',
406
381
  related: '/posts/1/author'
407
- },
408
- data: {
409
- type: 'people',
410
- id: '1'
411
382
  }
412
383
  },
413
384
  tags: {
@@ -497,20 +468,12 @@ class SerializerTest < ActionDispatch::IntegrationTest
497
468
  links: {
498
469
  self: '/comments/1/relationships/author',
499
470
  related: '/comments/1/author'
500
- },
501
- data: {
502
- type: 'people',
503
- id: '1'
504
471
  }
505
472
  },
506
473
  post: {
507
474
  links: {
508
475
  self: '/comments/1/relationships/post',
509
476
  related: '/comments/1/post'
510
- },
511
- data: {
512
- type: 'posts',
513
- id: '1'
514
477
  }
515
478
  },
516
479
  tags: {
@@ -539,20 +502,12 @@ class SerializerTest < ActionDispatch::IntegrationTest
539
502
  links: {
540
503
  self: '/comments/2/relationships/author',
541
504
  related: '/comments/2/author'
542
- },
543
- data: {
544
- type: 'people',
545
- id: '2'
546
505
  }
547
506
  },
548
507
  post: {
549
508
  links: {
550
509
  self: '/comments/2/relationships/post',
551
510
  related: '/comments/2/post'
552
- },
553
- data: {
554
- type: 'posts',
555
- id: '1'
556
511
  }
557
512
  },
558
513
  tags: {
@@ -614,15 +569,13 @@ class SerializerTest < ActionDispatch::IntegrationTest
614
569
  links: {
615
570
  self: "/people/2/relationships/preferences",
616
571
  related: "/people/2/preferences"
617
- },
618
- data: nil
572
+ }
619
573
  },
620
574
  hairCut: {
621
575
  links: {
622
576
  self: "/people/2/relationships/hairCut",
623
577
  related: "/people/2/hairCut"
624
- },
625
- data: nil
578
+ }
626
579
  },
627
580
  vehicles: {
628
581
  links: {
@@ -647,20 +600,12 @@ class SerializerTest < ActionDispatch::IntegrationTest
647
600
  links: {
648
601
  self: '/comments/2/relationships/author',
649
602
  related: '/comments/2/author'
650
- },
651
- data: {
652
- type: 'people',
653
- id: '2'
654
603
  }
655
604
  },
656
605
  post: {
657
606
  links: {
658
607
  self: '/comments/2/relationships/post',
659
608
  related: '/comments/2/post'
660
- },
661
- data: {
662
- type: 'posts',
663
- id: '1'
664
609
  }
665
610
  },
666
611
  tags: {
@@ -685,20 +630,12 @@ class SerializerTest < ActionDispatch::IntegrationTest
685
630
  links: {
686
631
  self: '/comments/3/relationships/author',
687
632
  related: '/comments/3/author'
688
- },
689
- data: {
690
- type: 'people',
691
- id: '2'
692
633
  }
693
634
  },
694
635
  post: {
695
636
  links: {
696
637
  self: '/comments/3/relationships/post',
697
638
  related: '/comments/3/post'
698
- },
699
- data: {
700
- type: 'posts',
701
- id: '2'
702
639
  }
703
640
  },
704
641
  tags: {
@@ -715,13 +652,15 @@ class SerializerTest < ActionDispatch::IntegrationTest
715
652
  )
716
653
  end
717
654
 
718
- def test_serializer_array_of_resources
655
+ def test_serializer_array_of_resources_always_include_to_one_linkage_data
719
656
 
720
657
  posts = []
721
658
  Post.find(1, 2).each do |post|
722
659
  posts.push PostResource.new(post)
723
660
  end
724
661
 
662
+ JSONAPI.configuration.always_include_to_one_linkage_data = true
663
+
725
664
  assert_hash_equals(
726
665
  {
727
666
  data: [
@@ -1025,6 +964,282 @@ class SerializerTest < ActionDispatch::IntegrationTest
1025
964
  JSONAPI::ResourceSerializer.new(PostResource,
1026
965
  include: ['comments', 'comments.tags']).serialize_to_hash(posts)
1027
966
  )
967
+ JSONAPI.configuration.always_include_to_one_linkage_data = false
968
+ end
969
+
970
+ def test_serializer_array_of_resources
971
+
972
+ posts = []
973
+ Post.find(1, 2).each do |post|
974
+ posts.push PostResource.new(post)
975
+ end
976
+
977
+ assert_hash_equals(
978
+ {
979
+ data: [
980
+ {
981
+ type: 'posts',
982
+ id: '1',
983
+ attributes: {
984
+ title: 'New post',
985
+ body: 'A body!!!',
986
+ subject: 'New post'
987
+ },
988
+ links: {
989
+ self: '/posts/1'
990
+ },
991
+ relationships: {
992
+ section: {
993
+ links: {
994
+ self: '/posts/1/relationships/section',
995
+ related: '/posts/1/section'
996
+ }
997
+ },
998
+ author: {
999
+ links: {
1000
+ self: '/posts/1/relationships/author',
1001
+ related: '/posts/1/author'
1002
+ }
1003
+ },
1004
+ tags: {
1005
+ links: {
1006
+ self: '/posts/1/relationships/tags',
1007
+ related: '/posts/1/tags'
1008
+ }
1009
+ },
1010
+ comments: {
1011
+ links: {
1012
+ self: '/posts/1/relationships/comments',
1013
+ related: '/posts/1/comments'
1014
+ },
1015
+ data: [
1016
+ {type: 'comments', id: '1'},
1017
+ {type: 'comments', id: '2'}
1018
+ ]
1019
+ }
1020
+ }
1021
+ },
1022
+ {
1023
+ type: 'posts',
1024
+ id: '2',
1025
+ attributes: {
1026
+ title: 'JR Solves your serialization woes!',
1027
+ body: 'Use JR',
1028
+ subject: 'JR Solves your serialization woes!'
1029
+ },
1030
+ links: {
1031
+ self: '/posts/2'
1032
+ },
1033
+ relationships: {
1034
+ section: {
1035
+ links: {
1036
+ self: '/posts/2/relationships/section',
1037
+ related: '/posts/2/section'
1038
+ }
1039
+ },
1040
+ author: {
1041
+ links: {
1042
+ self: '/posts/2/relationships/author',
1043
+ related: '/posts/2/author'
1044
+ }
1045
+ },
1046
+ tags: {
1047
+ links: {
1048
+ self: '/posts/2/relationships/tags',
1049
+ related: '/posts/2/tags'
1050
+ }
1051
+ },
1052
+ comments: {
1053
+ links: {
1054
+ self: '/posts/2/relationships/comments',
1055
+ related: '/posts/2/comments'
1056
+ },
1057
+ data: [
1058
+ {type: 'comments', id: '3'}
1059
+ ]
1060
+ }
1061
+ }
1062
+ }
1063
+ ],
1064
+ included: [
1065
+ {
1066
+ type: 'tags',
1067
+ id: '1',
1068
+ attributes: {
1069
+ name: 'short'
1070
+ },
1071
+ links: {
1072
+ self: '/tags/1'
1073
+ },
1074
+ relationships: {
1075
+ posts: {
1076
+ links: {
1077
+ self: '/tags/1/relationships/posts',
1078
+ related: '/tags/1/posts'
1079
+ }
1080
+ }
1081
+ }
1082
+ },
1083
+ {
1084
+ type: 'tags',
1085
+ id: '2',
1086
+ attributes: {
1087
+ name: 'whiny'
1088
+ },
1089
+ links: {
1090
+ self: '/tags/2'
1091
+ },
1092
+ relationships: {
1093
+ posts: {
1094
+ links: {
1095
+ self: '/tags/2/relationships/posts',
1096
+ related: '/tags/2/posts'
1097
+ }
1098
+ }
1099
+ }
1100
+ },
1101
+ {
1102
+ type: 'tags',
1103
+ id: '4',
1104
+ attributes: {
1105
+ name: 'happy'
1106
+ },
1107
+ links: {
1108
+ self: '/tags/4'
1109
+ },
1110
+ relationships: {
1111
+ posts: {
1112
+ links: {
1113
+ self: '/tags/4/relationships/posts',
1114
+ related: '/tags/4/posts'
1115
+ }
1116
+ }
1117
+ }
1118
+ },
1119
+ {
1120
+ type: 'tags',
1121
+ id: '5',
1122
+ attributes: {
1123
+ name: 'JR'
1124
+ },
1125
+ links: {
1126
+ self: '/tags/5'
1127
+ },
1128
+ relationships: {
1129
+ posts: {
1130
+ links: {
1131
+ self: '/tags/5/relationships/posts',
1132
+ related: '/tags/5/posts'
1133
+ }
1134
+ }
1135
+ }
1136
+ },
1137
+ {
1138
+ type: 'comments',
1139
+ id: '1',
1140
+ attributes: {
1141
+ body: 'what a dumb post'
1142
+ },
1143
+ links: {
1144
+ self: '/comments/1'
1145
+ },
1146
+ relationships: {
1147
+ author: {
1148
+ links: {
1149
+ self: '/comments/1/relationships/author',
1150
+ related: '/comments/1/author'
1151
+ }
1152
+ },
1153
+ post: {
1154
+ links: {
1155
+ self: '/comments/1/relationships/post',
1156
+ related: '/comments/1/post'
1157
+ }
1158
+ },
1159
+ tags: {
1160
+ links: {
1161
+ self: '/comments/1/relationships/tags',
1162
+ related: '/comments/1/tags'
1163
+ },
1164
+ data: [
1165
+ {type: 'tags', id: '1'},
1166
+ {type: 'tags', id: '2'}
1167
+ ]
1168
+ }
1169
+ }
1170
+ },
1171
+ {
1172
+ type: 'comments',
1173
+ id: '2',
1174
+ attributes: {
1175
+ body: 'i liked it'
1176
+ },
1177
+ links: {
1178
+ self: '/comments/2'
1179
+ },
1180
+ relationships: {
1181
+ author: {
1182
+ links: {
1183
+ self: '/comments/2/relationships/author',
1184
+ related: '/comments/2/author'
1185
+ }
1186
+ },
1187
+ post: {
1188
+ links: {
1189
+ self: '/comments/2/relationships/post',
1190
+ related: '/comments/2/post'
1191
+ }
1192
+ },
1193
+ tags: {
1194
+ links: {
1195
+ self: '/comments/2/relationships/tags',
1196
+ related: '/comments/2/tags'
1197
+ },
1198
+ data: [
1199
+ {type: 'tags', id: '4'},
1200
+ {type: 'tags', id: '1'}
1201
+ ]
1202
+ }
1203
+ }
1204
+ },
1205
+ {
1206
+ type: 'comments',
1207
+ id: '3',
1208
+ attributes: {
1209
+ body: 'Thanks man. Great post. But what is JR?'
1210
+ },
1211
+ links: {
1212
+ self: '/comments/3'
1213
+ },
1214
+ relationships: {
1215
+ author: {
1216
+ links: {
1217
+ self: '/comments/3/relationships/author',
1218
+ related: '/comments/3/author'
1219
+ }
1220
+ },
1221
+ post: {
1222
+ links: {
1223
+ self: '/comments/3/relationships/post',
1224
+ related: '/comments/3/post'
1225
+ }
1226
+ },
1227
+ tags: {
1228
+ links: {
1229
+ self: '/comments/3/relationships/tags',
1230
+ related: '/comments/3/tags'
1231
+ },
1232
+ data: [
1233
+ {type: 'tags', id: '5'}
1234
+ ]
1235
+ }
1236
+ }
1237
+ }
1238
+ ]
1239
+ },
1240
+ JSONAPI::ResourceSerializer.new(PostResource,
1241
+ include: ['comments', 'comments.tags']).serialize_to_hash(posts)
1242
+ )
1028
1243
  end
1029
1244
 
1030
1245
  def test_serializer_array_of_resources_limited_fields
@@ -1141,10 +1356,6 @@ class SerializerTest < ActionDispatch::IntegrationTest
1141
1356
  links: {
1142
1357
  self: '/comments/1/relationships/post',
1143
1358
  related: '/comments/1/post'
1144
- },
1145
- data: {
1146
- type: 'posts',
1147
- id: '1'
1148
1359
  }
1149
1360
  }
1150
1361
  }
@@ -1163,10 +1374,6 @@ class SerializerTest < ActionDispatch::IntegrationTest
1163
1374
  links: {
1164
1375
  self: '/comments/2/relationships/post',
1165
1376
  related: '/comments/2/post'
1166
- },
1167
- data: {
1168
- type: 'posts',
1169
- id: '1'
1170
1377
  }
1171
1378
  }
1172
1379
  }
@@ -1185,10 +1392,6 @@ class SerializerTest < ActionDispatch::IntegrationTest
1185
1392
  links: {
1186
1393
  self: '/comments/3/relationships/post',
1187
1394
  related: '/comments/3/post'
1188
- },
1189
- data: {
1190
- type: 'posts',
1191
- id: '2'
1192
1395
  }
1193
1396
  }
1194
1397
  }
@@ -1300,8 +1503,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
1300
1503
  links: {
1301
1504
  self: '/planets/8/relationships/planetType',
1302
1505
  related: '/planets/8/planetType'
1303
- },
1304
- data: nil
1506
+ }
1305
1507
  },
1306
1508
  tags: {
1307
1509
  links: {
@@ -1436,8 +1638,7 @@ class SerializerTest < ActionDispatch::IntegrationTest
1436
1638
  links: {
1437
1639
  self: '/preferences/1/relationships/author',
1438
1640
  related: '/preferences/1/author'
1439
- },
1440
- data: nil
1641
+ }
1441
1642
  },
1442
1643
  friends: {
1443
1644
  links: {
@@ -1481,4 +1682,54 @@ class SerializerTest < ActionDispatch::IntegrationTest
1481
1682
  JSONAPI::ResourceSerializer.new(FactResource).serialize_to_hash(facts)
1482
1683
  )
1483
1684
  end
1685
+
1686
+ def test_serializer_to_one
1687
+ serialized = JSONAPI::ResourceSerializer.new(
1688
+ Api::V5::AuthorResource,
1689
+ include: ['author_detail']
1690
+ ).serialize_to_hash(Api::V5::AuthorResource.new(Person.find(1)))
1691
+
1692
+ assert_hash_equals(
1693
+ {
1694
+ data: {
1695
+ type: 'authors',
1696
+ id: '1',
1697
+ attributes: {
1698
+ name: 'Joe Author',
1699
+ },
1700
+ links: {
1701
+ self: '/api/v5/authors/1'
1702
+ },
1703
+ relationships: {
1704
+ posts: {
1705
+ links: {
1706
+ self: '/api/v5/authors/1/relationships/posts',
1707
+ related: '/api/v5/authors/1/posts'
1708
+ }
1709
+ },
1710
+ authorDetail: {
1711
+ links: {
1712
+ self: '/api/v5/authors/1/relationships/authorDetail',
1713
+ related: '/api/v5/authors/1/authorDetail'
1714
+ },
1715
+ data: {type: 'authorDetails', id: '1'}
1716
+ }
1717
+ }
1718
+ },
1719
+ included: [
1720
+ {
1721
+ type: 'authorDetails',
1722
+ id: '1',
1723
+ attributes: {
1724
+ authorStuff: 'blah blah'
1725
+ },
1726
+ links: {
1727
+ self: '/api/v5/authorDetails/1'
1728
+ }
1729
+ }
1730
+ ]
1731
+ },
1732
+ serialized
1733
+ )
1734
+ end
1484
1735
  end