Browse Source

DATAMONGO-2193 - Fix String <> ObjectId conversion for non-Id properties.

We now make sure to only convert valid ObjectId Strings if the property can be considered as id property.

Original pull request: #640.
pull/666/head
Christoph Strobl 7 years ago committed by Mark Paluch
parent
commit
0f7fc7880b
  1. 7
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java
  2. 31
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java
  3. 12
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

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

@ -907,8 +907,11 @@ public class QueryMapper { @@ -907,8 +907,11 @@ public class QueryMapper {
@Override
public boolean isIdField() {
MongoPersistentProperty idProperty = (property != null && property.isIdProperty()) ? property
: entity.getIdProperty();
if(property != null) {
return property.isIdProperty();
}
MongoPersistentProperty idProperty = entity.getIdProperty();
if (idProperty != null) {

31
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

@ -234,6 +234,7 @@ public class MongoTemplateTests { @@ -234,6 +234,7 @@ public class MongoTemplateTests {
template.dropCollection(WithGeoJson.class);
template.dropCollection(DocumentWithNestedTypeHavingStringIdProperty.class);
template.dropCollection(ImmutableAudited.class);
template.dropCollection(Outer.class);
}
@Test
@ -3663,6 +3664,24 @@ public class MongoTemplateTests { @@ -3663,6 +3664,24 @@ public class MongoTemplateTests {
assertThat(read.modified).isEqualTo(result.modified).describedAs("Expected auditing information to be read!");
}
@Test // DATAMONGO-2193
public void shouldNotConvertStringToObjectIdForNonIdField() {
ObjectId outerId = new ObjectId();
String innerId = new ObjectId().toHexString();
org.bson.Document source = new org.bson.Document() //
.append("_id", outerId) //
.append("inner", new org.bson.Document("id", innerId).append("value", "boooh"));
template.getDb().getCollection(template.getCollectionName(Outer.class)).insertOne(source);
Outer target = template.findOne(query(where("inner.id").is(innerId)), Outer.class);
assertThat(target).isNotNull();
assertThat(target.id).isEqualTo(outerId);
assertThat(target.inner.id).isEqualTo(innerId);
}
private AtomicReference<ImmutableVersioned> createAfterSaveReference() {
AtomicReference<ImmutableVersioned> saved = new AtomicReference<>();
@ -4178,4 +4197,16 @@ public class MongoTemplateTests { @@ -4178,4 +4197,16 @@ public class MongoTemplateTests {
@Id String id;
@LastModifiedDate Instant modified;
}
static class Outer {
@Id ObjectId id;
Inner inner;
}
static class Inner {
@Field("id") String id;
String value;
}
}

12
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

@ -819,6 +819,18 @@ public class QueryMapperUnitTests { @@ -819,6 +819,18 @@ public class QueryMapperUnitTests {
assertThat(mappedObject).containsEntry("className", "foo");
}
@Test // DATAMONGO-2193
public void shouldNotConvertHexStringToObjectIdForRenamedNestedIdField() {
String idHex = new ObjectId().toHexString();
Query query = new Query(where("nested.id").is(idHex));
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
context.getPersistentEntity(RootForClassWithExplicitlyRenamedIdField.class));
assertThat(document).isEqualTo(new org.bson.Document("nested.id", idHex));
}
@Document
public class Foo {
@Id private ObjectId id;

Loading…
Cancel
Save