rest-layer icon indicating copy to clipboard operation
rest-layer copied to clipboard

Explicit $eq in filter passes whole predicate to Validate(Query)

Open mishak87 opened this issue 5 years ago • 3 comments

I have encountered confusing behavior that does not lead to error.

For example ?filter={field:{$eq:"value"}} will pass map[string]interface{}{"$eq": "value"} instead of "value" to Validate functions. Proper form ?filter={field:"value"} works as expected passing "value". I assume this behavior is caused because query.Equal is a default predicate operator.

mishak87 avatar Aug 03 '19 19:08 mishak87

"$eq" is not recognized as an operator by rest-layer. All operators currently handed can be found here:

https://github.com/rs/rest-layer/blob/369f453dbbffee4c7c275e8038c8bbed5fbf8b5a/schema/query/predicate.go#L12

smyrman avatar Aug 04 '19 21:08 smyrman

MongoDB supports both variants I think compatibility is a strong argument for adding it.

It will be a breaking change to support it.

It would make more convenient playing with filters because all other filters require wrapping in object {<op>:<value>}. Counting closing braces gets bit annoying after a long day.

mishak87 avatar Aug 05 '19 23:08 mishak87

rest-layer only aims to support/allow a sub-set of the MongoDB query syntax, but in this case I agree. We can just add it.

Unfortunately the amount of time I have found to contribute to rest-layer have been quite limited. Are you open to attempt a PR for it? Otherwise I will try to fix it eventually.

On the note of operator parsing, I just want to mention that there are more problems to be aware of... rest-layer only allows one operator within a given dict. E.g. {$lt: 4, $gt:1} would be legal in MongoDB, but not understood by rest-layer. In rest-layer we would instead need to wrap it via an explicit and: {"$and":[{$lt: 4},{$gt:1}]}. Fixing this is a bigger change, so we will leave it out-of-scope.

smyrman avatar Aug 05 '19 23:08 smyrman