Skip to content
This repository was archived by the owner on Jan 13, 2022. It is now read-only.

Only "data" key is cast to GraphNode #700

Closed
jonny-no1 opened this issue Nov 14, 2016 · 10 comments · Fixed by #1034
Closed

Only "data" key is cast to GraphNode #700

jonny-no1 opened this issue Nov 14, 2016 · 10 comments · Fixed by #1034
Labels
Milestone

Comments

@jonny-no1
Copy link
Contributor

jonny-no1 commented Nov 14, 2016

I'm retrieving a page's ratings including their id by requesting: /v2.8/*PAGE_ID*/ratings?fields=open_graph_story. The response has the following format:

{
  "data": [
    {
      "open_graph_story": {
        "message": "*REVIEW_TEXT*",
        "start_time": "2016-11-08T14:09:42+0000",
        "type": "places.rates",
        "data": {
          "rating": {
            "value": 5,
            "scale": 5
          },
          "review_text": "*REVIEW_TEXT*",
          "language": "en",
          "generic_place": {
            "id": "*PAGE_ID*",
            "title": "*PAGE_NAME*",
            "type": "place",
            "url": "*PAGE_URL*"
          }
        },
        "id": "*REVIEW_ID*"
      }
    },
  ]
}

Each object in the data array has a key id that contains the review's story id. However, it is dropped when the reviews are casted into GraphNode objects:

object(Facebook\GraphNodes\GraphEdge)#1878 (5) {
  ["items":protected]=>
  array(25) {
    [0]=>
    object(Facebook\GraphNodes\GraphNode)#1761 (1) {
      ["items":protected]=>
      array(1) {
        ["open_graph_story"]=>
        object(Facebook\GraphNodes\GraphNode)#1762 (1) {
          ["items":protected]=>
          array(4) {
            ["rating"]=>
            object(Facebook\GraphNodes\GraphNode)#1774 (1) {
              ["items":protected]=>
              array(2) {
                ["value"]=>
                int(5)
                ["scale"]=>
                int(5)
              }
            }
            ["review_text"]=>
            string(928) "*REVIEW_TEXT*"
            ["language"]=>
            string(2) "en"
            ["generic_place"]=>
            object(Facebook\GraphNodes\GraphNode)#1760 (1) {
              ["items":protected]=>
              array(4) {
                ["id"]=>
                string(15) "*PAGE_ID*"
                ["title"]=>
                string(10) "*PAGE_NAME*"
                ["type"]=>
                string(5) "place"
                ["url"]=>
                string(35) "*PAGE_URL*"
              }
            }
          }
        }
      }
    }
  }
}

Responsible is this:

// Sometimes Graph is a weirdo and returns a GraphNode under the "data" key
$data = $data['data'];

Graph is being a weirdo and doesn't include all information under the data key.

@jonny-no1
Copy link
Contributor Author

The missing id field bug might get solved in 6.0.0 via #437 anyway, but please keep in mind.

@jonny-no1 jonny-no1 changed the title Can't access rating field open_graph_story Only data key is casted to GraphNode Nov 27, 2016
@jonny-no1 jonny-no1 changed the title Only data key is casted to GraphNode Only "data" key is casted to GraphNode Nov 27, 2016
@SammyK
Copy link
Contributor

SammyK commented Dec 13, 2016

Hey @foaly-nr1! Yes - you've stumbled upon a bug in the overly-complex Graph-node casting. :/ We should do some digging to find out what's causing this, but until then, a workaround is to bypass the GraphEdges & GraphNodes and just access the array directly with getDecodedBody():

$res = $fb->get('/{pageId}/ratings?fields=open_graph_story', $accessToken);
$ratings = $res->getDecodedBody();
foreach ($ratings['data'] as $rating) {
	var_dump($rating['open_graph_story']['id']);
}

I hope that helps! :)

@SammyK SammyK added the bug label Dec 13, 2016
@jonny-no1
Copy link
Contributor Author

Thanks @SammyK that's super helpful.

What's causing this is that when you descent through the nodes to cast them, you either cast the full node or when there is a "data" key, you cast the "data" key instead. So any structure that has information on the same level as a data key will lead to this information being lost. Example:

{
    aRandomNode: {
        thisWillBeLost: 123,
        data: {
            thisWillBeUnderRandomNode: 456
        }
}

@SammyK
Copy link
Contributor

SammyK commented Dec 13, 2016

Hey @foaly-nr1! Good call. I think this is the problem right here. :)

@tolbon
Copy link
Contributor

tolbon commented Jul 5, 2017

@SammyK @yguedidi
Do you have an example when "Graph is a weirdo" ?

It is not possible to just check like this ?

if (\count($data) === 1)
{
    // Sometimes Graph is a weirdo and returns a GraphNode under the "data" key
    $data = $data['data'];
}

@SammyK
Copy link
Contributor

SammyK commented Jul 5, 2017

@yguedidi Yes, when grabbing a user's profile picture for example: /me?fields=id,picture would result in something like:

{
  "id": "123456",
  "picture": {
    "data": {
      "is_silhouette": false,
      "url": "https://scontent.xx.fbcdn.net/p50x50/foo.jpg"
    }
  }
}

@tolbon
Copy link
Contributor

tolbon commented Jul 5, 2017

Ok I test tomorrow but my "patch" seems to be ok

@jonny-no1
Copy link
Contributor Author

@tolbon can you create a PR please?

@tolbon
Copy link
Contributor

tolbon commented Jul 5, 2017

@foaly-nr1 Yep tomorrow after my test :)

@yguedidi
Copy link
Contributor

This will be fixed soon, finally! :)

@jonny-no1 jonny-no1 changed the title Only "data" key is casted to GraphNode Only "data" key is cast to GraphNode Mar 12, 2018
@yguedidi yguedidi added this to the 5.7.0 milestone Nov 21, 2018
yguedidi added a commit that referenced this issue Nov 23, 2018
Conserve id when next to data key, resolves #700
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants