turkraft / springfilter

Dynamically filter JPA entities and Mongo collections with a user-friendly query syntax. Seamless integration with Spring APIs. Star to support the project! ⭐️
https://turkraft.com/springfilter
225 stars 38 forks source link

Filter list with contains-function #236

Closed Dennis-Nilsson closed 1 year ago

Dennis-Nilsson commented 2 years ago

Hello!

Is it possible to filter a List-field with a "contains"-function?

If I got a field with datatype List and I want to filter with something like this: ?filter=stakeholders.roles contains 'ADMIN'

I can't find anything about this in the documentation.

sisimomo commented 1 year ago

You should simply do: ?filter=exists(stakeholders.roles : 'ADMIN')

See doc on subqueries

Dennis-Nilsson commented 1 year ago

That query results in this exception unfortunately:

com.turkraft.springfilter.exception.InternalFilterException: The input 'ADMIN' could not be converted to interface java.util.List
        at com.turkraft.springfilter.parser.generator.expression.ExpressionGenerator.visitInput(ExpressionGenerator.java:482)
        at com.turkraft.springfilter.parser.generator.expression.ExpressionGenerator.visitInput(ExpressionGenerator.java:44)
        at com.turkraft.springfilter.parser.FilterParser$InputContext.accept(FilterParser.java:158)
        at com.turkraft.springfilter.shaded.org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visit(AbstractParseTreeVisitor.java:18)
        at com.turkraft.springfilter.parser.generator.expression.ExpressionGenerator.visitComparison(ExpressionGenerator.java:200)
        at com.turkraft.springfilter.parser.generator.expression.ExpressionGenerator.visitInfix(ExpressionGenerator.java:172)
        at com.turkraft.springfilter.parser.generator.expression.ExpressionGenerator.visitInfix(ExpressionGenerator.java:44)
        at com.turkraft.springfilter.parser.FilterParser$InfixContext.accept(FilterParser.java:263)
        at com.turkraft.springfilter.shaded.org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visit(AbstractParseTreeVisitor.java:18)
        at com.turkraft.springfilter.parser.generator.expression.ExpressionGenerator.run(ExpressionGenerator.java:53)
        at com.turkraft.springfilter.parser.generator.expression.ExpressionGenerator.run(ExpressionGenerator.java:58)
        at com.turkraft.springfilter.parser.generator.expression.ExpressionGenerator.getSubquery(ExpressionGenerator.java:449)
        at com.turkraft.springfilter.parser.generator.expression.ExpressionGenerator.visitFunction(ExpressionGenerator.java:401)
        at com.turkraft.springfilter.parser.generator.expression.ExpressionGenerator.visitFunction(ExpressionGenerator.java:44)
        at com.turkraft.springfilter.parser.FilterParser$FunctionContext.accept(FilterParser.java:214)
        at com.turkraft.springfilter.shaded.org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visit(AbstractParseTreeVisitor.java:18)
        at com.turkraft.springfilter.parser.generator.expression.ExpressionGenerator.visitFilter(ExpressionGenerator.java:109)
        at com.turkraft.springfilter.parser.generator.expression.ExpressionGenerator.visitFilter(ExpressionGenerator.java:44)
        at com.turkraft.springfilter.parser.FilterParser$FilterContext.accept(FilterParser.java:111)
        at com.turkraft.springfilter.shaded.org.antlr.v4.runtime.tree.AbstractParseTreeVisitor.visit(AbstractParseTreeVisitor.java:18)
        at com.turkraft.springfilter.parser.generator.expression.ExpressionGenerator.run(ExpressionGenerator.java:53)
        at com.turkraft.springfilter.boot.FilterSpecification.toPredicate(FilterSpecification.java:62)
torshid commented 1 year ago

Hi @Dennis-Nilsson

This is fixed in version 3.0.0. You should be able to query 'admin' in stakeholders.roles.