@@ -227,16 +227,20 @@ def value(serializer, include_slice)
227
227
end
228
228
229
229
def serialize_association_value! ( association_value , serializer_class , parent_serializer , parent_serializer_options )
230
- if ( serializer = build_association_serializer ( parent_serializer , parent_serializer_options , association_value , serializer_class ) )
231
- { serializer : serializer }
230
+ if to_many?
231
+ if ( serializer = build_association_collection_serializer ( parent_serializer , parent_serializer_options , association_value , serializer_class ) )
232
+ { serializer : serializer }
233
+ else
234
+ # BUG: per #2027, JSON API resource relationships are only id and type, and hence either
235
+ # *require* a serializer or we need to be a little clever about figuring out the id/type.
236
+ # In either case, returning the raw virtual value will almost always be incorrect.
237
+ #
238
+ # Should be reflection_options[:virtual_value] or adapter needs to figure out what to do
239
+ # with an object that is non-nil and has no defined serializer.
240
+ { virtual_value : association_value . try ( :as_json ) || association_value }
241
+ end
232
242
else
233
- # BUG: per #2027, JSON API resource relationships are only id and type, and hence either
234
- # *require* a serializer or we need to be a little clever about figuring out the id/type.
235
- # In either case, returning the raw virtual value will almost always be incorrect.
236
- #
237
- # Should be reflection_options[:virtual_value] or adapter needs to figure out what to do
238
- # with an object that is non-nil and has no defined serializer.
239
- { virtual_value : association_value . try ( :as_json ) || association_value }
243
+ { serializer : build_association_serializer ( parent_serializer , parent_serializer_options , association_value , serializer_class ) }
240
244
end
241
245
end
242
246
@@ -254,19 +258,24 @@ def build_association_options(parent_serializer, parent_serializer_namespace_opt
254
258
end
255
259
256
260
# NOTE(BF): This serializer throw/catch should only happen when the serializer is a collection
257
- # serializer. This is a good reason for the reflection to have a to_many? or collection? type method.
261
+ # serializer.
258
262
#
259
263
# @return [ActiveModel::Serializer, nil]
260
- def build_association_serializer ( parent_serializer , parent_serializer_options , association_value , serializer_class )
264
+ def build_association_collection_serializer ( parent_serializer , parent_serializer_options , association_value , serializer_class )
261
265
catch ( :no_serializer ) do
262
- # Make all the parent serializer instance options available to associations
263
- # except ActiveModelSerializers-specific ones we don't want.
264
- serializer_options = parent_serializer_options . except ( :serializer )
265
- serializer_options [ :serializer_context_class ] = parent_serializer . class
266
- serializer_options [ :serializer ] = serializer if serializer
267
- serializer_class . new ( association_value , serializer_options )
266
+ build_association_serializer ( parent_serializer , parent_serializer_options , association_value , serializer_class )
268
267
end
269
268
end
269
+
270
+ # @return [ActiveModel::Serializer, nil]
271
+ def build_association_serializer ( parent_serializer , parent_serializer_options , association_value , serializer_class )
272
+ # Make all the parent serializer instance options available to associations
273
+ # except ActiveModelSerializers-specific ones we don't want.
274
+ serializer_options = parent_serializer_options . except ( :serializer )
275
+ serializer_options [ :serializer_context_class ] = parent_serializer . class
276
+ serializer_options [ :serializer ] = serializer if serializer
277
+ serializer_class . new ( association_value , serializer_options )
278
+ end
270
279
end
271
280
end
272
281
end
0 commit comments