@@ -121,6 +121,10 @@ def include_data(value = true)
121
121
:nil
122
122
end
123
123
124
+ def to_many?
125
+ false
126
+ end
127
+
124
128
# Build association. This method is used internally to
125
129
# build serializer's association by its reflection.
126
130
#
@@ -150,17 +154,9 @@ def build_association(parent_serializer, parent_serializer_options, include_slic
150
154
reflection_options ||= settings . merge ( include_data : include_data? ( include_slice ) ) # Needs to be after association_value is evaluated unless reflection.block.nil?
151
155
152
156
if serializer_class
153
- if ( serializer = build_association_serializer ( parent_serializer , parent_serializer_options , association_value , serializer_class ) )
154
- reflection_options [ :serializer ] = serializer
155
- else
156
- # BUG: per #2027, JSON API resource relationships are only id and type, and hence either
157
- # *require* a serializer or we need to be a little clever about figuring out the id/type.
158
- # In either case, returning the raw virtual value will almost always be incorrect.
159
- #
160
- # Should be reflection_options[:virtual_value] or adapter needs to figure out what to do
161
- # with an object that is non-nil and has no defined serializer.
162
- reflection_options [ :virtual_value ] = association_value . try ( :as_json ) || association_value
163
- end
157
+ reflection_options . merge! (
158
+ serialize_association_value! ( association_value , serializer_class , parent_serializer , parent_serializer_options )
159
+ )
164
160
elsif !association_value . nil? && !association_value . instance_of? ( Object )
165
161
reflection_options [ :virtual_value ] = association_value
166
162
end
@@ -230,6 +226,20 @@ def value(serializer, include_slice)
230
226
end
231
227
end
232
228
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 }
232
+ 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 }
240
+ end
241
+ end
242
+
233
243
def build_association_options ( parent_serializer , parent_serializer_namespace_option , include_slice )
234
244
serializer_for_options = {
235
245
# Pass the parent's namespace onto the child serializer
0 commit comments