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.
This commit is contained in:
Christoph Strobl
2019-01-30 09:49:48 +01:00
committed by Mark Paluch
parent 6e42f49b08
commit 0f7fc7880b
3 changed files with 48 additions and 2 deletions

View File

@@ -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) {

View File

@@ -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 {
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 {
@Id String id;
@LastModifiedDate Instant modified;
}
static class Outer {
@Id ObjectId id;
Inner inner;
}
static class Inner {
@Field("id") String id;
String value;
}
}

View File

@@ -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;