Skip to content

Commit cf29db3

Browse files
committed
Fix JSON:API polymorphic type regression from v0.10.5
1 parent 0fcb8a6 commit cf29db3

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

lib/active_model_serializers/adapter/json_api/relationship.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ def data_for_one(association)
4646
if association.belongs_to? &&
4747
parent_serializer.object.respond_to?(association.reflection.foreign_key)
4848
id = parent_serializer.read_attribute_for_serialization(association.reflection.foreign_key)
49-
type = association.reflection.type.to_s
49+
if association.polymorphic?
50+
# We can't infer resource type for polymorphic relationships from the serializer.
51+
# We can ONLY know a polymorphic resource type by inspecting each resource.
52+
serializer = association.lazy_association.serializer
53+
type = serializer.json_key
54+
else
55+
type = association.reflection.type.to_s
56+
end
5057
ResourceIdentifier.for_type_with_id(type, id, serializable_resource_options)
5158
else
5259
# TODO(BF): Process relationship without evaluating lazy_association

0 commit comments

Comments
 (0)