fast_jsonapi icon indicating copy to clipboard operation
fast_jsonapi copied to clipboard

Bug: Associations that have an attribute or method named `map` cannot be serialized

Open benlieb opened this issue 5 years ago • 1 comments

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:

image

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.

benlieb avatar Aug 01 '20 05:08 benlieb

FYI https://github.com/Netflix/fast_jsonapi/issues/462

davidwparker avatar Aug 03 '20 01:08 davidwparker