Skip to content

Commit

Permalink
Fix for ensureIndexes() logic (MorphiaOrg#1211)
Browse files Browse the repository at this point in the history
* added integration tests to confirm the issue MorphiaOrg#1175

* collect indexes from subclasses only if field is an interface or abstract class

* fixed errors reported by checkstyle
  • Loading branch information
darklynx authored and VPriesnitz committed Oct 5, 2018
1 parent 0c78bd5 commit 0d136d8
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
4 changes: 3 additions & 1 deletion morphia/src/main/java/org/mongodb/morphia/IndexHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ private List<Index> collectNestedIndexes(final MappedClass mc, final List<Mapped
List<MappedClass> classes = new ArrayList<MappedClass>();
MappedClass mappedClass = mapper.getMappedClass(mf.isSingleValue() ? mf.getType() : mf.getSubClass());
classes.add(mappedClass);
classes.addAll(mapper.getSubTypes(mappedClass));
if (mappedClass.isInterface() || mappedClass.isAbstract()) {
classes.addAll(mapper.getSubTypes(mappedClass));
}
for (MappedClass aClass : classes) {
for (Index index : collectIndexes(aClass, parents)) {
List<Field> fields = new ArrayList<Field>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.mongodb.morphia.issue1175;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mongodb.morphia.testutil.IndexMatcher.doesNotHaveIndexNamed;
import static org.mongodb.morphia.testutil.IndexMatcher.hasIndexNamed;

import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.mongodb.morphia.TestBase;
import org.mongodb.morphia.annotations.Embedded;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.annotations.Indexed;
import org.mongodb.morphia.utils.IndexDirection;

import com.mongodb.DBObject;

public class EnsureIndexesTest extends TestBase {
@Test
public final void ensureIndexesFromSuperclasses() {
// given
getMorphia().map(Person.class, Employee.class, Company.class);

// when
getAds().ensureIndexes();

// then
List<DBObject> indexInfo = getDs().getCollection(Company.class).getIndexInfo();
assertEquals(4, indexInfo.size());
assertThat(indexInfo, hasIndexNamed("_id_"));
assertThat(indexInfo, hasIndexNamed("employees.birthday_-1"));
assertThat(indexInfo, hasIndexNamed("employees.fullName_1"));
assertThat(indexInfo, hasIndexNamed("employees.employeeId_1"));
}

@Test
public final void ensureIndexesWithoutSubclasses() {
// given
getMorphia().map(Person.class, Employee.class, Contract.class);

// when
getAds().ensureIndexes();

// then
List<DBObject> indexInfo = getDs().getCollection(Contract.class).getIndexInfo();
assertEquals(3, indexInfo.size());
assertThat(indexInfo, hasIndexNamed("_id_"));
assertThat(indexInfo, hasIndexNamed("person.birthday_-1"));
assertThat(indexInfo, hasIndexNamed("person.fullName_1"));
assertThat(indexInfo, doesNotHaveIndexNamed("person.employeeId_1"));
}

@Embedded
public static class LivingBeing {
@Indexed(IndexDirection.DESC)
private Date birthday;
}

@Embedded
public static class Person extends LivingBeing {
@Indexed
private String fullName;
}

@Embedded
public static class Employee extends Person {
@Indexed
private Long employeeId;
private String title;
}

@Entity
public static class Contract {
@Id
private Long contractId;
private Long companyId;
@Embedded
private Person person;
}

@Entity
public static class Company {
@Id
private Long companyId;
@Embedded
private List<Employee> employees;
}
}

0 comments on commit 0d136d8

Please sign in to comment.