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

It seems that lazy_load_data: true does not work as expected for multilevel and polymorphic or STI relationships defined more than once. #246

Description

@dev-jwshin

Problem

class ApplicationSerializer
  include JSONAPI::Serializer

  def self.has_many(name, options = {}, &block)
    super(name, options.merge(lazy_load_data: true), &block)
  end

  def self.has_one(name, options = {}, &block)
    super(name, options.merge(lazy_load_data: true), &block)
  end

  def self.belongs_to(name, options = {}, &block)
    super(name, options.merge(lazy_load_data: true), &block)
  end

  def self.has_and_belongs_to_many(name, options = {}, &block)
    super(name, options.merge(lazy_load_data: true), &block)
  end
end
class ASerializer < ApplicationSerializer
  has_many :b
end
class BSerializer < ApplicationSerializer
  belongs_to :c, polymorphic: true
end

I wrote the code above and sent a request with include=a.c to A contoller.

It seems that lazy_load_data: true does not work as expected for multilevel and polymorphic or STI relationships defined more than once.

Expected Behavior

{
  "data"=>{
    ...
  },
  "included"=>
  [
    ...
    {
      ...
    },
    ...
  ],l
  "relationships"=> {
    "c"=>{
      data => {
        ...
      }
    }
  }
},
"meta"=>{"total-count"=>1},
"links"=> {
  "self"=> "http://link.com/path",
  "current"=> "http://link.com/path",
}

Actual Behavior

{
  "data"=>{
    ...
  },
  "included"=>
  [
    ...
    {
      ...
    },
    ...
  ],l
  "relationships"=> {
    "c"=>{
      data => {}
    }
  }
},
"meta"=>{"total-count"=>1},
"links"=> {
  "self"=> "http://link.com/path",
  "current"=> "http://link.com/path",
}

Specifications

  • Version:
    jsonapi ~> 1.7.0'
    jsonapi-serializer ~> 2.2'
  • Ruby version: 3.0.5p211
  • Rails version: 7.0.8

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions