jsonapi-resources 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +68 -1
- data/Rakefile +3 -7
- data/lib/jsonapi-resources.rb +1 -0
- data/lib/jsonapi/configuration.rb +29 -4
- data/lib/jsonapi/error_codes.rb +6 -2
- data/lib/jsonapi/exceptions.rb +92 -19
- data/lib/jsonapi/operation.rb +0 -18
- data/lib/jsonapi/paginator.rb +98 -0
- data/lib/jsonapi/request.rb +257 -182
- data/lib/jsonapi/resource.rb +58 -47
- data/lib/jsonapi/resource_controller.rb +85 -29
- data/lib/jsonapi/resource_serializer.rb +88 -33
- data/lib/jsonapi/resources/version.rb +1 -1
- data/lib/jsonapi/routing_ext.rb +38 -12
- data/test/controllers/controller_test.rb +761 -455
- data/test/fixtures/active_record.rb +90 -18
- data/test/integration/requests/request_test.rb +183 -25
- data/test/integration/routes/routes_test.rb +0 -5
- data/test/test_helper.rb +31 -7
- data/test/unit/operation/operations_processor_test.rb +28 -1
- data/test/unit/resource/resource_test.rb +4 -0
- data/test/unit/serializer/serializer_test.rb +882 -377
- metadata +3 -2
|
@@ -27,11 +27,6 @@ class RoutesTest < ActionDispatch::IntegrationTest
|
|
|
27
27
|
{controller: 'posts', action: 'destroy_association', post_id: '1', association: 'author'})
|
|
28
28
|
end
|
|
29
29
|
|
|
30
|
-
def test_routing_posts_links_author_create
|
|
31
|
-
assert_routing({path: '/posts/1/links/author', method: :post},
|
|
32
|
-
{controller: 'posts', action: 'create_association', post_id: '1', association: 'author'})
|
|
33
|
-
end
|
|
34
|
-
|
|
35
30
|
def test_routing_posts_links_author_update
|
|
36
31
|
assert_routing({path: '/posts/1/links/author', method: :put},
|
|
37
32
|
{controller: 'posts', action: 'update_association', post_id: '1', association: 'author'})
|
data/test/test_helper.rb
CHANGED
|
@@ -36,12 +36,14 @@ class TestApp < Rails::Application
|
|
|
36
36
|
|
|
37
37
|
#Raise errors on unsupported parameters
|
|
38
38
|
config.action_controller.action_on_unpermitted_parameters = :raise
|
|
39
|
+
|
|
40
|
+
config.active_record.schema_format = :none
|
|
39
41
|
end
|
|
40
42
|
|
|
41
43
|
TestApp.initialize!
|
|
42
44
|
|
|
43
45
|
require File.expand_path('../fixtures/active_record', __FILE__)
|
|
44
|
-
|
|
46
|
+
JSONAPI.configuration.route_format = :underscored_route
|
|
45
47
|
TestApp.routes.draw do
|
|
46
48
|
jsonapi_resources :authors
|
|
47
49
|
jsonapi_resources :people
|
|
@@ -76,10 +78,19 @@ TestApp.routes.draw do
|
|
|
76
78
|
jsonapi_resources :likes
|
|
77
79
|
end
|
|
78
80
|
|
|
81
|
+
JSONAPI.configuration.route_format = :underscored_route
|
|
79
82
|
namespace :v2 do
|
|
80
|
-
jsonapi_resources :authors
|
|
81
|
-
|
|
83
|
+
jsonapi_resources :authors do
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
jsonapi_resources :posts do
|
|
87
|
+
jsonapi_link :author, except: [:destroy]
|
|
88
|
+
end
|
|
89
|
+
|
|
82
90
|
jsonapi_resource :preferences
|
|
91
|
+
|
|
92
|
+
jsonapi_resources :books
|
|
93
|
+
jsonapi_resources :book_comments
|
|
83
94
|
end
|
|
84
95
|
|
|
85
96
|
namespace :v3 do
|
|
@@ -95,15 +106,28 @@ TestApp.routes.draw do
|
|
|
95
106
|
|
|
96
107
|
JSONAPI.configuration.route_format = :camelized_route
|
|
97
108
|
namespace :v4 do
|
|
98
|
-
jsonapi_resources :posts
|
|
99
|
-
|
|
100
|
-
|
|
109
|
+
jsonapi_resources :posts do
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
jsonapi_resources :expense_entries do
|
|
113
|
+
jsonapi_link :iso_currency
|
|
114
|
+
jsonapi_related_resource :iso_currency
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
jsonapi_resources :iso_currencies do
|
|
118
|
+
end
|
|
101
119
|
end
|
|
120
|
+
|
|
102
121
|
JSONAPI.configuration.route_format = :dasherized_route
|
|
103
122
|
namespace :v5 do
|
|
104
|
-
jsonapi_resources :posts
|
|
123
|
+
jsonapi_resources :posts do
|
|
124
|
+
end
|
|
125
|
+
|
|
105
126
|
jsonapi_resources :expense_entries
|
|
106
127
|
jsonapi_resources :iso_currencies
|
|
128
|
+
|
|
129
|
+
jsonapi_resources :employees
|
|
130
|
+
|
|
107
131
|
end
|
|
108
132
|
JSONAPI.configuration.route_format = :underscored_route
|
|
109
133
|
end
|
|
@@ -113,7 +113,6 @@ class OperationsProcessorTest < MiniTest::Unit::TestCase
|
|
|
113
113
|
planetoid = PlanetType.find(2)
|
|
114
114
|
assert_equal(saturn.planet_type_id, planetoid.id)
|
|
115
115
|
|
|
116
|
-
|
|
117
116
|
operations = [
|
|
118
117
|
JSONAPI::ReplaceHasOneAssociationOperation.new(PlanetResource, saturn.id, :planet_type, gas_giant.id)
|
|
119
118
|
]
|
|
@@ -129,6 +128,18 @@ class OperationsProcessorTest < MiniTest::Unit::TestCase
|
|
|
129
128
|
|
|
130
129
|
saturn.reload
|
|
131
130
|
assert_equal(saturn.planet_type_id, gas_giant.id)
|
|
131
|
+
|
|
132
|
+
# Reset
|
|
133
|
+
operations = [
|
|
134
|
+
JSONAPI::ReplaceHasOneAssociationOperation.new(PlanetResource, saturn.id, :planet_type, 5)
|
|
135
|
+
]
|
|
136
|
+
|
|
137
|
+
request = JSONAPI::Request.new
|
|
138
|
+
request.operations = operations
|
|
139
|
+
|
|
140
|
+
results = op.process(request)
|
|
141
|
+
saturn.reload
|
|
142
|
+
assert_equal(saturn.planet_type_id, 5)
|
|
132
143
|
end
|
|
133
144
|
|
|
134
145
|
def test_create_has_many_association
|
|
@@ -162,6 +173,14 @@ class OperationsProcessorTest < MiniTest::Unit::TestCase
|
|
|
162
173
|
assert_equal(betax.planet_type_id, gas_giant.id)
|
|
163
174
|
assert_equal(betay.planet_type_id, gas_giant.id)
|
|
164
175
|
assert_equal(betaz.planet_type_id, gas_giant.id)
|
|
176
|
+
|
|
177
|
+
# Reset
|
|
178
|
+
betax.planet_type_id = unknown.id
|
|
179
|
+
betay.planet_type_id = unknown.id
|
|
180
|
+
betaz.planet_type_id = unknown.id
|
|
181
|
+
betax.save!
|
|
182
|
+
betay.save!
|
|
183
|
+
betaz.save!
|
|
165
184
|
end
|
|
166
185
|
|
|
167
186
|
def test_replace_has_many_association
|
|
@@ -195,6 +214,14 @@ class OperationsProcessorTest < MiniTest::Unit::TestCase
|
|
|
195
214
|
assert_equal(betax.planet_type_id, gas_giant.id)
|
|
196
215
|
assert_equal(betay.planet_type_id, gas_giant.id)
|
|
197
216
|
assert_equal(betaz.planet_type_id, gas_giant.id)
|
|
217
|
+
|
|
218
|
+
# Reset
|
|
219
|
+
betax.planet_type_id = unknown.id
|
|
220
|
+
betay.planet_type_id = unknown.id
|
|
221
|
+
betaz.planet_type_id = unknown.id
|
|
222
|
+
betax.save!
|
|
223
|
+
betay.save!
|
|
224
|
+
betaz.save!
|
|
198
225
|
end
|
|
199
226
|
|
|
200
227
|
def test_replace_attributes
|
|
@@ -21,311 +21,574 @@ class SerializerTest < MiniTest::Unit::TestCase
|
|
|
21
21
|
|
|
22
22
|
assert_hash_equals(
|
|
23
23
|
{
|
|
24
|
-
|
|
24
|
+
data: {
|
|
25
|
+
type: 'posts',
|
|
25
26
|
id: '1',
|
|
26
27
|
title: 'New post',
|
|
27
28
|
body: 'A body!!!',
|
|
28
29
|
subject: 'New post',
|
|
29
30
|
links: {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
self: 'http://example.com/posts/1',
|
|
32
|
+
section: {
|
|
33
|
+
self: 'http://example.com/posts/1/links/section',
|
|
34
|
+
resource: 'http://example.com/posts/1/section',
|
|
35
|
+
type: 'sections',
|
|
36
|
+
id: nil
|
|
37
|
+
},
|
|
38
|
+
author: {
|
|
39
|
+
self: 'http://example.com/posts/1/links/author',
|
|
40
|
+
resource: 'http://example.com/posts/1/author',
|
|
41
|
+
type: 'people',
|
|
42
|
+
id: '1'
|
|
43
|
+
},
|
|
44
|
+
tags: {
|
|
45
|
+
self: 'http://example.com/posts/1/links/tags',
|
|
46
|
+
resource: 'http://example.com/posts/1/tags'
|
|
47
|
+
},
|
|
48
|
+
comments: {
|
|
49
|
+
self: 'http://example.com/posts/1/links/comments',
|
|
50
|
+
resource: 'http://example.com/posts/1/comments'
|
|
51
|
+
}
|
|
34
52
|
}
|
|
35
53
|
}
|
|
36
54
|
},
|
|
37
|
-
JSONAPI::ResourceSerializer.new(PostResource
|
|
38
|
-
|
|
55
|
+
JSONAPI::ResourceSerializer.new(PostResource,
|
|
56
|
+
base_url: 'http://example.com').serialize_to_hash(PostResource.new(@post))
|
|
57
|
+
)
|
|
39
58
|
end
|
|
40
59
|
|
|
41
60
|
def test_serializer_namespaced_resource
|
|
42
61
|
assert_hash_equals(
|
|
43
62
|
{
|
|
44
|
-
|
|
63
|
+
data: {
|
|
64
|
+
type: 'posts',
|
|
45
65
|
id: '1',
|
|
46
66
|
title: 'New post',
|
|
47
67
|
body: 'A body!!!',
|
|
48
68
|
subject: 'New post',
|
|
49
69
|
links: {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
70
|
+
self: 'http://example.com/api/v1/posts/1',
|
|
71
|
+
section: {
|
|
72
|
+
self: 'http://example.com/api/v1/posts/1/links/section',
|
|
73
|
+
resource: 'http://example.com/api/v1/posts/1/section',
|
|
74
|
+
type: 'sections',
|
|
75
|
+
id: nil
|
|
76
|
+
},
|
|
77
|
+
writer: {
|
|
78
|
+
self: 'http://example.com/api/v1/posts/1/links/writer',
|
|
79
|
+
resource: 'http://example.com/api/v1/posts/1/writer',
|
|
80
|
+
type: 'writers',
|
|
81
|
+
id: '1'
|
|
82
|
+
},
|
|
83
|
+
comments: {
|
|
84
|
+
self: 'http://example.com/api/v1/posts/1/links/comments',
|
|
85
|
+
resource: 'http://example.com/api/v1/posts/1/comments'
|
|
86
|
+
}
|
|
53
87
|
}
|
|
54
88
|
}
|
|
55
89
|
},
|
|
56
|
-
JSONAPI::ResourceSerializer.new(Api::V1::PostResource
|
|
57
|
-
|
|
90
|
+
JSONAPI::ResourceSerializer.new(Api::V1::PostResource,
|
|
91
|
+
base_url: 'http://example.com').serialize_to_hash(
|
|
92
|
+
Api::V1::PostResource.new(@post))
|
|
93
|
+
)
|
|
58
94
|
end
|
|
59
95
|
|
|
60
96
|
def test_serializer_limited_fieldset
|
|
61
97
|
|
|
62
98
|
assert_hash_equals(
|
|
63
99
|
{
|
|
64
|
-
|
|
100
|
+
data: {
|
|
101
|
+
type: 'posts',
|
|
65
102
|
id: '1',
|
|
66
103
|
title: 'New post',
|
|
67
104
|
links: {
|
|
68
|
-
|
|
105
|
+
self: '/posts/1',
|
|
106
|
+
author: {
|
|
107
|
+
self: '/posts/1/links/author',
|
|
108
|
+
resource: '/posts/1/author',
|
|
109
|
+
type: 'people',
|
|
110
|
+
id: '1'
|
|
111
|
+
}
|
|
69
112
|
}
|
|
70
113
|
}
|
|
71
114
|
},
|
|
72
|
-
JSONAPI::ResourceSerializer.new(PostResource
|
|
73
|
-
|
|
74
|
-
|
|
115
|
+
JSONAPI::ResourceSerializer.new(PostResource,
|
|
116
|
+
fields: {posts: [:id, :title, :author]}).serialize_to_hash(PostResource.new(@post))
|
|
117
|
+
)
|
|
75
118
|
end
|
|
76
119
|
|
|
77
120
|
def test_serializer_include
|
|
78
121
|
|
|
79
122
|
assert_hash_equals(
|
|
80
123
|
{
|
|
81
|
-
|
|
124
|
+
data: {
|
|
125
|
+
type: 'posts',
|
|
82
126
|
id: '1',
|
|
83
127
|
title: 'New post',
|
|
84
128
|
body: 'A body!!!',
|
|
85
129
|
subject: 'New post',
|
|
86
130
|
links: {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
131
|
+
self: '/posts/1',
|
|
132
|
+
section: {
|
|
133
|
+
self: '/posts/1/links/section',
|
|
134
|
+
resource: '/posts/1/section',
|
|
135
|
+
type: 'sections',
|
|
136
|
+
id: nil
|
|
137
|
+
},
|
|
138
|
+
author: {
|
|
139
|
+
self: '/posts/1/links/author',
|
|
140
|
+
resource: '/posts/1/author',
|
|
141
|
+
type: 'people',
|
|
142
|
+
id: '1'
|
|
143
|
+
},
|
|
144
|
+
tags: {
|
|
145
|
+
self: '/posts/1/links/tags',
|
|
146
|
+
resource: '/posts/1/tags'
|
|
147
|
+
},
|
|
148
|
+
comments: {
|
|
149
|
+
self: '/posts/1/links/comments',
|
|
150
|
+
resource: '/posts/1/comments'
|
|
151
|
+
}
|
|
91
152
|
}
|
|
92
153
|
},
|
|
93
|
-
linked:
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
154
|
+
linked: [
|
|
155
|
+
{
|
|
156
|
+
type: 'people',
|
|
157
|
+
id: '1',
|
|
158
|
+
name: 'Joe Author',
|
|
159
|
+
email: 'joe@xyz.fake',
|
|
160
|
+
dateJoined: '2013-08-07 16:25:00 -0400',
|
|
161
|
+
links: {
|
|
162
|
+
self: '/people/1',
|
|
163
|
+
comments: {
|
|
164
|
+
self: '/people/1/links/comments',
|
|
165
|
+
resource: '/people/1/comments'
|
|
166
|
+
},
|
|
167
|
+
posts: {
|
|
168
|
+
self: '/people/1/links/posts',
|
|
169
|
+
resource: '/people/1/posts'
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
]
|
|
105
174
|
},
|
|
106
|
-
JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash(
|
|
107
|
-
PostResource.new(@post)
|
|
175
|
+
JSONAPI::ResourceSerializer.new(PostResource, include: [:author]).serialize_to_hash(
|
|
176
|
+
PostResource.new(@post)))
|
|
108
177
|
end
|
|
109
178
|
|
|
110
179
|
def test_serializer_key_format
|
|
111
180
|
|
|
112
181
|
assert_hash_equals(
|
|
113
182
|
{
|
|
114
|
-
|
|
183
|
+
data: {
|
|
184
|
+
type: 'posts',
|
|
115
185
|
id: '1',
|
|
116
186
|
title: 'New post',
|
|
117
187
|
body: 'A body!!!',
|
|
118
188
|
subject: 'New post',
|
|
119
189
|
links: {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
190
|
+
self: '/posts/1',
|
|
191
|
+
section: {
|
|
192
|
+
self: '/posts/1/links/section',
|
|
193
|
+
resource: '/posts/1/section',
|
|
194
|
+
type: 'sections',
|
|
195
|
+
id: nil
|
|
196
|
+
},
|
|
197
|
+
author: {
|
|
198
|
+
self: '/posts/1/links/author',
|
|
199
|
+
resource: '/posts/1/author',
|
|
200
|
+
type: 'people',
|
|
201
|
+
id: '1'
|
|
202
|
+
},
|
|
203
|
+
tags: {
|
|
204
|
+
self: '/posts/1/links/tags',
|
|
205
|
+
resource: '/posts/1/tags'
|
|
206
|
+
},
|
|
207
|
+
comments: {
|
|
208
|
+
self: '/posts/1/links/comments',
|
|
209
|
+
resource: '/posts/1/comments'
|
|
210
|
+
}
|
|
124
211
|
}
|
|
125
212
|
},
|
|
126
|
-
linked:
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
213
|
+
linked: [
|
|
214
|
+
{
|
|
215
|
+
type: 'people',
|
|
216
|
+
id: '1',
|
|
217
|
+
name: 'Joe Author',
|
|
218
|
+
email: 'joe@xyz.fake',
|
|
219
|
+
date_joined: '2013-08-07 16:25:00 -0400',
|
|
220
|
+
links: {
|
|
221
|
+
self: '/people/1',
|
|
222
|
+
comments: {
|
|
223
|
+
self: '/people/1/links/comments',
|
|
224
|
+
resource: '/people/1/comments'
|
|
225
|
+
},
|
|
226
|
+
posts: {
|
|
227
|
+
self: '/people/1/links/posts',
|
|
228
|
+
resource: '/people/1/posts'
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
]
|
|
138
233
|
},
|
|
139
|
-
JSONAPI::ResourceSerializer.new(PostResource
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
234
|
+
JSONAPI::ResourceSerializer.new(PostResource,
|
|
235
|
+
include: [:author],
|
|
236
|
+
key_formatter: UnderscoredKeyFormatter).serialize_to_hash(PostResource.new(@post))
|
|
237
|
+
)
|
|
143
238
|
end
|
|
144
239
|
|
|
145
240
|
def test_serializer_include_sub_objects
|
|
146
241
|
|
|
147
242
|
assert_hash_equals(
|
|
148
243
|
{
|
|
149
|
-
|
|
244
|
+
data: {
|
|
245
|
+
type: 'posts',
|
|
150
246
|
id: '1',
|
|
151
247
|
title: 'New post',
|
|
152
248
|
body: 'A body!!!',
|
|
153
249
|
subject: 'New post',
|
|
154
250
|
links: {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
251
|
+
self: '/posts/1',
|
|
252
|
+
section: {
|
|
253
|
+
self: '/posts/1/links/section',
|
|
254
|
+
resource: '/posts/1/section',
|
|
255
|
+
type: 'sections',
|
|
256
|
+
id: nil
|
|
257
|
+
},
|
|
258
|
+
author: {
|
|
259
|
+
self: '/posts/1/links/author',
|
|
260
|
+
resource: '/posts/1/author',
|
|
261
|
+
type: 'people',
|
|
262
|
+
id: '1'
|
|
263
|
+
},
|
|
264
|
+
tags: {
|
|
265
|
+
self: '/posts/1/links/tags',
|
|
266
|
+
resource: '/posts/1/tags'
|
|
267
|
+
},
|
|
268
|
+
comments: {
|
|
269
|
+
self: '/posts/1/links/comments',
|
|
270
|
+
resource: '/posts/1/comments',
|
|
271
|
+
type: 'comments',
|
|
272
|
+
ids: ['1', '2']
|
|
273
|
+
}
|
|
159
274
|
}
|
|
160
275
|
},
|
|
161
|
-
linked:
|
|
162
|
-
tags: [
|
|
276
|
+
linked: [
|
|
163
277
|
{
|
|
278
|
+
type: 'tags',
|
|
164
279
|
id: '1',
|
|
165
280
|
name: 'short',
|
|
166
281
|
links: {
|
|
167
|
-
|
|
282
|
+
self: '/tags/1',
|
|
283
|
+
posts: {
|
|
284
|
+
self: '/tags/1/links/posts',
|
|
285
|
+
resource: '/tags/1/posts'
|
|
286
|
+
}
|
|
168
287
|
}
|
|
169
288
|
},
|
|
170
289
|
{
|
|
290
|
+
type: 'tags',
|
|
171
291
|
id: '2',
|
|
172
292
|
name: 'whiny',
|
|
173
293
|
links: {
|
|
174
|
-
|
|
294
|
+
self: '/tags/2',
|
|
295
|
+
posts: {
|
|
296
|
+
self: '/tags/2/links/posts',
|
|
297
|
+
resource: '/tags/2/posts'
|
|
298
|
+
}
|
|
175
299
|
}
|
|
176
300
|
},
|
|
177
301
|
{
|
|
302
|
+
type: 'tags',
|
|
178
303
|
id: '4',
|
|
179
304
|
name: 'happy',
|
|
180
305
|
links: {
|
|
181
|
-
|
|
306
|
+
self: '/tags/4',
|
|
307
|
+
posts: {
|
|
308
|
+
self: '/tags/4/links/posts',
|
|
309
|
+
resource: '/tags/4/posts',
|
|
310
|
+
}
|
|
182
311
|
}
|
|
183
|
-
}
|
|
184
|
-
],
|
|
185
|
-
comments: [
|
|
312
|
+
},
|
|
186
313
|
{
|
|
314
|
+
type: 'comments',
|
|
187
315
|
id: '1',
|
|
188
316
|
body: 'what a dumb post',
|
|
189
317
|
links: {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
318
|
+
self: '/comments/1',
|
|
319
|
+
author: {
|
|
320
|
+
self: '/comments/1/links/author',
|
|
321
|
+
resource: '/comments/1/author',
|
|
322
|
+
type: 'people',
|
|
323
|
+
id: '1'
|
|
324
|
+
},
|
|
325
|
+
post: {
|
|
326
|
+
self: '/comments/1/links/post',
|
|
327
|
+
resource: '/comments/1/post',
|
|
328
|
+
type: 'posts',
|
|
329
|
+
id: '1'
|
|
330
|
+
},
|
|
331
|
+
tags: {
|
|
332
|
+
self: '/comments/1/links/tags',
|
|
333
|
+
resource: '/comments/1/tags',
|
|
334
|
+
type: 'tags',
|
|
335
|
+
ids: ['1', '2']
|
|
336
|
+
}
|
|
193
337
|
}
|
|
194
338
|
},
|
|
195
339
|
{
|
|
340
|
+
type: 'comments',
|
|
196
341
|
id: '2',
|
|
197
342
|
body: 'i liked it',
|
|
198
343
|
links: {
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
344
|
+
self: '/comments/2',
|
|
345
|
+
author: {
|
|
346
|
+
self: '/comments/2/links/author',
|
|
347
|
+
resource: '/comments/2/author',
|
|
348
|
+
type: 'people',
|
|
349
|
+
id: '2'
|
|
350
|
+
},
|
|
351
|
+
post: {
|
|
352
|
+
self: '/comments/2/links/post',
|
|
353
|
+
resource: '/comments/2/post',
|
|
354
|
+
type: 'posts',
|
|
355
|
+
id: '1'
|
|
356
|
+
},
|
|
357
|
+
tags: {
|
|
358
|
+
self: '/comments/2/links/tags',
|
|
359
|
+
resource: '/comments/2/tags',
|
|
360
|
+
type: 'tags',
|
|
361
|
+
ids: ['4', '1']
|
|
362
|
+
}
|
|
202
363
|
}
|
|
203
364
|
}
|
|
204
365
|
]
|
|
205
|
-
}
|
|
206
366
|
},
|
|
207
|
-
JSONAPI::ResourceSerializer.new(PostResource
|
|
208
|
-
|
|
367
|
+
JSONAPI::ResourceSerializer.new(PostResource,
|
|
368
|
+
include: [:comments, 'comments.tags']).serialize_to_hash(PostResource.new(@post))
|
|
369
|
+
)
|
|
209
370
|
end
|
|
210
371
|
|
|
211
372
|
def test_serializer_include_has_many_sub_objects_only
|
|
212
373
|
|
|
213
374
|
assert_hash_equals(
|
|
214
375
|
{
|
|
215
|
-
|
|
376
|
+
data: {
|
|
377
|
+
type: 'posts',
|
|
216
378
|
id: '1',
|
|
217
379
|
title: 'New post',
|
|
218
380
|
body: 'A body!!!',
|
|
219
381
|
subject: 'New post',
|
|
220
382
|
links: {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
383
|
+
self: '/posts/1',
|
|
384
|
+
section: {
|
|
385
|
+
self: '/posts/1/links/section',
|
|
386
|
+
resource: '/posts/1/section',
|
|
387
|
+
type: 'sections',
|
|
388
|
+
id: nil
|
|
389
|
+
},
|
|
390
|
+
author: {
|
|
391
|
+
self: '/posts/1/links/author',
|
|
392
|
+
resource: '/posts/1/author',
|
|
393
|
+
type: 'people',
|
|
394
|
+
id: '1'
|
|
395
|
+
},
|
|
396
|
+
tags: {
|
|
397
|
+
self: '/posts/1/links/tags',
|
|
398
|
+
resource: '/posts/1/tags'
|
|
399
|
+
},
|
|
400
|
+
comments: {
|
|
401
|
+
self: '/posts/1/links/comments',
|
|
402
|
+
resource: '/posts/1/comments'
|
|
403
|
+
}
|
|
225
404
|
}
|
|
226
405
|
},
|
|
227
|
-
linked:
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
406
|
+
linked: [
|
|
407
|
+
{
|
|
408
|
+
type: 'tags',
|
|
409
|
+
id: '1',
|
|
410
|
+
name: 'short',
|
|
411
|
+
links: {
|
|
412
|
+
self: '/tags/1',
|
|
413
|
+
posts: {
|
|
414
|
+
self: '/tags/1/links/posts',
|
|
415
|
+
resource: '/tags/1/posts'
|
|
234
416
|
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
417
|
+
}
|
|
418
|
+
},
|
|
419
|
+
{
|
|
420
|
+
type: 'tags',
|
|
421
|
+
id: '2',
|
|
422
|
+
name: 'whiny',
|
|
423
|
+
links: {
|
|
424
|
+
self: '/tags/2',
|
|
425
|
+
posts: {
|
|
426
|
+
self: '/tags/2/links/posts',
|
|
427
|
+
resource: '/tags/2/posts'
|
|
241
428
|
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
429
|
+
}
|
|
430
|
+
},
|
|
431
|
+
{
|
|
432
|
+
type: 'tags',
|
|
433
|
+
id: '4',
|
|
434
|
+
name: 'happy',
|
|
435
|
+
links: {
|
|
436
|
+
self: '/tags/4',
|
|
437
|
+
posts: {
|
|
438
|
+
self: '/tags/4/links/posts',
|
|
439
|
+
resource: '/tags/4/posts',
|
|
248
440
|
}
|
|
249
441
|
}
|
|
250
|
-
|
|
251
|
-
|
|
442
|
+
}
|
|
443
|
+
]
|
|
252
444
|
},
|
|
253
|
-
JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash(
|
|
254
|
-
|
|
445
|
+
JSONAPI::ResourceSerializer.new(PostResource, include: ['comments.tags']).serialize_to_hash(PostResource.new(@post))
|
|
446
|
+
)
|
|
255
447
|
end
|
|
256
448
|
|
|
257
449
|
def test_serializer_include_has_one_sub_objects_only
|
|
258
450
|
|
|
259
451
|
assert_hash_equals(
|
|
260
452
|
{
|
|
261
|
-
|
|
453
|
+
data: {
|
|
454
|
+
type: 'posts',
|
|
262
455
|
id: '1',
|
|
263
456
|
title: 'New post',
|
|
264
457
|
body: 'A body!!!',
|
|
265
458
|
subject: 'New post',
|
|
266
459
|
links: {
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
460
|
+
self: '/posts/1',
|
|
461
|
+
section: {
|
|
462
|
+
self: '/posts/1/links/section',
|
|
463
|
+
resource: '/posts/1/section',
|
|
464
|
+
type: 'sections',
|
|
465
|
+
id: nil
|
|
466
|
+
},
|
|
467
|
+
author: {
|
|
468
|
+
self: '/posts/1/links/author',
|
|
469
|
+
resource: '/posts/1/author',
|
|
470
|
+
type: 'people',
|
|
471
|
+
id: '1'
|
|
472
|
+
},
|
|
473
|
+
tags: {
|
|
474
|
+
self: '/posts/1/links/tags',
|
|
475
|
+
resource: '/posts/1/tags'
|
|
476
|
+
},
|
|
477
|
+
comments: {
|
|
478
|
+
self: '/posts/1/links/comments',
|
|
479
|
+
resource: '/posts/1/comments'
|
|
480
|
+
}
|
|
271
481
|
}
|
|
272
482
|
},
|
|
273
|
-
linked:
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
483
|
+
linked: [
|
|
484
|
+
{
|
|
485
|
+
type: 'comments',
|
|
486
|
+
id: '1',
|
|
487
|
+
body: 'what a dumb post',
|
|
488
|
+
links: {
|
|
489
|
+
self: '/comments/1',
|
|
490
|
+
author: {
|
|
491
|
+
self: '/comments/1/links/author',
|
|
492
|
+
resource: '/comments/1/author',
|
|
493
|
+
type: 'people',
|
|
494
|
+
id: '1'
|
|
495
|
+
},
|
|
496
|
+
post: {
|
|
497
|
+
self: '/comments/1/links/post',
|
|
498
|
+
resource: '/comments/1/post',
|
|
499
|
+
type: 'posts',
|
|
500
|
+
id: '1'
|
|
501
|
+
},
|
|
502
|
+
tags: {
|
|
503
|
+
self: '/comments/1/links/tags',
|
|
504
|
+
resource: '/comments/1/tags'
|
|
282
505
|
}
|
|
283
506
|
}
|
|
284
|
-
|
|
285
|
-
|
|
507
|
+
}
|
|
508
|
+
]
|
|
286
509
|
},
|
|
287
|
-
JSONAPI::ResourceSerializer.new(PostResource
|
|
288
|
-
|
|
510
|
+
JSONAPI::ResourceSerializer.new(PostResource,
|
|
511
|
+
include: ['author.comments']).serialize_to_hash(PostResource.new(@post))
|
|
512
|
+
)
|
|
289
513
|
end
|
|
290
514
|
|
|
291
515
|
def test_serializer_different_foreign_key
|
|
292
516
|
|
|
293
517
|
assert_hash_equals(
|
|
294
518
|
{
|
|
295
|
-
|
|
519
|
+
data: {
|
|
520
|
+
type: 'people',
|
|
296
521
|
id: '2',
|
|
297
522
|
name: 'Fred Reader',
|
|
298
523
|
email: 'fred@xyz.fake',
|
|
299
524
|
dateJoined: '2013-10-31 16:25:00 -0400',
|
|
300
525
|
links: {
|
|
301
|
-
|
|
302
|
-
|
|
526
|
+
self: '/people/2',
|
|
527
|
+
posts: {
|
|
528
|
+
self: '/people/2/links/posts',
|
|
529
|
+
resource: '/people/2/posts'
|
|
530
|
+
},
|
|
531
|
+
comments: {
|
|
532
|
+
self: '/people/2/links/comments',
|
|
533
|
+
resource: '/people/2/comments',
|
|
534
|
+
type: 'comments',
|
|
535
|
+
ids: ['2', '3']
|
|
536
|
+
}
|
|
303
537
|
}
|
|
304
538
|
},
|
|
305
|
-
linked:
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
539
|
+
linked: [
|
|
540
|
+
{
|
|
541
|
+
type: 'comments',
|
|
542
|
+
id: '2',
|
|
543
|
+
body: 'i liked it',
|
|
544
|
+
links: {
|
|
545
|
+
self: '/comments/2',
|
|
546
|
+
author: {
|
|
547
|
+
self: '/comments/2/links/author',
|
|
548
|
+
resource: '/comments/2/author',
|
|
549
|
+
type: 'people',
|
|
550
|
+
id: '2'
|
|
551
|
+
},
|
|
552
|
+
post: {
|
|
553
|
+
self: '/comments/2/links/post',
|
|
554
|
+
resource: '/comments/2/post',
|
|
555
|
+
type: 'posts',
|
|
556
|
+
id: '1'
|
|
557
|
+
},
|
|
558
|
+
tags: {
|
|
559
|
+
self: '/comments/2/links/tags',
|
|
560
|
+
resource: '/comments/2/tags'
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
},
|
|
564
|
+
{
|
|
565
|
+
type: 'comments',
|
|
566
|
+
id: '3',
|
|
567
|
+
body: 'Thanks man. Great post. But what is JR?',
|
|
568
|
+
links: {
|
|
569
|
+
self: '/comments/3',
|
|
570
|
+
author: {
|
|
571
|
+
self: '/comments/3/links/author',
|
|
572
|
+
resource: '/comments/3/author',
|
|
573
|
+
type: 'people',
|
|
574
|
+
id: '2'
|
|
575
|
+
},
|
|
576
|
+
post: {
|
|
577
|
+
self: '/comments/3/links/post',
|
|
578
|
+
resource: '/comments/3/post',
|
|
579
|
+
type: 'posts',
|
|
580
|
+
id: '2'
|
|
581
|
+
},
|
|
582
|
+
tags: {
|
|
583
|
+
self: '/comments/3/links/tags',
|
|
584
|
+
resource: '/comments/3/tags'
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
]
|
|
326
589
|
},
|
|
327
|
-
JSONAPI::ResourceSerializer.new(PersonResource).serialize_to_hash(
|
|
328
|
-
|
|
590
|
+
JSONAPI::ResourceSerializer.new(PersonResource, include: ['comments']).serialize_to_hash(PersonResource.new(@fred))
|
|
591
|
+
)
|
|
329
592
|
end
|
|
330
593
|
|
|
331
594
|
def test_serializer_array_of_resources
|
|
@@ -337,94 +600,204 @@ class SerializerTest < MiniTest::Unit::TestCase
|
|
|
337
600
|
|
|
338
601
|
assert_hash_equals(
|
|
339
602
|
{
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
posts: :not_nil
|
|
603
|
+
data: [
|
|
604
|
+
{
|
|
605
|
+
type: 'posts',
|
|
606
|
+
id: '1',
|
|
607
|
+
title: 'New post',
|
|
608
|
+
body: 'A body!!!',
|
|
609
|
+
subject: 'New post',
|
|
610
|
+
links: {
|
|
611
|
+
self: '/posts/1',
|
|
612
|
+
section: {
|
|
613
|
+
self: '/posts/1/links/section',
|
|
614
|
+
resource: '/posts/1/section',
|
|
615
|
+
type: 'sections',
|
|
616
|
+
id: nil
|
|
617
|
+
},
|
|
618
|
+
author: {
|
|
619
|
+
self: '/posts/1/links/author',
|
|
620
|
+
resource: '/posts/1/author',
|
|
621
|
+
type: 'people',
|
|
622
|
+
id: '1'
|
|
623
|
+
},
|
|
624
|
+
tags: {
|
|
625
|
+
self: '/posts/1/links/tags',
|
|
626
|
+
resource: '/posts/1/tags'
|
|
627
|
+
},
|
|
628
|
+
comments: {
|
|
629
|
+
self: '/posts/1/links/comments',
|
|
630
|
+
resource: '/posts/1/comments',
|
|
631
|
+
type: 'comments',
|
|
632
|
+
ids: ['1', '2']
|
|
371
633
|
}
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
634
|
+
}
|
|
635
|
+
},
|
|
636
|
+
{
|
|
637
|
+
type: 'posts',
|
|
638
|
+
id: '2',
|
|
639
|
+
title: 'JR Solves your serialization woes!',
|
|
640
|
+
body: 'Use JR',
|
|
641
|
+
subject: 'JR Solves your serialization woes!',
|
|
642
|
+
links: {
|
|
643
|
+
self: '/posts/2',
|
|
644
|
+
section: {
|
|
645
|
+
self: '/posts/2/links/section',
|
|
646
|
+
resource: '/posts/2/section',
|
|
647
|
+
type: 'sections',
|
|
648
|
+
id: '3'
|
|
649
|
+
},
|
|
650
|
+
author: {
|
|
651
|
+
self: '/posts/2/links/author',
|
|
652
|
+
resource: '/posts/2/author',
|
|
653
|
+
type: 'people',
|
|
654
|
+
id: '1'
|
|
655
|
+
},
|
|
656
|
+
tags: {
|
|
657
|
+
self: '/posts/2/links/tags',
|
|
658
|
+
resource: '/posts/2/tags'
|
|
659
|
+
},
|
|
660
|
+
comments: {
|
|
661
|
+
self: '/posts/2/links/comments',
|
|
662
|
+
resource: '/posts/2/comments',
|
|
663
|
+
type: 'comments',
|
|
664
|
+
ids: ['3']
|
|
378
665
|
}
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
],
|
|
669
|
+
linked: [
|
|
670
|
+
{
|
|
671
|
+
type: 'tags',
|
|
672
|
+
id: '1',
|
|
673
|
+
name: 'short',
|
|
674
|
+
links: {
|
|
675
|
+
self: '/tags/1',
|
|
676
|
+
posts: {
|
|
677
|
+
self: '/tags/1/links/posts',
|
|
678
|
+
resource: '/tags/1/posts'
|
|
385
679
|
}
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
680
|
+
}
|
|
681
|
+
},
|
|
682
|
+
{
|
|
683
|
+
type: 'tags',
|
|
684
|
+
id: '2',
|
|
685
|
+
name: 'whiny',
|
|
686
|
+
links: {
|
|
687
|
+
self: '/tags/2',
|
|
688
|
+
posts: {
|
|
689
|
+
self: '/tags/2/links/posts',
|
|
690
|
+
resource: '/tags/2/posts'
|
|
392
691
|
}
|
|
393
692
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
693
|
+
},
|
|
694
|
+
{
|
|
695
|
+
type: 'tags',
|
|
696
|
+
id: '4',
|
|
697
|
+
name: 'happy',
|
|
698
|
+
links: {
|
|
699
|
+
self: '/tags/4',
|
|
700
|
+
posts: {
|
|
701
|
+
self: '/tags/4/links/posts',
|
|
702
|
+
resource: '/tags/4/posts',
|
|
403
703
|
}
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
704
|
+
}
|
|
705
|
+
},
|
|
706
|
+
{
|
|
707
|
+
type: 'tags',
|
|
708
|
+
id: '5',
|
|
709
|
+
name: 'JR',
|
|
710
|
+
links: {
|
|
711
|
+
self: '/tags/5',
|
|
712
|
+
posts: {
|
|
713
|
+
self: '/tags/5/links/posts',
|
|
714
|
+
resource: '/tags/5/posts',
|
|
412
715
|
}
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
716
|
+
}
|
|
717
|
+
},
|
|
718
|
+
{
|
|
719
|
+
type: 'comments',
|
|
720
|
+
id: '1',
|
|
721
|
+
body: 'what a dumb post',
|
|
722
|
+
links: {
|
|
723
|
+
self: '/comments/1',
|
|
724
|
+
author: {
|
|
725
|
+
self: '/comments/1/links/author',
|
|
726
|
+
resource: '/comments/1/author',
|
|
727
|
+
type: 'people',
|
|
728
|
+
id: '1'
|
|
729
|
+
},
|
|
730
|
+
post: {
|
|
731
|
+
self: '/comments/1/links/post',
|
|
732
|
+
resource: '/comments/1/post',
|
|
733
|
+
type: 'posts',
|
|
734
|
+
id: '1'
|
|
735
|
+
},
|
|
736
|
+
tags: {
|
|
737
|
+
self: '/comments/1/links/tags',
|
|
738
|
+
resource: '/comments/1/tags',
|
|
739
|
+
type: 'tags',
|
|
740
|
+
ids: ['1', '2']
|
|
421
741
|
}
|
|
422
742
|
}
|
|
423
|
-
|
|
424
|
-
|
|
743
|
+
},
|
|
744
|
+
{
|
|
745
|
+
type: 'comments',
|
|
746
|
+
id: '2',
|
|
747
|
+
body: 'i liked it',
|
|
748
|
+
links: {
|
|
749
|
+
self: '/comments/2',
|
|
750
|
+
author: {
|
|
751
|
+
self: '/comments/2/links/author',
|
|
752
|
+
resource: '/comments/2/author',
|
|
753
|
+
type: 'people',
|
|
754
|
+
id: '2'
|
|
755
|
+
},
|
|
756
|
+
post: {
|
|
757
|
+
self: '/comments/2/links/post',
|
|
758
|
+
resource: '/comments/2/post',
|
|
759
|
+
type: 'posts',
|
|
760
|
+
id: '1'
|
|
761
|
+
},
|
|
762
|
+
tags: {
|
|
763
|
+
self: '/comments/2/links/tags',
|
|
764
|
+
resource: '/comments/2/tags',
|
|
765
|
+
type: 'tags',
|
|
766
|
+
ids: ['4', '1']
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
},
|
|
770
|
+
{
|
|
771
|
+
type: 'comments',
|
|
772
|
+
id: '3',
|
|
773
|
+
body: 'Thanks man. Great post. But what is JR?',
|
|
774
|
+
links: {
|
|
775
|
+
self: '/comments/3',
|
|
776
|
+
author: {
|
|
777
|
+
self: '/comments/3/links/author',
|
|
778
|
+
resource: '/comments/3/author',
|
|
779
|
+
type: 'people',
|
|
780
|
+
id: '2'
|
|
781
|
+
},
|
|
782
|
+
post: {
|
|
783
|
+
self: '/comments/3/links/post',
|
|
784
|
+
resource: '/comments/3/post',
|
|
785
|
+
type: 'posts',
|
|
786
|
+
id: '2'
|
|
787
|
+
},
|
|
788
|
+
tags: {
|
|
789
|
+
self: '/comments/3/links/tags',
|
|
790
|
+
resource: '/comments/3/tags',
|
|
791
|
+
type: 'tags',
|
|
792
|
+
ids: ['5']
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
]
|
|
425
797
|
},
|
|
426
|
-
JSONAPI::ResourceSerializer.new(PostResource
|
|
427
|
-
|
|
798
|
+
JSONAPI::ResourceSerializer.new(PostResource,
|
|
799
|
+
include: ['comments', 'comments.tags']).serialize_to_hash(posts)
|
|
800
|
+
)
|
|
428
801
|
end
|
|
429
802
|
|
|
430
803
|
def test_serializer_array_of_resources_limited_fields
|
|
@@ -436,120 +809,201 @@ class SerializerTest < MiniTest::Unit::TestCase
|
|
|
436
809
|
|
|
437
810
|
assert_hash_equals(
|
|
438
811
|
{
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
}],
|
|
453
|
-
linked: {
|
|
454
|
-
tags: [
|
|
455
|
-
{
|
|
456
|
-
name: 'short'
|
|
457
|
-
},
|
|
458
|
-
{
|
|
459
|
-
name: 'whiny'
|
|
460
|
-
},
|
|
461
|
-
{
|
|
462
|
-
name: 'happy'
|
|
463
|
-
},
|
|
464
|
-
{
|
|
465
|
-
name: 'JR'
|
|
812
|
+
data: [
|
|
813
|
+
{
|
|
814
|
+
type: 'posts',
|
|
815
|
+
id: '1',
|
|
816
|
+
title: 'New post',
|
|
817
|
+
links: {
|
|
818
|
+
self: '/posts/1',
|
|
819
|
+
author: {
|
|
820
|
+
self: '/posts/1/links/author',
|
|
821
|
+
resource: '/posts/1/author',
|
|
822
|
+
type: 'people',
|
|
823
|
+
id: '1'
|
|
824
|
+
}
|
|
466
825
|
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
826
|
+
},
|
|
827
|
+
{
|
|
828
|
+
type: 'posts',
|
|
829
|
+
id: '2',
|
|
830
|
+
title: 'JR Solves your serialization woes!',
|
|
831
|
+
links: {
|
|
832
|
+
self: '/posts/2',
|
|
833
|
+
author: {
|
|
834
|
+
self: '/posts/2/links/author',
|
|
835
|
+
resource: '/posts/2/author',
|
|
836
|
+
type: 'people',
|
|
837
|
+
id: '1'
|
|
474
838
|
}
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
],
|
|
842
|
+
linked: [
|
|
843
|
+
{
|
|
844
|
+
type: 'posts',
|
|
845
|
+
id: '11',
|
|
846
|
+
title: 'JR How To',
|
|
847
|
+
links: {
|
|
848
|
+
self: '/posts/11',
|
|
849
|
+
author: {
|
|
850
|
+
self: '/posts/11/links/author',
|
|
851
|
+
resource: '/posts/11/author',
|
|
852
|
+
type: 'people',
|
|
853
|
+
id: '1'
|
|
481
854
|
}
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
855
|
+
}
|
|
856
|
+
},
|
|
857
|
+
{
|
|
858
|
+
type: 'people',
|
|
859
|
+
id: '1',
|
|
860
|
+
email: 'joe@xyz.fake',
|
|
861
|
+
links: {
|
|
862
|
+
self: '/people/1',
|
|
863
|
+
comments: {
|
|
864
|
+
self: '/people/1/links/comments',
|
|
865
|
+
resource: '/people/1/comments'
|
|
488
866
|
}
|
|
489
867
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
868
|
+
},
|
|
869
|
+
{
|
|
870
|
+
id: '1',
|
|
871
|
+
type: 'tags',
|
|
872
|
+
name: 'short',
|
|
873
|
+
links: {
|
|
874
|
+
self: '/tags/1'
|
|
875
|
+
}
|
|
876
|
+
},
|
|
877
|
+
{
|
|
878
|
+
id: '2',
|
|
879
|
+
type: 'tags',
|
|
880
|
+
name: 'whiny',
|
|
881
|
+
links: {
|
|
882
|
+
self: '/tags/2'
|
|
883
|
+
}
|
|
884
|
+
},
|
|
885
|
+
{
|
|
886
|
+
id: '4',
|
|
887
|
+
type: 'tags',
|
|
888
|
+
name: 'happy',
|
|
889
|
+
links: {
|
|
890
|
+
self: '/tags/4'
|
|
891
|
+
}
|
|
892
|
+
},
|
|
893
|
+
{
|
|
894
|
+
id: '5',
|
|
895
|
+
type: 'tags',
|
|
896
|
+
name: 'JR',
|
|
897
|
+
links: {
|
|
898
|
+
self: '/tags/5'
|
|
899
|
+
}
|
|
900
|
+
},
|
|
901
|
+
{
|
|
902
|
+
type: 'comments',
|
|
903
|
+
id: '1',
|
|
904
|
+
body: 'what a dumb post',
|
|
905
|
+
links: {
|
|
906
|
+
self: '/comments/1',
|
|
907
|
+
post: {
|
|
908
|
+
self: '/comments/1/links/post',
|
|
909
|
+
resource: '/comments/1/post',
|
|
910
|
+
type: 'posts',
|
|
911
|
+
id: '1'
|
|
497
912
|
}
|
|
498
913
|
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
914
|
+
},
|
|
915
|
+
{
|
|
916
|
+
type: 'comments',
|
|
917
|
+
id: '2',
|
|
918
|
+
body: 'i liked it',
|
|
919
|
+
links: {
|
|
920
|
+
self: '/comments/2',
|
|
921
|
+
post: {
|
|
922
|
+
self: '/comments/2/links/post',
|
|
923
|
+
resource: '/comments/2/post',
|
|
924
|
+
type: 'posts',
|
|
925
|
+
id: '1'
|
|
506
926
|
}
|
|
507
|
-
}
|
|
508
|
-
|
|
927
|
+
}
|
|
928
|
+
},
|
|
929
|
+
{
|
|
930
|
+
type: 'comments',
|
|
931
|
+
id: '3',
|
|
932
|
+
body: 'Thanks man. Great post. But what is JR?',
|
|
933
|
+
links: {
|
|
934
|
+
self: '/comments/3',
|
|
935
|
+
post: {
|
|
936
|
+
self: '/comments/3/links/post',
|
|
937
|
+
resource: '/comments/3/post',
|
|
938
|
+
type: 'posts',
|
|
939
|
+
id: '2'
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
]
|
|
509
944
|
},
|
|
510
|
-
JSONAPI::ResourceSerializer.new(PostResource
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
945
|
+
JSONAPI::ResourceSerializer.new(PostResource,
|
|
946
|
+
include: ['comments', 'author', 'comments.tags', 'author.posts'],
|
|
947
|
+
fields: {
|
|
948
|
+
people: [:id, :email, :comments],
|
|
949
|
+
posts: [:id, :title, :author],
|
|
950
|
+
tags: [:name],
|
|
951
|
+
comments: [:id, :body, :post]
|
|
952
|
+
}).serialize_to_hash(posts)
|
|
953
|
+
)
|
|
519
954
|
end
|
|
520
955
|
|
|
521
956
|
def test_serializer_camelized_with_value_formatters
|
|
522
957
|
assert_hash_equals(
|
|
523
958
|
{
|
|
524
|
-
|
|
959
|
+
data: {
|
|
960
|
+
type: 'expense_entries',
|
|
525
961
|
id: '1',
|
|
526
962
|
transactionDate: '04/15/2014',
|
|
527
963
|
cost: 12.05,
|
|
528
964
|
links: {
|
|
529
|
-
|
|
530
|
-
|
|
965
|
+
self: '/expense_entries/1',
|
|
966
|
+
isoCurrency: {
|
|
967
|
+
self: '/expense_entries/1/links/iso_currency',
|
|
968
|
+
resource: '/expense_entries/1/iso_currency',
|
|
969
|
+
type: 'iso_currencies',
|
|
970
|
+
id: 'USD'
|
|
971
|
+
},
|
|
972
|
+
employee: {
|
|
973
|
+
self: '/expense_entries/1/links/employee',
|
|
974
|
+
resource: '/expense_entries/1/employee',
|
|
975
|
+
type: 'people',
|
|
976
|
+
id: '3'
|
|
977
|
+
}
|
|
531
978
|
}
|
|
532
979
|
},
|
|
533
|
-
linked:
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
980
|
+
linked: [
|
|
981
|
+
{
|
|
982
|
+
type: 'iso_currencies',
|
|
983
|
+
id: 'USD',
|
|
984
|
+
countryName: 'United States',
|
|
985
|
+
name: 'United States Dollar',
|
|
986
|
+
minorUnit: 'cent',
|
|
987
|
+
links: {
|
|
988
|
+
self: '/iso_currencies/USD'
|
|
989
|
+
}
|
|
990
|
+
},
|
|
991
|
+
{
|
|
992
|
+
type: 'people',
|
|
993
|
+
id: '3',
|
|
994
|
+
email: 'lazy@xyz.fake',
|
|
995
|
+
name: 'Lazy Author',
|
|
996
|
+
dateJoined: '2013-10-31 17:25:00 -0400',
|
|
997
|
+
links: {
|
|
998
|
+
self: '/people/3',
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
]
|
|
547
1002
|
},
|
|
548
|
-
JSONAPI::ResourceSerializer.new(ExpenseEntryResource
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
)
|
|
1003
|
+
JSONAPI::ResourceSerializer.new(ExpenseEntryResource,
|
|
1004
|
+
include: ['iso_currency', 'employee'],
|
|
1005
|
+
fields: {people: [:id, :name, :email, :date_joined]}).serialize_to_hash(
|
|
1006
|
+
ExpenseEntryResource.new(@expense_entry))
|
|
553
1007
|
)
|
|
554
1008
|
end
|
|
555
1009
|
|
|
@@ -559,21 +1013,30 @@ class SerializerTest < MiniTest::Unit::TestCase
|
|
|
559
1013
|
|
|
560
1014
|
assert_hash_equals(
|
|
561
1015
|
{
|
|
562
|
-
|
|
1016
|
+
data: {
|
|
1017
|
+
type: 'planets',
|
|
563
1018
|
id: '8',
|
|
564
1019
|
name: 'Beta W',
|
|
565
1020
|
description: 'Newly discovered Planet W',
|
|
566
1021
|
links: {
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
1022
|
+
self: '/planets/8',
|
|
1023
|
+
planetType: {
|
|
1024
|
+
self: '/planets/8/links/planet_type',
|
|
1025
|
+
resource: '/planets/8/planet_type',
|
|
1026
|
+
type: 'planet_types',
|
|
1027
|
+
id: nil
|
|
1028
|
+
},
|
|
1029
|
+
tags: {
|
|
1030
|
+
self: '/planets/8/links/tags',
|
|
1031
|
+
resource: '/planets/8/tags'
|
|
1032
|
+
},
|
|
1033
|
+
moons: {
|
|
1034
|
+
self: '/planets/8/links/moons',
|
|
1035
|
+
resource: '/planets/8/moons'
|
|
1036
|
+
}
|
|
570
1037
|
}
|
|
571
1038
|
}
|
|
572
1039
|
}, planet_hash)
|
|
573
|
-
|
|
574
|
-
json = planet_hash.to_json
|
|
575
|
-
assert_match /\"planetType\":null/, json
|
|
576
|
-
assert_match /\"moons\":\[\]/, json
|
|
577
1040
|
end
|
|
578
1041
|
|
|
579
1042
|
def test_serializer_include_with_empty_links_null_and_array
|
|
@@ -582,44 +1045,70 @@ class SerializerTest < MiniTest::Unit::TestCase
|
|
|
582
1045
|
planets.push PlanetResource.new(planet)
|
|
583
1046
|
end
|
|
584
1047
|
|
|
585
|
-
planet_hash = JSONAPI::ResourceSerializer.new(PlanetResource
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
fields: { planet_types: [:id, :name] }
|
|
589
|
-
)
|
|
1048
|
+
planet_hash = JSONAPI::ResourceSerializer.new(PlanetResource,
|
|
1049
|
+
include: ['planet_type'],
|
|
1050
|
+
fields: { planet_types: [:id, :name] }).serialize_to_hash(planets)
|
|
590
1051
|
|
|
591
1052
|
assert_hash_equals(
|
|
592
1053
|
{
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
1054
|
+
data: [{
|
|
1055
|
+
type: 'planets',
|
|
1056
|
+
id: '7',
|
|
1057
|
+
name: 'Beta X',
|
|
1058
|
+
description: 'Newly discovered Planet Z',
|
|
1059
|
+
links: {
|
|
1060
|
+
self: '/planets/7',
|
|
1061
|
+
planetType: {
|
|
1062
|
+
self: '/planets/7/links/planet_type',
|
|
1063
|
+
resource: '/planets/7/planet_type',
|
|
1064
|
+
type: 'planet_types',
|
|
1065
|
+
id: '5'
|
|
1066
|
+
},
|
|
1067
|
+
tags: {
|
|
1068
|
+
self: '/planets/7/links/tags',
|
|
1069
|
+
resource: '/planets/7/tags'
|
|
1070
|
+
},
|
|
1071
|
+
moons: {
|
|
1072
|
+
self: '/planets/7/links/moons',
|
|
1073
|
+
resource: '/planets/7/moons'
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
},
|
|
1077
|
+
{
|
|
1078
|
+
type: 'planets',
|
|
1079
|
+
id: '8',
|
|
1080
|
+
name: 'Beta W',
|
|
1081
|
+
description: 'Newly discovered Planet W',
|
|
1082
|
+
links: {
|
|
1083
|
+
self: '/planets/8',
|
|
1084
|
+
planetType: {
|
|
1085
|
+
self: '/planets/8/links/planet_type',
|
|
1086
|
+
resource: '/planets/8/planet_type',
|
|
1087
|
+
type: 'planet_types',
|
|
1088
|
+
id: nil
|
|
1089
|
+
},
|
|
1090
|
+
tags: {
|
|
1091
|
+
self: '/planets/8/links/tags',
|
|
1092
|
+
resource: '/planets/8/tags'
|
|
1093
|
+
},
|
|
1094
|
+
moons: {
|
|
1095
|
+
self: '/planets/8/links/moons',
|
|
1096
|
+
resource: '/planets/8/moons'
|
|
1097
|
+
}
|
|
1098
|
+
}
|
|
617
1099
|
}
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
1100
|
+
],
|
|
1101
|
+
linked: [
|
|
1102
|
+
{
|
|
1103
|
+
type: 'planet_types',
|
|
1104
|
+
id: '5',
|
|
1105
|
+
name: 'unknown',
|
|
1106
|
+
links: {
|
|
1107
|
+
self: '/planet_types/5'
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
]
|
|
1111
|
+
}, planet_hash)
|
|
623
1112
|
end
|
|
624
1113
|
|
|
625
1114
|
def test_serializer_booleans
|
|
@@ -629,16 +1118,27 @@ class SerializerTest < MiniTest::Unit::TestCase
|
|
|
629
1118
|
|
|
630
1119
|
assert_hash_equals(
|
|
631
1120
|
{
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
1121
|
+
data: {
|
|
1122
|
+
type: 'preferences',
|
|
1123
|
+
id: '1',
|
|
1124
|
+
advanced_mode: false,
|
|
1125
|
+
links: {
|
|
1126
|
+
self: '/preferences/1',
|
|
1127
|
+
author: {
|
|
1128
|
+
self: '/preferences/1/links/author',
|
|
1129
|
+
resource: '/preferences/1/author',
|
|
1130
|
+
type: 'authors',
|
|
1131
|
+
id: nil
|
|
1132
|
+
},
|
|
1133
|
+
friends: {
|
|
1134
|
+
self: '/preferences/1/links/friends',
|
|
1135
|
+
resource: '/preferences/1/friends'
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
640
1139
|
},
|
|
641
|
-
JSONAPI::ResourceSerializer.new(PreferencesResource).serialize_to_hash(preferences)
|
|
1140
|
+
JSONAPI::ResourceSerializer.new(PreferencesResource).serialize_to_hash(preferences)
|
|
1141
|
+
)
|
|
642
1142
|
end
|
|
643
1143
|
|
|
644
1144
|
def test_serializer_data_types
|
|
@@ -648,7 +1148,8 @@ class SerializerTest < MiniTest::Unit::TestCase
|
|
|
648
1148
|
|
|
649
1149
|
assert_hash_equals(
|
|
650
1150
|
{
|
|
651
|
-
|
|
1151
|
+
data: {
|
|
1152
|
+
type: 'facts',
|
|
652
1153
|
id: '1',
|
|
653
1154
|
spouse_name: 'Jane Author',
|
|
654
1155
|
bio: 'First man to run across Antartica.',
|
|
@@ -658,9 +1159,13 @@ class SerializerTest < MiniTest::Unit::TestCase
|
|
|
658
1159
|
birthday: Date.parse('1965-06-30'),
|
|
659
1160
|
bedtime: Time.parse('2000-01-01 20:00:00 UTC +00:00'), #DB seems to set the date to 2001-01-01 for time types
|
|
660
1161
|
photo: "abc",
|
|
661
|
-
cool: false
|
|
1162
|
+
cool: false,
|
|
1163
|
+
links: {
|
|
1164
|
+
self: '/facts/1'
|
|
1165
|
+
}
|
|
662
1166
|
}
|
|
663
1167
|
},
|
|
664
|
-
JSONAPI::ResourceSerializer.new(FactResource).serialize_to_hash(facts)
|
|
1168
|
+
JSONAPI::ResourceSerializer.new(FactResource).serialize_to_hash(facts)
|
|
1169
|
+
)
|
|
665
1170
|
end
|
|
666
1171
|
end
|