Sitelet https://github.com/jsonapi-serializer/jsonapi-serializer/issues/23
Skip to content

lazy_load_data overwrites include and does not render relationship data #23

Description

@mocerinoj

When lazy loading polymorphic relationships.. when including the relationship, the relationship object comes through but the relationship data does not.

# provider_lead_serializer.rb
class ProviderLeadSerializer
  include FastJsonapi::ObjectSerializer

  belongs_to :owner, polymorphic: true, lazy_load_data: true

  ...
end

# agent_serializer.rb
class AgentSerializer
  include FastJsonapi::ObjectSerializer
  ...
end

Without including the relationship, the data is not loaded (as expected):

ProviderLeadSerializer.new(provider_lead)

yields:

{
  "data": [
    {
      "id": 123,
      "type": "provider_lead",
      "attributes": {},
      "relationships": {
        "owner": { }
      }
    }
  ],
  "included": []
}

Then, I want to explicitly include the owner relationship:

ProviderLeadSerializer.new(provider_lead, { include: [:owner] })

yields:

{
  "data": [
    {
      "id": 123,
      "type": "provider_lead",
      "attributes": {},
      "relationships": {
        "owner": { }
      }
    }
  ],
  "included": [
    {
      "id": 456,
      "type": "agent",
      "attributes": {}
    }
  ]
}

^^^ This is missing the data on provider_lead.relationships. If i remove lazy_load_data: true, everything works as expect.

Expected JSON would be:

{
  "data": [
    {
      "id": 123,
      "type": "provider_lead",
      "attributes": {},
      "relationships": {
        "owner": {
          "data": {
            "id": 456,
            "type": "agent"
          }
        }
      }
    }
  ],
  "included": [
    {
      "id": 456,
      "type": "agent",
      "attributes": {}
    }
  ]
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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