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

Resources with an ID of "0" disappear when using ClassHydrator #9

Closed
shrink opened this issue Dec 3, 2017 · 1 comment
Closed

Resources with an ID of "0" disappear when using ClassHydrator #9

shrink opened this issue Dec 3, 2017 · 1 comment

Comments

@shrink
Copy link

shrink commented Dec 3, 2017

As far as I can tell from the spec the only requirement for an ID is that it's a string, there are no limitations otherwise, so an ID of 0 is valid.

Example JSON-API response from jsonapi.org where a comment has an ID set to 0.

{
  "links": {
    "self": "http://example.com/articles",
    "next": "http://example.com/articles?page[offset]=2",
    "last": "http://example.com/articles?page[offset]=10"
  },
  "data": [{
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "JSON API paints my bikeshed!"
    },
    "relationships": {
      "author": {
        "links": {
          "self": "http://example.com/articles/1/relationships/author",
          "related": "http://example.com/articles/1/author"
        },
        "data": { "type": "people", "id": "9" }
      },
      "comments": {
        "links": {
          "self": "http://example.com/articles/1/relationships/comments",
          "related": "http://example.com/articles/1/comments"
        },
        "data": [
          { "type": "comments", "id": "0" },
          { "type": "comments", "id": "12" }
        ]
      }
    },
    "links": {
      "self": "http://example.com/articles/1"
    }
  }],
  "included": [{
    "type": "people",
    "id": "9",
    "attributes": {
      "first-name": "Dan",
      "last-name": "Gebhardt",
      "twitter": "dgeb"
    },
    "links": {
      "self": "http://example.com/people/9"
    }
  }, {
    "type": "comments",
    "id": "0",
    "attributes": {
      "body": "I am a comment with an ID of 0!"
    },
    "relationships": {
      "author": {
        "data": { "type": "people", "id": "2" }
      }
    },
    "links": {
      "self": "http://example.com/comments/0"
    }
  }, {
    "type": "comments",
    "id": "12",
    "attributes": {
      "body": "I like XML better"
    },
    "relationships": {
      "author": {
        "data": { "type": "people", "id": "9" }
      }
    },
    "links": {
      "self": "http://example.com/comments/12"
    }
  }]
}
$hydrator = new ClassHydrator();
$articles = $hydrator->hydrate($response->document());

foreach ($articles[0]->comments as $comment) {
   echo $comment->body . "\n";
}

You would expect to see:

I am a comment with an ID of 0!
I like XML better

Actually see:

I like XML better
@shrink shrink changed the title When hydrating, if a resource ID is "0", that resource is not available in the results Resources with an ID of "0" disappear when using ClassHydrator Dec 3, 2017
@kocsismate
Copy link
Member

kocsismate commented Dec 6, 2017

Hi!

That was a really nice catch :) My last commit should solve the problem (6a6ab89). And a release with the fix is on its way.

Cheers

@shrink shrink closed this as completed Dec 9, 2017
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