Browse Source

DATAMONGO-466 - QueryMapper now only tries id conversion for top level document.

So far the QueryMapper has tried to map id properties of nested documents to ObjectIds which it shouldn't do actually.
1.0.x
Oliver Gierke 14 years ago
parent
commit
1be1297ef9
  1. 6
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryMapper.java
  2. 29
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryMapperUnitTests.java

6
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryMapper.java

@ -141,7 +141,11 @@ public class QueryMapper { @@ -141,7 +141,11 @@ public class QueryMapper {
*/
private boolean isIdKey(String key, MongoPersistentEntity<?> entity) {
if (null != entity && entity.getIdProperty() != null) {
if (entity == null) {
return false;
}
if (entity.getIdProperty() != null) {
MongoPersistentProperty idProperty = entity.getIdProperty();
return idProperty.getName().equals(key) || idProperty.getFieldName().equals(key);
}

29
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryMapperUnitTests.java

@ -83,7 +83,7 @@ public class QueryMapperUnitTests { @@ -83,7 +83,7 @@ public class QueryMapperUnitTests {
public void convertsStringIntoObjectId() {
DBObject query = new BasicDBObject("_id", new ObjectId().toString());
DBObject result = mapper.getMappedObject(query, null);
DBObject result = mapper.getMappedObject(query, context.getPersistentEntity(IdWrapper.class));
assertThat(result.get("_id"), is(instanceOf(ObjectId.class)));
}
@ -91,7 +91,7 @@ public class QueryMapperUnitTests { @@ -91,7 +91,7 @@ public class QueryMapperUnitTests {
public void handlesBigIntegerIdsCorrectly() {
DBObject dbObject = new BasicDBObject("id", new BigInteger("1"));
DBObject result = mapper.getMappedObject(dbObject, null);
DBObject result = mapper.getMappedObject(dbObject, context.getPersistentEntity(IdWrapper.class));
assertThat(result.get("_id"), is((Object) "1"));
}
@ -100,7 +100,7 @@ public class QueryMapperUnitTests { @@ -100,7 +100,7 @@ public class QueryMapperUnitTests {
ObjectId id = new ObjectId();
DBObject dbObject = new BasicDBObject("id", new BigInteger(id.toString(), 16));
DBObject result = mapper.getMappedObject(dbObject, null);
DBObject result = mapper.getMappedObject(dbObject, context.getPersistentEntity(IdWrapper.class));
assertThat(result.get("_id"), is((Object) id));
}
@ -198,6 +198,29 @@ public class QueryMapperUnitTests { @@ -198,6 +198,29 @@ public class QueryMapperUnitTests {
assertThat(result, is(query.getQueryObject()));
}
@Test
public void doesNotHandleNestedFieldsWithDefaultIdNames() {
BasicDBObject dbObject = new BasicDBObject("id", new ObjectId().toString());
dbObject.put("nested", new BasicDBObject("id", new ObjectId().toString()));
MongoPersistentEntity<?> entity = context.getPersistentEntity(ClassWithDefaultId.class);
DBObject result = mapper.getMappedObject(dbObject, entity);
assertThat(result.get("_id"), is(instanceOf(ObjectId.class)));
assertThat(((DBObject) result.get("nested")).get("id"), is(instanceOf(String.class)));
}
class IdWrapper {
Object id;
}
class ClassWithDefaultId {
String id;
ClassWithDefaultId nested;
}
class Sample {
@Id

Loading…
Cancel
Save