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

Remove items key from embedded array in paginated list #307

Closed
webdevilopers opened this issue Mar 31, 2020 · 6 comments
Closed

Remove items key from embedded array in paginated list #307

webdevilopers opened this issue Mar 31, 2020 · 6 comments

Comments

@webdevilopers
Copy link

We have a pagerfanta collection representation which uses the items default key on the _embedded array:

{
  "page": 1,
  "limit": 10,
  "pages": 461,
  "total": 4610,
  "_links": {
    "self": {
      "href": "\/service_contracts?page=1&limit=10"
    },
    "first": {
      "href": "\/service_contracts?page=1&limit=10"
    },
    "last": {
      "href": "\/service_contracts?page=461&limit=10"
    },
    "next": {
      "href": "\/service_contracts?page=2&limit=10"
    }
  },
  "_embedded": {
    "items": [
      {
        "contractId": "1f432848-0303-490c-a86f-4a2b1a2a1aba",
        "createdAt": "2015-08-13T00:00:00+02:00"
      },
      {
        "contractId": "097acd77-14bf-4409-855c-677e570f555a",
        "createdAt": "2015-08-13T00:00:00+02:00"
      }
    ]
  }
}

We need to remove the _items key.

However I was able to manage it by using a custom representation based on the original Hateoas\Representation\CollectionRepresentation:

class Representation
{
    /**
     * @var mixed
     */
    private $_embedded;

    /**
     * @param array|\Traversable $resources
     */
    public function __construct($resources)
    {
        if ($resources instanceof \Traversable) {
            $resources = iterator_to_array($resources);
        }

        $this->_embedded      = $resources;
    }

    /**
     * @return mixed
     */
    public function getResources()
    {
        return $this->_embedded;
    }

But this looks like an ugly hack. Is there a better way?

Came from @willdurand @schmittjoh :

@goetas
Copy link
Collaborator

goetas commented Mar 31, 2020

Can you please post the expected JSON?

@webdevilopers
Copy link
Author

{
  "page": 1,
  "limit": 10,
  "pages": 461,
  "total": 4610,
  "_links": {
    "self": {
      "href": "\/service_contracts?page=1&limit=10"
    },
    "first": {
      "href": "\/service_contracts?page=1&limit=10"
    },
    "last": {
      "href": "\/service_contracts?page=461&limit=10"
    },
    "next": {
      "href": "\/service_contracts?page=2&limit=10"
    }
  },
  "_embedded": [
      {
        "contractId": "1f432848-0303-490c-a86f-4a2b1a2a1aba",
        "createdAt": "2015-08-13T00:00:00+02:00"
      },
      {
        "contractId": "097acd77-14bf-4409-855c-677e570f555a",
        "createdAt": "2015-08-13T00:00:00+02:00"
      }
    ]
}

@webdevilopers
Copy link
Author

Maybe it is possible with a custom JsonHalSerializer?

@goetas
Copy link
Collaborator

goetas commented Apr 1, 2020

See my comments in #284 (comment), in particular:

Hateoas\Representation* classes are deprecated, recommended user-land implementations

What you are asking is not part of JSON-HAL as in that spec, each embedded must have a name and be a resource.

@goetas goetas closed this as completed Apr 1, 2020
@webdevilopers
Copy link
Author

webdevilopers commented Apr 2, 2020

Thank you for your feedback @goetas .
I would like to support my UI team with using the actual JSON-HAL spec. Do you have a link where this is mentioned?

@goetas
Copy link
Collaborator

goetas commented Apr 2, 2020

https://tools.ietf.org/html/draft-kelly-json-hal-08#section-4.1.2

It is an object whose property names are link relation types

The part: object whose property names are link relation types, in your case is an array, and this is already wrong by the spec.

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

2 participants