phly / PhlyRestfully

ZF2 module for creating RESTful JSON APIs using HAL and API-Problem
108 stars 45 forks source link

HalCollection comprised of HalResources will not render correctly #70

Closed MichaelGooden closed 11 years ago

MichaelGooden commented 11 years ago

Fix in PR #71

In PhlyRestfully\Plugin\HalLinks::extractCollection() we do not check whether the current $resource is instanceof HalResource. The result is that if you are using a ClassMethods hydrator, the resulting array then looks like ["links"=>xx].

The reason I want to return HalResources from my $collection is so that I can compose links of sub resources using this feature.

If there is an easier way to include resource links in collections that I am missing, please advise.

Why I need this:

{"_links": {
    "self": {
        "href": "https://the.internet/api/customers/5151682bbb467/contacts"
    }
},
"_embedded": {
    "items": [
        {
            "id": "5151681710977",
            "name": "Michael Gooden",
            "position": "Owner",
            "notes": "Awesome guy\nSeriously! ReAlly",
            "_links": {
                "phones": {
                    "href": "https://the.internet/api/contacts/5151681710977/phones"
                },
                "self": {
                    "href": "https://the.internet/api/contacts/5151681710977"
                }
            }
        },
        {
            "id": "51519c5562d82",
            "name": "Ian David Gooden",
            "position": "Accountant",
            "notes": "The boring old guy\nHey look a new line!",
            "_links": {
                "phones": {
                    "href": "https://the.internet/api/contacts/51519c5562d82/phones"
                },
                "self": {
                    "href": "https://the.internet/api/contacts/51519c5562d82"
                }
            }
        }
    ]
}}

This is a Collection of Contacts. I need to have the phone link on each of my Contacts.

weierophinney commented 11 years ago

Ideally, we should also have a mechanism in the metadata map for specifying additional links to include in resources. This would likely require some sort of event system to allow developers to dynamically create such links (which is what you're doing here, too).

MichaelGooden commented 11 years ago

Well would you always require an event to specify your links? In my above example I could tell the metadata map the link relation, and route for my resource links. That would be all it needs to create the link on each resource. While I do see the need to have an event system in place to later possibly overwrite these links for more complex reasons, would having to do this in an event for basic links be necessary?

weierophinney commented 11 years ago

@MichaelGooden No -- if you have a links collection already, you would not need to trigger events, necessarily. But if there are links in the metadata for a given resource type, it would make sense. I'm mainly just making notes to myself here. :)