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

Filtering validator broken when query resource by id #218

Closed
danDanV1 opened this issue Aug 18, 2018 · 4 comments
Closed

Filtering validator broken when query resource by id #218

danDanV1 opened this issue Aug 18, 2018 · 4 comments
Milestone

Comments

@danDanV1
Copy link

danDanV1 commented Aug 18, 2018

Filtering validator does not read filter values when requesting resource by id.

Can be replicated using the demo-laravel-json-api.

by ID
http://demo-laravel-json-api.homestead/api/v1/posts/15?filter[title]=Voluptas perspiciatis

{
    "errors": [
        {
            "title": "Filter should contain only allowed values.",
            "source": {
                "parameter": "filter"
            }
        }
    ]
}

by index filter is recognized works as expected
http://demo-laravel-json-api.homestead/api/v1/posts?filter[title]=Voluptas perspiciatis

{
    "meta": {
        "page": {
            "current-page": 1,
            "per-page": 15,
            "from": 1,
            "to": 1,
            "total": 1,
            "last-page": 1
        }
    },
    "links": {
        "first": "http://demo-laravel-json-api.homestead/api/v1/posts?filter%5Btitle%5D=Voluptas+perspiciatis&page%5Bnumber%5D=1&page%5Bsize%5D=15",
        "last": "http://demo-laravel-json-api.homestead/api/v1/posts?filter%5Btitle%5D=Voluptas+perspiciatis&page%5Bnumber%5D=1&page%5Bsize%5D=15"
    },
    "data": [
        {
            "type": "posts",
            "id": "1",
            "attributes": {
                "created-at": "2018-08-17T22:50:11+00:00",
                "updated-at": "2018-08-17T22:50:11+00:00",
                "title": "Voluptas perspiciatis porro.",
                "slug": "quod-molestias-id-non-quidem-assumenda-itaque",
                "content": "Possimus optio nulla repellat. Sint laudantium dolores est ratione qui et. Sit fuga aut dicta ea numquam pariatur ut. Id velit veniam quibusdam voluptas sit qui.\n\nConsequatur nihil dolorum sit iusto ut delectus quaerat. Eos nihil porro molestiae nam nulla corporis fugiat voluptas. Modi explicabo atque repellendus quas dolor aut quia voluptates. Quidem quisquam accusantium veritatis eum architecto mollitia ducimus.\n\nNeque doloremque commodi ratione repellat debitis hic aperiam occaecati. Et eaque quidem voluptatum recusandae fugiat et sapiente minus.",
                "published-at": "2010-02-23T16:52:53+00:00"
            },
            "relationships": {
                "author": {
                    "links": {
                        "self": "http://demo-laravel-json-api.homestead/api/v1/posts/1/relationships/author",
                        "related": "http://demo-laravel-json-api.homestead/api/v1/posts/1/author"
                    }
                },
                "comments": {
                    "links": {
                        "self": "http://demo-laravel-json-api.homestead/api/v1/posts/1/relationships/comments",
                        "related": "http://demo-laravel-json-api.homestead/api/v1/posts/1/comments"
                    }
                },
                "tags": {
                    "links": {
                        "self": "http://demo-laravel-json-api.homestead/api/v1/posts/1/relationships/tags",
                        "related": "http://demo-laravel-json-api.homestead/api/v1/posts/1/tags"
                    }
                }
            },
            "links": {
                "self": "http://demo-laravel-json-api.homestead/api/v1/posts/1"
            }
        }
    ]
}

I want to filter by related model. For the sake of using demo-laravel-json-api, I'm just using the existing user model relation for proof of concept. But you can imagine appropriate scenario for filtering a has-many relationship.

Working example:
http://demo-laravel-json-api.homestead/api/v1/posts?include=author&filter[user.name]=Fabian

Using a custom filter:

        if ($filters->has('user.name')) {
            $query->with(['author' => function ($related) use ($filters) {
                $related->where('name', 'like', '%' . $filters->get('user.name') . '%');
            }]);
        }

Works as expected.
-Returns all posts and includes only user (author) record for Fabian.

Issue is querying by record ID, filters aren't recognized. Filters are valid as established in previous examples.
http://demo-laravel-json-api.homestead/api/v1/posts/15?include=author&filter[user.name]=Fabian

{
    "errors": [
        {
            "title": "Filter should contain only allowed values.",
            "source": {
                "parameter": "filter"
            }
        }
    ]
}

How to implement filtering when fetching record by ID?

@lindyhopchris
Copy link
Member

Filtering on a specific resource's endpoint is not supported by this package, which is why you're getting the error.

Instead the package allows filtering by id values on the index route, i.e. you can do any of the following:

GET /api/posts?filter[id]=1
GET /api/posts?filter[id][]=1&filter[id][]=4
GET /api/posts?filter[id]=1,4

At the moment though if the id filter is provided, it ignores all other filters... however I have been planning to fix this because we have a scenario at work where we want to do:

GET /api/posts?filter[id]=1,4&published=1

I.e. give me posts 1 and 4, but only if they are published. I think this is equivalent to your scenario.

I was going to do a release very soon (in the next few days), so I'll include this fix in it.

@danDanV1
Copy link
Author

Thanks for the quick response.

Looks like the secondary issue I just reported:
#219 might be the same issue you're mentioning to publish the fix about shortly?

@lindyhopchris
Copy link
Member

Have added this feature to the develop branch (which will be 1.0.0-beta.1.

GET /api/posts/1?filter['published']=1

Returns post 1 if it is published.

The same is also now possible on to-one relations:

GET /api/comments/1/post?filter['published']=1

Returns the post that is associated to comment 1 if that post is published.

@lindyhopchris
Copy link
Member

lindyhopchris commented Aug 21, 2018

Sort and page params are still not supported on these endpoints because there is no point sorting and paging a resource end-point that is going to return a specific resource or null.

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

No branches or pull requests

2 participants