Skip to content

set_id does not seem to be called for relationships #53

@johnnybenson

Description

@johnnybenson

Expected Behavior

I expected relationships serialized with set_id defined, to use the return value of set_id in the serialized data.

Serializers

class Serializers::Base
  include FastJsonapi::ObjectSerializer
  set_id { |object| object.encoded_id }   # <- encoded id!
end

class Serializers::Employee < Serializers::Base
  set_type :employee
end

class Serializers::Pro < Serializers::Base
  set_type :pro
end

class Serializers::Consult < Serializers::Base
  set_type :consult

  belongs_to :employee, serializer: Serializers::Employee
  belongs_to :pro, serializer: Serializers::Pro
end

Expected JSON

{
    "data": {
        "id": "x6vx5z",
        "type": "consult",
        "relationships": {
            "employee": {
                "data": {
                    "id": "g5v29z",
                    "type": "employee"
                }
            },
            "pro": {
                "data": {
                    "id": "rxvemv",
                    "type": "pro"
                }
            }
        }
    }
}

Actual JSON

Note: the top level "consult" id is encoded correctly where the relationships are displaying the integer id

{
    "data": {
        "id": "x6vx5z",
        "type": "consult",
        "relationships": {
            "employee": {
                "data": {
                    "id": "2",
                    "type": "employee"
                }
            },
            "pro": {
                "data": {
                    "id": "3",
                    "type": "pro"
                }
            }
        }
    }
}

Specifications

GIT
  remote: https://github.com/fast-jsonapi/fast_jsonapi
  revision: c305b699a970ba60e77ba5d7d1f962390bcdc363
  specs:
    fast_jsonapi (1.6.0)
      activesupport (>= 4.2)
  • Ruby version: ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-darwin18]

Maybe possibly related to the open pr: #45

Felt like this was a simple enough example to illustrate just in case it's not related!

Thanks for any insights (or a workaround?)

😎

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions