Skip to content

Commit a0de45a

Browse files
committed
Fix JSON:API: for_type_and_id should always inflect_type
Should Serializer._type ever be inflected? Right now, it won't be, but association.serializer._type will be inflected. That's the current behavior.
1 parent cf29db3 commit a0de45a

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

lib/active_model_serializers/adapter/json_api/relationship.rb

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,16 @@ def data_for(association)
4343
end
4444

4545
def data_for_one(association)
46-
if association.belongs_to? &&
47-
parent_serializer.object.respond_to?(association.reflection.foreign_key)
46+
if belongs_to_id_on_self?(association)
4847
id = parent_serializer.read_attribute_for_serialization(association.reflection.foreign_key)
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
48+
type =
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+
association.lazy_association.serializer.json_key
53+
else
54+
association.reflection.type.to_s
55+
end
5756
ResourceIdentifier.for_type_with_id(type, id, serializable_resource_options)
5857
else
5958
# TODO(BF): Process relationship without evaluating lazy_association
@@ -93,6 +92,11 @@ def meta_for(association)
9392
meta = association.meta
9493
meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
9594
end
95+
96+
def belongs_to_id_on_self?(association)
97+
association.belongs_to? &&
98+
parent_serializer.object.respond_to?(association.reflection.foreign_key)
99+
end
96100
end
97101
end
98102
end

lib/active_model_serializers/adapter/json_api/resource_identifier.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def self.type_for(serializer, serializer_type = nil, transform_options = {})
99

1010
def self.for_type_with_id(type, id, options)
1111
return nil if id.blank?
12+
type = inflect_type(type)
1213
{
1314
id: id.to_s,
1415
type: type_for(:no_class_needed, type, options)
@@ -17,13 +18,17 @@ def self.for_type_with_id(type, id, options)
1718

1819
def self.raw_type_from_serializer_object(object)
1920
class_name = object.class.name # should use model_name
20-
serializer_type = class_name.underscore
21+
raw_type = class_name.underscore
22+
raw_type = inflect_type(raw_type)
23+
raw_type
24+
.gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
25+
raw_type
26+
end
27+
28+
def self.inflect_type(type)
2129
singularize = ActiveModelSerializers.config.jsonapi_resource_type == :singular
2230
inflection = singularize ? :singularize : :pluralize
23-
serializer_type = ActiveSupport::Inflector.public_send(inflection, serializer_type)
24-
serializer_type
25-
.gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
26-
serializer_type
31+
ActiveSupport::Inflector.public_send(inflection, type)
2732
end
2833

2934
# {http://jsonapi.org/format/#document-resource-identifier-objects Resource Identifier Objects}
@@ -44,7 +49,8 @@ def as_json
4449
private
4550

4651
def type_for(serializer, transform_options)
47-
self.class.type_for(serializer, serializer._type, transform_options)
52+
serializer_type = serializer._type
53+
self.class.type_for(serializer, serializer_type, transform_options)
4854
end
4955

5056
def id_for(serializer)

0 commit comments

Comments
 (0)