Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include doesnt include the resource when multiple references #2432

Open
TeamNautilus opened this issue May 2, 2022 · 0 comments
Open

Include doesnt include the resource when multiple references #2432

TeamNautilus opened this issue May 2, 2022 · 0 comments

Comments

@TeamNautilus
Copy link

TeamNautilus commented May 2, 2022

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

@TeamNautilus TeamNautilus changed the title Include doesnt include the resource when multiple reference Include doesnt include the resource when multiple references May 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant