@@ -121,6 +121,10 @@ def include_data(value = true)
121121 :nil
122122 end
123123
124+ def to_many?
125+ false
126+ end
127+
124128 # Build association. This method is used internally to
125129 # build serializer's association by its reflection.
126130 #
@@ -150,17 +154,9 @@ def build_association(parent_serializer, parent_serializer_options, include_slic
150154 reflection_options ||= settings . merge ( include_data : include_data? ( include_slice ) ) # Needs to be after association_value is evaluated unless reflection.block.nil?
151155
152156 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+ )
164160 elsif !association_value . nil? && !association_value . instance_of? ( Object )
165161 reflection_options [ :virtual_value ] = association_value
166162 end
@@ -230,6 +226,20 @@ def value(serializer, include_slice)
230226 end
231227 end
232228
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+
233243 def build_association_options ( parent_serializer , parent_serializer_namespace_option , include_slice )
234244 serializer_for_options = {
235245 # Pass the parent's namespace onto the child serializer
0 commit comments