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

@dev-jwshin

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

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