fast_jsonapi
fast_jsonapi copied to clipboard
Bug: Associations that have an attribute or method named `map` cannot be serialized
The following method in relationship.rb uses a check for respond_to? :map to determine whether an association is plural or not:
def ids_hash_from_record_and_relationship(record, params = {})
return ids_hash(
fetch_id(record, params)
) unless polymorphic
return unless associated_object = fetch_associated_object(record, params)
return associated_object.map do |object|
id_hash_from_record object, polymorphic
end if associated_object.respond_to? :map
id_hash_from_record associated_object, polymorphic
end
However I have a model that has a field/attribute called map. When this model is an association it doesn't get serialized properly.
The association gets put in included fine, but in the relationships key I get the value of the map attribute:

And this cannot be properly parsed, and the relationship is lost. This was a particularly difficult issue to debug, mostly because all the values of my map property while I was trying to debug this were nil. So it wasn't clear it was a bug at first. I spent a couple of hours trying to figure out what was wrong with MY code. It was only after reading your source code that I was able to work out the problem. I'm surprised no one else has had this issue.
Do you have a recommended workaround for this?
Also I suspect that it is not your intention to require models to not have attributes or methods named map. Ideally this could be changed rather quickly in your source code. Otherwise, I'll have to rename the attribute in a rather large application, made harder by the fact that the native iterator map is also used extensively, so a big mess really.
FYI https://github.com/Netflix/fast_jsonapi/issues/462