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

Invalid syntax causes NPE during parse #395

Open shnplr opened 4 months ago

shnplr commented 4 months ago

Hi,

Java 17 + JPA/Specification SpringBoot v3

Have this error on my project by leaving out the final single quote e.g. (status in ['Submitted', 'Completed', 'Pending'] and firstName ~ 'Bob)

Exception:

java.lang.NullPointerException: Cannot invoke "java.util.List.size()" because "tokens" is null
        at com.turkraft.springfilter.parser.AntlrFilterLexer.nextToken(AntlrFilterLexer.java:142)
        at com.turkraft.springfilter.shaded.org.antlr.v4.runtime.BufferedTokenStream.fetch(BufferedTokenStream.java:169)

Would it be feasible to throw a "ParseException" instead of NPE? I'm catching this in the global exception handler but its difficult to send appropriate 4xx http response to the caller - I usually send 5xx for unhandled errors (which could be anything).

Can I convert the filter query parameter to Specification manually within my controller? I could localize the NPE at least

Thanks, great project by the way!

shnplr commented 4 months ago

Possible workaround to inject the FilterSpecificationConverter in the handler component and invoke convert method with the request param filter string inside try catch.