neomerx / json-api

Framework agnostic JSON API (jsonapi.org) implementation
Apache License 2.0
743 stars 66 forks source link

Include array of `Identifier`s #235

Closed untone-survive closed 5 years ago

untone-survive commented 5 years ago

I have a resource that has many child resources of the same type (e.g. category that has many subcategories), to prevent recursion-related problems (we provide both parent and children relationships) I pass only subcategories IDs and not the whole resource object in Schema's getRelationships().

I've tried to use array of Identifiers, but got error

No Schema found for resource `Neomerx\\JsonApi\\Schema\\Identifier`

which is frustrating. Now I create semi-fake resource objects, that contain only IDs to get identifier objects in json:

        if (count($resource->getChildren())) {
            $childResources = [];

            foreach ($resource->getChildren() as $childId) {
                // create fake resource to provide ID
                $childResources[] = new Category(['id' => $childId]);
            }

            $result['children'] = [
                self::RELATIONSHIP_DATA => $childResources,
                self::RELATIONSHIP_LINKS_SELF => false,
            ];

        }

        return $result;

Is this the desired way?

neomerx commented 5 years ago

Hi, that looks like a bug. Identifiers should work just fine in arrays as not in arrays. I've made some changes in develop branch and it should work now. Please have a look and let me know if on your side it works as well.

neomerx commented 5 years ago

BTW, I believe the lib can work with resources that have parent and children interrelationships just fine. It's covered by tests.

neomerx commented 5 years ago

@h8every1 ping

neomerx commented 5 years ago

I haven't received any feedback from you on the fix for some time. Let me know if there are any issues remaining.

Fix released in v3.0.4.

untone-survive commented 5 years ago

Sorry for the delay. Everything seems to be working OK now.