Core: Search filter for null values

Created on 21 May 2015  Â·  6Comments  Â·  Source: api-platform/core

One request often made when filtering a collection by values is the case of the null value. Sadly this is a very painful case because the value retrieved on the server side is always a string and that it's no possible with the current implementation to pass an empty value.

Unfortunately I didn't find much on the subject aside from this post which suggest a "hacky" solution: defining custom values.

I tested a little bit with a StrongLoop application and the following query ?filter[where][property]=null works whatever the type of the value is.

The only problem is for string values. Indeed it becomes impossible to search for a string with the value "null".

@dunglas, @sroze what are your though on this case? Should we do as StrongLoop which has the drawback of disabling the search filter for string properties of value "null"?

Most helpful comment

@theofidry Yeah, just landed in this issue after some research and wanted to share our solution with the next person that ends up here. :-)

All 6 comments

There is only one proper way to handle such case. Using the explicit representation of Hydra: http://www.hydra-cg.com/spec/latest/core/#h-templated-links

Currently we only support BasicRepresentation. Work on that is welcome. An other way to handle such edge cases for now is using custom filters.

Here is a custom filter doing this: https://gist.github.com/blaues0cke/e45a56f32439d8882390

Use it like this: ?property-name-empty=true

Thanks @blaues0cke, I've rolled out mine since then ;) It was more a question of merging it back to the bundle or not.

@theofidry Yeah, just landed in this issue after some research and wanted to share our solution with the next person that ends up here. :-)

In the same spirit as @blaues0cke I'll share our solution inspired by range filter:
?property[null]=true/false

https://gist.github.com/nicholasruunu/db1d840bebe4dd6de3ba20ac776dcf41

Please see #906.

On Tue, 21 Feb 2017, 23:20 Nicholas Ruunu, notifications@github.com wrote:

In the same spirit as @blaues0cke https://github.com/blaues0cke I'll
share our solution inspired by range filter: ?property[null]=true/false

https://gist.github.com/nicholasruunu/db1d840bebe4dd6de3ba20ac776dcf41

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/api-platform/core/issues/70#issuecomment-281374347,
or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAhf65lkGJepR3VvN1Sl6ftN-qsjkvQAks5rewDDgaJpZM4Ej9b8
.

Was this page helpful?
0 / 5 - 0 ratings