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

Error when search for an element in null Collection/Array #24

Open
FHamster opened this issue Mar 15, 2021 · 0 comments
Open

Error when search for an element in null Collection/Array #24

FHamster opened this issue Mar 15, 2021 · 0 comments

Comments

@FHamster
Copy link

This error occurs when predicatevisitor is used as the visitor of AST

Predicate<Element> predicate = condition.query(new PredicateVisitor<>());

I have a collection like this

[
  {
    "_id": "604da82b13b1687602fd97e5",
    "author": [
      {
        "_VALUE": "Frank Piessens"
      }
    ]
  },
  {
    "_id": "604da89613b1687602141823",
    "author": null
  }
]    

The RSQL query will process

author._VALUE=="Frank Piessens"

Then the error happended.The log is very simple.

java.lang.NullPointerException: null
	at com.github.rutledgepaulv.qbuilders.visitors.PredicateVisitor.recurseSingle(PredicateVisitor.java:232) ~[q-builders-1.5.jar:na]
	...

I found out that the code in PredicateVisitor.recurseSingle isn't null safe.The if statement will throw a exception,when parameter root is null.So I override the method PredicateVisitor.recurseSingle in q-builder https://github.com/RutledgePaulV/q-builders/blob/develop/src/main/java/com/github/rutledgepaulv/qbuilders/visitors/PredicateVisitor.java

private boolean recurseSingle(Object root, String field, ComparisonNode node, BiPredicate<Object, Object> func) {
  			//Add an if statement
  			//Ensure null safe
        if (root == null) {
            return Stream.empty().anyMatch(t -> resolveSingleField(t, field, node, func));
        }
        if (root.getClass().isArray()) {
            return Arrays.stream((Object[]) root).anyMatch(t -> resolveSingleField(t, field, node, func));
        }

        if (root instanceof Collection) {
            return ((Collection<Object>) root).stream().anyMatch(t -> resolveSingleField(t, field, node, func));
        }

        return resolveSingleField(root, field, node, func);
    }

Hope to help others.

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

No branches or pull requests

1 participant