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

FieldEnd.hasAnyOf() fails with exception on empty Iterable #846

Closed
daveehrenberger opened this issue Sep 14, 2015 · 1 comment
Closed

FieldEnd.hasAnyOf() fails with exception on empty Iterable #846

daveehrenberger opened this issue Sep 14, 2015 · 1 comment
Labels
Milestone

Comments

@daveehrenberger
Copy link
Contributor

When trying to utilize the hasAnyOf() method within a Query, if the Iterable passed in points to an empty Collection, the following exception is thrown. I would have expected a more graceful handling of this such that asList() just returns an empty List.

Morphia: 1.0.1
JDK: 1.7.0_67
MongoDB: 2.13.3

Stacktrace:

com.mongodb.MongoException: Can't canonicalize query: BadValue $in needs an array
    at com.mongodb.QueryResultIterator.throwOnQueryFailure(QueryResultIterator.java:246)
    at com.mongodb.QueryResultIterator.init(QueryResultIterator.java:224)
    at com.mongodb.QueryResultIterator.initFromQueryResponse(QueryResultIterator.java:184)
    at com.mongodb.QueryResultIterator.<init>(QueryResultIterator.java:62)
    at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:86)
    at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:66)
    at com.mongodb.DBCursor._check(DBCursor.java:498)
    at com.mongodb.DBCursor._hasNext(DBCursor.java:621)
    at com.mongodb.DBCursor.hasNext(DBCursor.java:657)
    at org.mongodb.morphia.query.MorphiaIterator.hasNext(MorphiaIterator.java:57)
    at org.mongodb.morphia.query.QueryImpl.asList(QueryImpl.java:323)
    at com.fs.core.test.dao.SampleTest.testAddEmpty(SampleTest.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
    at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:105)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:98)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:61)
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:54)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:52)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Sample code:

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import junit.framework.Assert;

import org.bson.types.ObjectId;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.query.Query;

import atunit.AtUnit;
import atunit.Container;
import atunit.Unit;

import com.google.inject.Inject;

@RunWith(AtUnit.class)
@Container(Container.Option.GUICE)
public class SampleTest
{
  @Inject Datastore ds;
  @Unit Data data;

  @Test
  public void testAddEmpty()
  {
    data = new Data();

    Query<Data> query = ds.createQuery( Data.class );
    Set<ObjectId> memberships = data.otherIds;

    query.or(
              query.criteria( "id" ).hasAnyOf( memberships ),
              query.criteria( "otherIds" ).hasAnyOf( memberships )
            );

    List<Data> dataList = query.asList();

    Assert.assertEquals( 0, dataList.size() );
  }


  @Entity("data")
  private static class Data
  {
    private ObjectId id;
    private Set<ObjectId> otherIds;

    public Data()
    {
      otherIds = new HashSet<>();
    }
  }
}
@evanchooly evanchooly added the bug label Dec 21, 2015
@evanchooly evanchooly added this to the 1.1.0 milestone Dec 21, 2015
@evanchooly
Copy link
Member

java generics strike again. I think I have a fix, though.

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

No branches or pull requests

2 participants