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

double included fields #26

Open
twiduch opened this issue May 18, 2017 · 4 comments
Open

double included fields #26

twiduch opened this issue May 18, 2017 · 4 comments
Labels

Comments

@twiduch
Copy link

twiduch commented May 18, 2017

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"
      }
    }
  ]
}
@myabc
Copy link
Collaborator

myabc commented May 20, 2017

@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?

@twiduch
Copy link
Author

twiduch commented May 23, 2017

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

@bencallaway
Copy link

@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).

@phortx
Copy link

phortx commented Nov 27, 2017

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants