cloudant / java-cloudant

A Java client for Cloudant
Apache License 2.0
79 stars 68 forks source link

When using QueryBuilder, the and Operation sometimes doesn't emit an array #515

Closed esrose closed 4 years ago

esrose commented 4 years ago

Bug Description

When I instantiate a QueryBuilder with an and Operation containing only a single Selector, the resulting JSON does not have an array for the $and field inside the selector.

I'm an IBM Cloudant customer.

1. Steps to reproduce and the simplest code sample possible to demonstrate the issue

  1. Create a QueryBuilder: QueryBuilder builder = new QueryBuilder(and(eq("userId", "myUser")));
  2. Execute the query against an existing database: database.query(builder.build(), JsonObject.class);

Alternatively, inspect the selector variable on the QueryBuilder after step 1 to see the incorrect JSON. For example, "$and": {"userId": {"$eq": "myUser"}}

2. What you expected to happen

I expect QueryBuilder to emit JSON which is accepted by CouchDB, meaning $and should always be an array regardless of how many elements are in it.

3. What actually happened

CouchDB gives me a 400 error from the _find endpoint because the $and is malformed.

Environment details

I'm executing the query against CouchDB 3 in my local dev environment.

mojito317 commented 4 years ago

Hi @esrose! First of all, thank you for the bug report!

Now you cannot use $and with a single selector, we will work on the fix.

Till then, you can use QueryBuilder builder = new QueryBuilder(eq("userId", "myUser")) on your single element selectors. E.g.:

if (selectorArray.length == 1) {
 new QueryBuilder(selectorArray[0])
} else {
 new QueryBuilder(and(selectorArray))
}