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:
committed by
Mark Paluch
parent
cd8402f4ba
commit
f4c9cdcacb
@@ -931,8 +931,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) {
|
||||
|
||||
|
||||
@@ -238,6 +238,7 @@ public class MongoTemplateTests {
|
||||
template.dropCollection(DocumentWithNestedTypeHavingStringIdProperty.class);
|
||||
template.dropCollection(ImmutableAudited.class);
|
||||
template.dropCollection(RawStringId.class);
|
||||
template.dropCollection(Outer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -3687,6 +3688,24 @@ public class MongoTemplateTests {
|
||||
assertThat(target).isEqualTo(source);
|
||||
}
|
||||
|
||||
@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<>();
|
||||
@@ -4209,4 +4228,16 @@ public class MongoTemplateTests {
|
||||
@MongoId String id;
|
||||
String value;
|
||||
}
|
||||
|
||||
static class Outer {
|
||||
|
||||
@Id ObjectId id;
|
||||
Inner inner;
|
||||
}
|
||||
|
||||
static class Inner {
|
||||
|
||||
@Field("id") String id;
|
||||
String value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user