Skip to content

Collection representation #23

@Kris-LIBIS

Description

@Kris-LIBIS

I have some issues using the JSONAPI output for collections. I'm generating the collection output with:

orgs = paginate(Libis::Ingester::Organization.all)
Representer::OrganizationRepresenter.for_collection.prepare(orgs).to_hash(pagination_hash(orgs))

The options generated by 'pagination_hash' looks like this:

    {
      user_options:
      {
        ...,
        links:
        {
          self: "http://localhost:9393/api/organizations?per_page=2&page=1",
          first: "http://localhost:9393/api/organizations?per_page=2&page=1",
          last: "http://localhost:9393/api/organizations?per_page=2&page=3",
          next: "http://localhost:9393/api/organizations?per_page=2&page=2"
        }
      },
      meta: { per_page:2, :total=>5, :page=>1, :max_page=>3, :next_page=>2, :prev_page=>nil}
    }

The generated hash looks like this:

{
    "data": [
        {
            "id": "58b7ccd7e852dd0775f51cae",
            "attributes": {
                "name": "Test Ingest Organization"
            },
            "type": "organization",
            "links": {
                "self": "http://localhost:9393/api/organizations/58b7ccd7e852dd0775f51cae",
                "all": "http://localhost:9393/api/organizations"
            },
            "meta": {
                "per_page": 2,
                "total": 5,
                "page": 1,
                "max_page": 3,
                "next_page": 2,
                "prev_page": null
            }
        },
        {
            "id": "58b7ccd7e852dd0775f51caf",
            "attributes": {
                "name": "Dummy test organization"
            },
            "type": "organization",
            "links": {
                "self": "http://localhost:9393/api/organizations/58b7ccd7e852dd0775f51caf",
                "all": "http://localhost:9393/api/organizations"
            },
            "meta": {
                "per_page": 2,
                "total": 5,
                "page": 1,
                "max_page": 3,
                "next_page": 2,
                "prev_page": null
            }
        }
    ],
    "meta": {
        "per_page": 2,
        "total": 5,
        "page": 1,
        "max_page": 3,
        "next_page": 2,
        "prev_page": null
    }
}

There are two issues with this:

  • the 'meta' section is present on each 'organization', but should only be present as a top level entry.
  • the links are missing on the top-level (the links on the individual 'organization' entries are generated by the Decorator itself and are correct).

The output I'm expecting is:

{
    "data": [
        {
            "id": "58b7ccd7e852dd0775f51cae",
            "attributes": {
                "name": "Test Ingest Organization"
            },
            "type": "organization",
            "links": {
                "self": "http://localhost:9393/api/organizations/58b7ccd7e852dd0775f51cae",
                "all": "http://localhost:9393/api/organizations"
            }
        },
        {
            "id": "58b7ccd7e852dd0775f51caf",
            "attributes": {
                "name": "Dummy test organization"
            },
            "type": "organization",
            "links": {
                "self": "http://localhost:9393/api/organizations/58b7ccd7e852dd0775f51caf",
                "all": "http://localhost:9393/api/organizations"
            }
        }
    ],
    "links": {
        "self": "http://localhost:9393/api/organizations?per_page=2&page=1",
        "first": "http://localhost:9393/api/organizations?per_page=2&page=1",
        "last": "http://localhost:9393/api/organizations?per_page=2&page=3",
        "next": "http://localhost:9393/api/organizations?per_page=2&page=2"
    },
    "meta": {
        "per_page": 2,
        "total": 5,
        "page": 1,
        "max_page": 3,
        "next_page": 2
    }
}

BTW: I'm using latest releases of grape, grape-roar, roar and roar-jsonapi on Ruby 2.3.1.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions