Skip to content

Resource ID with value zero throws the exception: No JSON API resource id name set on route #178

Closed
@dacercoolex

Description

@dacercoolex

Trying to get a resource which ID value is zero, kinda similar to an issue found at the former cloudcreativity/laravel-json-api throws the LogicException No JSON API resource id name set on route. Turns out that on funtion modelOrResourceId in Route.php an assingnmet within the if stament is performed and it throws the exception because a logic value false is assumed, instead of the actual 0.

Here's referenced code (at line 102):

    /**
     * @inheritDoc
     */
    public function modelOrResourceId()
    {
        if (!$name = $this->resourceIdName()) {
            throw new LogicException('No JSON API resource id name set on route.');
        }

        if ($modelOrResourceId = $this->route->parameter($name)) {
            return $modelOrResourceId;
        }

        throw new LogicException('No JSON API resource id set on route.');
    }

And this is the workaround the I've implemented in order to prevent the exception being thrown:

    /**
     * @inheritDoc
     */
    public function modelOrResourceId()
    {
        if (!$name = $this->resourceIdName()) {
            throw new LogicException('No JSON API resource id name set on route.');
        }

        if (!is_null($this->route->parameter($name)) && $this->route->parameter($name)!='') {
            return $this->route->parameter($name);
        }

        throw new LogicException('No JSON API resource id set on route.');
    }

I'm not sure if this is the best solution but, considering that it's plausible to have a resource ID = zero, it solves my issues without affecting any other values.

Hope it helps!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions