@@ -98,6 +98,78 @@ def test_success_document_key_transform_default
98
98
} , result )
99
99
end
100
100
101
+ def test_success_document_key_transform_global_config
102
+ mock_request
103
+ result = with_config ( key_transform : :camel_lower ) do
104
+ serializer = PostSerializer . new ( @post )
105
+ adapter = ActiveModelSerializers ::Adapter ::JsonApi . new ( serializer )
106
+ adapter . serializable_hash ( @options )
107
+ end
108
+ assert_equal ( {
109
+ data : {
110
+ id : '1337' ,
111
+ type : 'posts' ,
112
+ attributes : {
113
+ title : 'Title 1' ,
114
+ body : 'Body 1' ,
115
+ publishAt : @publish_at
116
+ } ,
117
+ relationships : {
118
+ author : {
119
+ data : { id : '1' , type : 'authors' }
120
+ } ,
121
+ comments : {
122
+ data : [
123
+ { id : '7' , type : 'comments' } ,
124
+ { id : '12' , type : 'comments' }
125
+ ] }
126
+ } ,
127
+ links : {
128
+ self : 'http://example.com/posts/1337' ,
129
+ postAuthors : 'http://example.com/posts/1337/authors' ,
130
+ subscriberComments : 'http://example.com/posts/1337/comments'
131
+ } ,
132
+ meta : { rating : 5 , favoriteCount : 10 }
133
+ }
134
+ } , result )
135
+ end
136
+
137
+ def test_success_doc_key_transform_serialization_ctx_overrides_global
138
+ mock_request ( :camel )
139
+ result = with_config ( key_transform : :camel_lower ) do
140
+ serializer = PostSerializer . new ( @post )
141
+ adapter = ActiveModelSerializers ::Adapter ::JsonApi . new ( serializer )
142
+ adapter . serializable_hash ( @options )
143
+ end
144
+ assert_equal ( {
145
+ Data : {
146
+ Id : '1337' ,
147
+ Type : 'posts' ,
148
+ Attributes : {
149
+ Title : 'Title 1' ,
150
+ Body : 'Body 1' ,
151
+ PublishAt : @publish_at
152
+ } ,
153
+ Relationships : {
154
+ Author : {
155
+ Data : { Id : '1' , Type : 'authors' }
156
+ } ,
157
+ Comments : {
158
+ Data : [
159
+ { Id : '7' , Type : 'comments' } ,
160
+ { Id : '12' , Type : 'comments' }
161
+ ] }
162
+ } ,
163
+ Links : {
164
+ Self : 'http://example.com/posts/1337' ,
165
+ PostAuthors : 'http://example.com/posts/1337/authors' ,
166
+ SubscriberComments : 'http://example.com/posts/1337/comments'
167
+ } ,
168
+ Meta : { Rating : 5 , FavoriteCount : 10 }
169
+ }
170
+ } , result )
171
+ end
172
+
101
173
def test_success_document_key_transform_dashed
102
174
mock_request ( :dashed )
103
175
serializer = PostSerializer . new ( @post )
@@ -245,22 +317,76 @@ def test_success_document_key_transform_camel_lower
245
317
246
318
def test_error_document_key_transform_default
247
319
mock_request
248
-
249
320
resource = ModelWithErrors . new
250
321
resource . errors . add ( :published_at , 'must be in the future' )
251
322
resource . errors . add ( :title , 'must be longer' )
252
-
253
323
serializer = ActiveModel ::Serializer ::ErrorSerializer . new ( resource )
254
324
adapter = ActiveModelSerializers ::Adapter ::JsonApi . new ( serializer )
255
325
result = adapter . serializable_hash ( @options )
256
-
257
326
expected_errors_object =
258
327
{ :errors =>
259
328
[
260
- { :source => { :pointer => '/data/attributes/published_at' } , :detail => 'must be in the future' } ,
261
- { :source => { :pointer => '/data/attributes/title' } , :detail => 'must be longer' }
329
+ {
330
+ :source => { :pointer => '/data/attributes/published_at' } ,
331
+ :detail => 'must be in the future' } ,
332
+ {
333
+ :source => { :pointer => '/data/attributes/title' } ,
334
+ :detail => 'must be longer'
335
+ }
262
336
]
263
- }
337
+ }
338
+ assert_equal expected_errors_object , result
339
+ end
340
+
341
+ def test_error_document_key_transform_global_config
342
+ mock_request
343
+ result = with_config ( key_transform : :camel ) do
344
+ resource = ModelWithErrors . new
345
+ resource . errors . add ( :published_at , 'must be in the future' )
346
+ resource . errors . add ( :title , 'must be longer' )
347
+ serializer = ActiveModel ::Serializer ::ErrorSerializer . new ( resource )
348
+ adapter = ActiveModelSerializers ::Adapter ::JsonApi . new ( serializer )
349
+ adapter . serializable_hash ( @options )
350
+ end
351
+ expected_errors_object =
352
+ { :Errors =>
353
+ [
354
+ {
355
+ :Source => { :Pointer => '/data/attributes/published_at' } ,
356
+ :Detail => 'must be in the future'
357
+ } ,
358
+ {
359
+ :Source => { :Pointer => '/data/attributes/title' } ,
360
+ :Detail => 'must be longer'
361
+ }
362
+ ]
363
+ }
364
+ assert_equal expected_errors_object , result
365
+ end
366
+
367
+ def test_error_document_key_transform_serialization_ctx_overrides_global
368
+ mock_request ( :camel )
369
+ result = with_config ( key_transform : :camel_lower ) do
370
+ resource = ModelWithErrors . new
371
+ resource . errors . add ( :published_at , 'must be in the future' )
372
+ resource . errors . add ( :title , 'must be longer' )
373
+ serializer = ActiveModel ::Serializer ::ErrorSerializer . new ( resource )
374
+ adapter = ActiveModelSerializers ::Adapter ::JsonApi . new ( serializer )
375
+ adapter . serializable_hash ( @options )
376
+ end
377
+ expected_errors_object =
378
+ { :Errors =>
379
+ [
380
+ {
381
+ :Source => { :Pointer => '/data/attributes/published_at' } ,
382
+ :Detail => 'must be in the future'
383
+ } ,
384
+ {
385
+ :Source => { :Pointer => '/data/attributes/title' } ,
386
+ :Detail => 'must be longer'
387
+ }
388
+ ]
389
+ }
264
390
assert_equal expected_errors_object , result
265
391
end
266
392
@@ -278,8 +404,14 @@ def test_error_document_key_transform_dashed
278
404
expected_errors_object =
279
405
{ :errors =>
280
406
[
281
- { :source => { :pointer => '/data/attributes/published_at' } , :detail => 'must be in the future' } ,
282
- { :source => { :pointer => '/data/attributes/title' } , :detail => 'must be longer' }
407
+ {
408
+ :source => { :pointer => '/data/attributes/published_at' } ,
409
+ :detail => 'must be in the future'
410
+ } ,
411
+ {
412
+ :source => { :pointer => '/data/attributes/title' } ,
413
+ :detail => 'must be longer'
414
+ }
283
415
]
284
416
}
285
417
assert_equal expected_errors_object , result
0 commit comments