roar-jsonapi icon indicating copy to clipboard operation
roar-jsonapi copied to clipboard

double included fields

Open twiduch opened this issue 7 years ago • 4 comments

I have model User which has many Roles and Role has many Permissions The UserRepresenter

class UserRepresenter < BaseRepresenter
  type :users

  link :self, toplevel: true do
    "/users"
  end

  attributes do
    property :first_name
    property :last_name
  end

  link(:self) { "/users/#{represented.id}" }
  link(:all) { "/users" }

  has_many :roles, class: Role, decorator: RoleRepresenter do
    relationship do
      link(:related)  {"/users/#{represented.id}/roles"}
    end
  end
end

The RoleRepresenter

class RoleRepresenter < BaseRepresenter
  type :roles

  link :self, toplevel: true do
    "/roles"
  end

  attributes do
    property :name
  end

  link :self do
    "/roles/#{represented.id}"
  end

  has_many :permissions, decorator: PermissionRepresenter
end

When I call it, I receive included for user and included for roles. I think that in Json API there should be only one included field

{
  "data": {
    "relationships": {
      "roles": {
        "data": [
          {
            "id": "1",
            "type": "roles"
          }
        ],
        "links": {
          "related": "/users/2/roles"
        }
      }
    },
    "id": "2",
    "attributes": {
      "first-name": "Lady",
      "last-name": "Gaga",
    },
    "type": "users",
    "links": {
      "self": "/users/2",
      "all": "/users"
    }
  },
  "included": [
    {
      "relationships": {
        "permissions": {
          "data": [
            {
              "id": "4",
              "type": "permissions"
            },
            {
              "id": "15",
              "type": "permissions"
            }
          ]
        }
      },
      "included": [
        {
          "id": "4",
          "attributes": {
            "name": "View project",
          },
          "type": "permissions",
          "links": {
            "self": "/permissions/4"
          }
        },
        {
          "id": "15",
          "attributes": {
            "name": "View project user",
          },
          "type": "permissions",
          "links": {
            "self": "/permissions/15"
          }
        }
      ],
      "id": "1",
      "attributes": {
        "name": "Project User",
      },
      "type": "roles",
      "links": {
        "self": "/roles/1"
      }
    }
  ]
}

twiduch avatar May 18 '17 12:05 twiduch

@twiduch I'm trying to find where in the JSON API docs this behaviour is specified :)

I think that in Json API there should be only one included field

What would your expected behaviour be? That only the first-level of an object tree is included? - Or that the tree is flattened to still allow for side-loading of an entire object graph?

myabc avatar May 20 '17 20:05 myabc

I believe that objects can be nested, but only one included top-level field. The reason is not to repeat same objects.

If User has Roles and User has Friends (of type User). Friends have also Roles. Than you have Roles repeated in different included statement.

There should be relationships nested with type and id. All those objects should be referenced in top-level included

That is my understanding of spec

twiduch avatar May 23 '17 13:05 twiduch

@twiduch is correct. There should only be one included section for the purposes of not duplicating resources. It is mentioned under the top level and compound documents sections of the spec. Likewise it is only allowed as a top level property in the JSON Schema (http://jsonapi.org/schema - line 27).

bencallaway avatar Jun 17 '17 00:06 bencallaway

For me this is a problem due to the fact that my API consuming frontend lib can't (and shouldn't) handle multiple/nested included fields.

Is there a way to workaround this bug? Will this be fixed in the near future?

phortx avatar Nov 27 '17 10:11 phortx