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.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
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 {
|
||||
|
||||
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 {
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user