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

Correct deserialization of collection and complex object valued empty… #869

Merged
merged 1 commit into from
Dec 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void eval(final Object k, final Object val) {
}
});

if (!map.isEmpty()) {
if (!map.isEmpty() || mapper.getOptions().isStoreEmpties()) {
mf.setFieldValue(entity, map);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Collection;


/**
Expand Down Expand Up @@ -72,6 +73,42 @@ public void emptyMapStoredWithOptions() throws Exception {
shouldNotFindField(hm);
}

@Test
public void emptyCollectionValuedMapStoredWithOptions() throws Exception {
final HasCollectionValuedMap hm = new HasCollectionValuedMap();
hm.properties = new HashMap<String, Collection<String>>();

//Test default behavior
getMorphia().getMapper().getOptions().setStoreEmpties(false);
shouldNotFindField(hm);

//Test default storing empty map with storeEmpties option
getMorphia().getMapper().getOptions().setStoreEmpties(true);
shouldFindField(hm, new HashMap<String, Collection<String>>());

//Test opposite from above
getMorphia().getMapper().getOptions().setStoreEmpties(false);
shouldNotFindField(hm);
}

@Test
public void emptyComplexObjectValuedMapStoredWithOptions() throws Exception {
final HasComplexObjectValuedMap hm = new HasComplexObjectValuedMap();
hm.properties = new HashMap<String, ComplexObject>();

//Test default behavior
getMorphia().getMapper().getOptions().setStoreEmpties(false);
shouldNotFindField(hm);

//Test default storing empty map with storeEmpties option
getMorphia().getMapper().getOptions().setStoreEmpties(true);
shouldFindField(hm, new HashMap<String, ComplexObject>());

//Test opposite from above
getMorphia().getMapper().getOptions().setStoreEmpties(false);
shouldNotFindField(hm);
}

@Test
public void lowercaseDefaultCollection() {
DummyEntity entity = new DummyEntity();
Expand Down Expand Up @@ -137,6 +174,22 @@ private void shouldFindField(final HasMap hl, final Map<String, String> expected
Assert.assertEquals(expected, getDs().createQuery(HasMap.class).get().properties);
}

private void shouldFindField(final HasCollectionValuedMap hm, final Map<String, Collection<String>> expected) {
final DBObject dbObj;
getDs().save(hm);
dbObj = getDs().getCollection(HasCollectionValuedMap.class).findOne();
Assert.assertTrue("Should find the field", dbObj.containsField("properties"));
Assert.assertEquals(expected, getDs().createQuery(HasCollectionValuedMap.class).get().properties);
}

private void shouldFindField(final HasComplexObjectValuedMap hm, final Map<String, ComplexObject> expected) {
final DBObject dbObj;
getDs().save(hm);
dbObj = getDs().getCollection(HasComplexObjectValuedMap.class).findOne();
Assert.assertTrue("Should find the field", dbObj.containsField("properties"));
Assert.assertEquals(expected, getDs().createQuery(HasComplexObjectValuedMap.class).get().properties);
}

private void shouldNotFindField(final HasMap hl) {
getDs().save(hl);
DBObject dbObj = getDs().getCollection(HasMap.class).findOne();
Expand All @@ -151,6 +204,20 @@ private void shouldNotFindField(final HasList hl) {
Assert.assertNull(getDs().createQuery(HasList.class).get().names);
}

private void shouldNotFindField(final HasCollectionValuedMap hm) {
getDs().save(hm);
DBObject dbObj = getDs().getCollection(HasCollectionValuedMap.class).findOne();
Assert.assertFalse("field should not exist, value = " + dbObj.get("properties"), dbObj.containsField("properties"));
Assert.assertNull(getDs().createQuery(HasCollectionValuedMap.class).get().properties);
}

private void shouldNotFindField(final HasComplexObjectValuedMap hm) {
getDs().save(hm);
DBObject dbObj = getDs().getCollection(HasComplexObjectValuedMap.class).findOne();
Assert.assertFalse("field should not exist, value = " + dbObj.get("properties"), dbObj.containsField("properties"));
Assert.assertNull(getDs().createQuery(HasComplexObjectValuedMap.class).get().properties);
}

private static class HasList implements Serializable {
@Id
private ObjectId id = new ObjectId();
Expand All @@ -169,8 +236,30 @@ private static class HasMap implements Serializable {
}
}

private static class HasCollectionValuedMap implements Serializable {
@Id
private ObjectId id = new ObjectId();
private Map<String, Collection<String>> properties;

HasCollectionValuedMap() {
}
}

private static class HasComplexObjectValuedMap implements Serializable {
@Id
private ObjectId id = new ObjectId();
private Map<String, ComplexObject> properties;

HasComplexObjectValuedMap() {
}
}

@Entity
private static class DummyEntity {
}

private static class ComplexObject {
private String stringVal;
private int intVal;
}
}