Skip to content

Include doesnt include the resource when multiple references #2432

Open
@TeamNautilus

Description

@TeamNautilus

Expected behavior vs actual behavior

If there is a multiple referenced resource A which has_one another resource B, the resource B is included only by declaring the inclusion through the "first path", for example "first_reference.resource_a.resource_b".

Steps to reproduce

Given the models

class DummyA < ActiveModelSerializers::Model
  attr_accessor :group, :element

  def id
    'DummyA'
  end
end

class DummyB < ActiveModelSerializers::Model
  attr_accessor :parent

  def id
    'DummyB'
  end
end

class DummyC < ActiveModelSerializers::Model
  attr_accessor :category

  def id
    'DummyC'
  end

  def name
    puts 'DummyC#name was called'
    'name'
  end
end

class DummyD < ActiveModelSerializers::Model
  def id
    'DummyD'
  end

  def name
    puts 'DummyD#name was called'
    'name'
  end
end

serializers

class DummyASerializer < ActiveModel::Serializer
  type 'DummyA'

  has_many :group
  has_one :element
end

class DummyBSerializer < ActiveModel::Serializer
  type 'DummyB'

  has_one :parent
end

class DummyCSerializer < ActiveModel::Serializer
  type 'DummyC'

  has_one :category

  attributes :name
end

class DummyDSerializer < ActiveModel::Serializer
  type 'DummyD'

  attributes :name
end

and in the controller:

    base = DummyA.new
    dummy_b = DummyB.new
    dummy_c = DummyC.new
    dummy_d = DummyD.new
    dummy_c.category = dummy_d
    dummy_b.parent = dummy_c
    base.group = [dummy_c]
    base.element = dummy_b

    render json: [base],
           adapter: :json_api,
           include: 'group,element.parent.category'

I would expect the following (Note the inclusion of dummyD, the element.parent.category element)

{
  "data": [
    {
      "id": "DummyA",
      "type": "dummy-a",
      "relationships": {
        "group": {
          "data": [
            {
              "id": "DummyC",
              "type": "dummy-c"
            }
          ]
        },
        "element": {
          "data": {
            "id": "DummyB",
            "type": "dummy-b"
          }
        }
      }
    }
  ],
  "included": [
    {
      "id": "DummyB",
      "type": "dummy-b",
      "relationships": {
        "parent": {
          "data": {
            "id": "DummyC",
            "type": "dummy-c"
          }
        }
      }
    },
    {
      "id": "DummyC",
      "type": "dummy-c",
      "attributes": {
        "name": "name"
      },
      "relationships": {
        "category": {
          "data": {
            "id": "DummyD",
            "type": "dummy-d"
          }
        }
      }
    },
    {
      "id": "DummyD",
      "type": "dummy-d",
      "attributes": {
        "name": "name"
      }
    }
  ]
}

but instead I get

{
  "data": [
    {
      "id": "DummyA",
      "type": "dummy-a",
      "relationships": {
        "group": {
          "data": [
            {
              "id": "DummyC",
              "type": "dummy-c"
            }
          ]
        },
        "element": {
          "data": {
            "id": "DummyB",
            "type": "dummy-b"
          }
        }
      }
    }
  ],
  "included": [
    {
      "id": "DummyC",
      "type": "dummy-c",
      "attributes": {
        "name": "name"
      },
      "relationships": {
        "category": {
          "data": {
            "id": "DummyD",
            "type": "dummy-d"
          }
        }
      }
    },
    {
      "id": "DummyB",
      "type": "dummy-b",
      "relationships": {
        "parent": {
          "data": {
            "id": "DummyC",
            "type": "dummy-c"
          }
        }
      }
    }
  ]
}

which has DubbyC and DummyB only. My suspect is that since dummyC was referenced by dummyA (without the dummyD inclusion), the inclusion through DummyB use the previous cached version of dummyC, without the dummyD inclusion.

Does it make sense?

Environment

ActiveModelSerializers Version (commit ref if not on tag): v0.10.13

Output of ruby -e "puts RUBY_DESCRIPTION": ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [x86_64-linux]

OS Type & Version: Debian 10

Integrated application and version (e.g., Rails, Grape, etc): Rails 6.1.5

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