Netflix / fast_jsonapi

No Longer Maintained - A lightning fast JSON:API serializer for Ruby Objects.
Apache License 2.0
5.07k stars 425 forks source link

set_id value not respected in relationships #461

Closed clorbus closed 4 years ago

clorbus commented 4 years ago

I use randomly generated uids rather than incremental ids to identify resources in our API. Fast JSON API has a mechanism (set_id) to specify the value you'd like to use as the id.

However, this setting is ignored for relationships. In the example below, the Organization's id is "5" but the uid is "97132034882" which is what should have been returned.

Note that the meeting's id is "8" and it correctly returns the uid of "74992482388" as specified in the serializer.

Maybe it's just ignoring the OrganizationSerializer?

meeting_serializer.rb

class MeetingSerializer
  include FastJsonapi::ObjectSerializer

  set_id :uid
  belongs_to :organization
  attributes :created_at, :updated_at
end

organization_serializer.rb

class OrganizationSerializer
  include FastJsonapi::ObjectSerializer

  set_id :uid # this is ignored in the test run below
  attributes :name, :created_at, :updated_at
end

step to reproduce

> puts MeetingSerializer.new(meeting).to_json
{
  "data": {
    "id": "74992482388",
    "type": "meeting",
    "attributes": {
      "created_at": "2020-04-18T22:45:24.934Z",
      "updated_at": "2020-04-18T22:45:25.125Z"
    },
    "relationships": {
      "organization": {
        "data": {
          "id": "5",
          "type": "organization"
        }
      }
    }
  }
}
clorbus commented 4 years ago

Update It's skipping the OrganizationSerializer entirely. Looking at the code that serializes relationships, it looks like it skips the serializer and uses a method called id_hash_from_record instead.

id_hash_from_record looks for an id_method_name attribute, which you can specify as the method you'd like it to use for id lookup.

Closing this issue as it's not a bug.