diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryMapper.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryMapper.java index cefb06821..62305d6e3 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryMapper.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/QueryMapper.java @@ -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); } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryMapperUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryMapperUnitTests.java index 37adfee42..a461f5339 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryMapperUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryMapperUnitTests.java @@ -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