Skip to content

Commit

Permalink
bail out of index building when the field can't found and append rema…
Browse files Browse the repository at this point in the history
…ining path elements to the namePath for potential use in key naming if validation is disabled.
  • Loading branch information
Justin Lee committed Jan 26, 2016
1 parent 17b3157 commit 41a4fdc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 8 additions & 2 deletions morphia/src/main/java/org/mongodb/morphia/DatastoreImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -1336,8 +1336,14 @@ private MappedField findField(final List<String> namePath, final MappedClass mc,
if (value.contains(".")) {
String segment = value.substring(0, value.indexOf("."));
MappedField field = findField(namePath, mc, segment);
MappedClass mappedClass = getMapper().getMappedClass(field.getSubType() != null ? field.getSubType() : field.getConcreteType());
return findField(namePath, mappedClass, value.substring(value.indexOf(".") + 1));
if (field != null) {
MappedClass mappedClass =
getMapper().getMappedClass(field.getSubType() != null ? field.getSubType() : field.getConcreteType());
return findField(namePath, mappedClass, value.substring(value.indexOf(".") + 1));
} else {
namePath.addAll(Arrays.asList(value.split("\\.")));
return null;
}
} else {
MappedField mf = mc.getMappedField(value);
if (mf == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@

package org.mongodb.morphia.mapping;

import com.mongodb.DBObject;
import org.bson.types.ObjectId;
import org.junit.Assert;
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.Field;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.annotations.Index;
import org.mongodb.morphia.annotations.IndexOptions;
import org.mongodb.morphia.annotations.Indexes;
import org.mongodb.morphia.query.ValidationException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EmbeddedMappingTest extends TestBase {
Expand Down Expand Up @@ -53,6 +59,14 @@ public void mapGenericEmbeds() {
@Test
public void testNestedInterfaces() {
getMorphia().map(WithNested.class);
getDs().ensureIndexes();

final List<DBObject> indexInfo = getDs().getCollection(WithNested.class).getIndexInfo();
boolean indexFound = false;
for (DBObject dbObject : indexInfo) {
indexFound |= "nested.field.fail".equals(((DBObject) dbObject.get("key")).keySet().iterator().next());
}
Assert.assertTrue("Should find the nested field index", indexFound);
WithNested nested = new WithNested();
nested.nested = new NestedImpl("nested value");
getDs().save(nested);
Expand Down Expand Up @@ -188,6 +202,10 @@ public int hashCode() {
}
}

@Indexes({
@Index(fields = {@Field("nested.field.fail")},
options = @IndexOptions(disableValidation = true, sparse = true))
})
public static class WithNested {
@Id
private ObjectId id;
Expand Down

0 comments on commit 41a4fdc

Please sign in to comment.