You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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?
The text was updated successfully, but these errors were encountered:
emil-nasso
changed the title
ResourceIdentifier::fromArray returning null not being handles gracefully
ResourceIdentifier::fromArray returning null not being handled gracefully
Aug 2, 2016
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.
If you need to fetch a relationship from a request, you would normally do it like this:
This returns a
ToOneRelationShip
-object, if the relationship is found, ornull
if it is not.The
getResourceToOneRelationship
method in theRequest
class looks like thisThe last line calls the
fromArray
method. The problem is that thefromArray
method does this check:So an invalid relationship returns null, but the
ToOneRelationship
object gets created anyway. I think it would be clearer ifgetResourceToOneRelationship
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:
If you don't, you will very likely get an Fatal Error in php.
I would propose that
getResourceToOneRelationship
was changed to make sure thatResourceIdentifier:fromArray
considers the data valid, something like this:With this in place,
getResourceToOneRelationship
would return null if the relationship didn't exist, or one of the fieldstype
orid
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 thegetResourceToOneRelationship
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?
The text was updated successfully, but these errors were encountered: