mongodb/morphia — pull request #591 by pfuerholz (2014-03-18 09:25:42+00:00)
Title: Second fix for Issue 377

Thanks for recent fix on issue 377 (https://github.com/mongodb/morphia/pull/557). Unfortunately I found another corner case which remained undetected.
**Do mind that the Pull Request contains 1st fix which you already built into!** So you only have to take over the changes in Mapper.java and TestMapping.java. (Sorry but I am rather new to Git/GitHub.)

*Here more about this corner case:*

# Scenario:
Entity having a field annotated with @Serialized. Object to be saved has runtime type e.g. java.awt.Color and this field gets updated by means of an update operation.

# Effect:
When saving a whole entity the data type mapping occurs by means of Mapper#toDBObject. This case was fixed by the bug entry 377. When updating only one field the mapping occurs by means of Mapper#toMongoObject. In this case the Serialized-annotation is not respected due to following method:

private boolean isAssignable(final MappedField mf, final Object value) {
if (mf == null) {
return false;
}

return (mf.hasAnnotation(Reference.class) || mf.getType().isAssignableFrom(Key.class) || mf.getType().isAssignableFrom(DBRef.class)
|| isMultiValued(mf, value));
}

Issue was that

mf.getType().isAssignableFrom(Key.class)

returned true but shouldn't: getType() returned Serializable and expression got true. This is the case since Key implements Serializable.

# Fix:
Expression changed to

Key.class.isAssignableFrom(mf.getType())

(changed left and right). Now expression gets only true if mf.getType() is Key.class or a derivative of. (This is most probably was has been expected.)

Furthermore I checked all other isAssignableFrom-cases in Morphia. I found some more within the Mapper class which I (hopefully) improved. (Beyond the Mapper class I couldn't find any suspectable wrongly isAssignableFrom uses.)

# Test:
Extended the issue377/TestMapping-class.