DATAMONGO-679 - Fixed saving plain JSON strings in MongoTemplate.

MongoTemplate.doSave(…) didn't handle plain JSON strings correctly. It now correctly passes the marshaled object to the underlying method.
This commit is contained in:
Oliver Gierke
2013-05-23 20:40:05 +02:00
parent 23b276745c
commit 6b35ca80d4
2 changed files with 16 additions and 2 deletions

View File

@@ -798,7 +798,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
}
}
@SuppressWarnings("unchecked")
protected <T> void doSave(String collectionName, T objectToSave, MongoWriter<T> writer) {
assertUpdateableIdIfNotSet(objectToSave);
@@ -811,7 +810,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
writer.write(objectToSave, dbDoc);
} else {
try {
objectToSave = (T) JSON.parse((String) objectToSave);
dbDoc = (DBObject) JSON.parse((String) objectToSave);
} catch (JSONParseException e) {
throw new MappingException("Could not parse given String to save into a JSON document!", e);
}

View File

@@ -1597,6 +1597,21 @@ public class MongoTemplateTests {
}
}
/**
* @see DATAMONGO-679
*/
@Test
public void savesJsonStringCorrectly() {
DBObject dbObject = new BasicDBObject().append("first", "first").append("second", "second");
template.save(dbObject.toString(), "collection");
List<DBObject> result = template.findAll(DBObject.class, "collection");
assertThat(result.size(), is(1));
assertThat(result.get(0).containsField("first"), is(true));
}
static class MyId {
String first;