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

ResourceIdentifier::fromArray returning null not being handled gracefully #30

Closed
emil-nasso opened this issue Aug 2, 2016 · 3 comments
Labels
Milestone

Comments

@emil-nasso
Copy link
Contributor

If you need to fetch a relationship from a request, you would normally do it like this:

$resource = $jsonApi->getRequest()->getResourceToOneRelationship('spaceship');

This returns a ToOneRelationShip-object, if the relationship is found, or null if it is not.

The getResourceToOneRelationship method in the Request class looks like this

public function getResourceToOneRelationship($relationship)
    {
        $data = $this->getResource();
        if (isset($data["relationships"][$relationship]["data"]) === false) {
            return null;
        }
        return new ToOneRelationship(ResourceIdentifier::fromArray($data["relationships"][$relationship]["data"]));
    }

The last line calls the fromArray method. The problem is that the fromArray method does this check:

if (isset($array["type"]) === false || isset($array["id"]) === false) {
            return null;
        }

So an invalid relationship returns null, but the ToOneRelationship object gets created anyway. I think it would be clearer if getResourceToOneRelationship returned null in this case.

To catch the case where the input is malformed, you would have to write code like this with the current code:

 $resource = $jsonApi->getRequest()->getResourceToOneRelationship('spaceship');
        if (isset($resource)) {
           $resourceIdentifier = $resource->getResourceIdentifier();
            if ( isset($resourceIdentifier) ) {
                //get the id from the resourceIdentifier
            }
       }

If you don't, you will very likely get an Fatal Error in php.

I would propose that getResourceToOneRelationship was changed to make sure that ResourceIdentifier:fromArray considers the data valid, something like this:

public function getResourceToOneRelationship($relationship)
    {
        $data = $this->getResource();

        if (isset($data["relationships"][$relationship]["data"]) === false) {
            return null;
        }

        $resourceIdentifier = ResourceIdentifier::fromArray($data["relationships"][$relationship]["data"]);

        if (isset($resourceIdentifier) === false) {
            return null;
        }

        return new ToOneRelationship($resourceIdentifier);
    }

With this in place, getResourceToOneRelationship would return null if the relationship didn't exist, or one of the fields type or id was missing.

The getResourceToManyRelationship would need to be fixed too. I'm not 100% sure on how though. Skip non valid relationships? Return null from the method if one of the identifiers are invalid? Rise some kind of exception? Exceptions could perhaps be used in the getResourceToOneRelationship case too? Some kind of malformed input exception.

I didn't consider this fully thought through, so i didn't create a PR. What do you think about all this?

@emil-nasso emil-nasso changed the title ResourceIdentifier::fromArray returning null not being handles gracefully ResourceIdentifier::fromArray returning null not being handled gracefully Aug 2, 2016
@emil-nasso
Copy link
Contributor Author

This functionality is required when you send null as the data for a relationship. In that case, you want to clear the relationship completely. I'm working on a few things related to that. I will create a PR soon.

@kocsismate
Copy link
Member

Thank you very much for your work! I will come back from holiday on Sunday, so I can only deal with the issues early next week :)

@kocsismate kocsismate added this to the 0.11 milestone Aug 5, 2016
@kocsismate kocsismate added the bug label Aug 5, 2016
@kocsismate
Copy link
Member

I am closing this issue as I think I resolved it in this commit (78c316d), but feel free to reopen it and tell me if you think otherwise!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants