Skip to content

Commit 0f7fc78

Browse files
christophstroblmp911de
authored andcommitted
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.
1 parent 6e42f49 commit 0f7fc78

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -907,8 +907,11 @@ public MetadataBackedField with(String name) {
907907
@Override
908908
public boolean isIdField() {
909909

910-
MongoPersistentProperty idProperty = (property != null && property.isIdProperty()) ? property
911-
: entity.getIdProperty();
910+
if(property != null) {
911+
return property.isIdProperty();
912+
}
913+
914+
MongoPersistentProperty idProperty = entity.getIdProperty();
912915

913916
if (idProperty != null) {
914917

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ protected void cleanDb() {
234234
template.dropCollection(WithGeoJson.class);
235235
template.dropCollection(DocumentWithNestedTypeHavingStringIdProperty.class);
236236
template.dropCollection(ImmutableAudited.class);
237+
template.dropCollection(Outer.class);
237238
}
238239

239240
@Test
@@ -3663,6 +3664,24 @@ public void writesAuditingMetadataForImmutableTypes() {
36633664
assertThat(read.modified).isEqualTo(result.modified).describedAs("Expected auditing information to be read!");
36643665
}
36653666

3667+
@Test // DATAMONGO-2193
3668+
public void shouldNotConvertStringToObjectIdForNonIdField() {
3669+
3670+
ObjectId outerId = new ObjectId();
3671+
String innerId = new ObjectId().toHexString();
3672+
3673+
org.bson.Document source = new org.bson.Document() //
3674+
.append("_id", outerId) //
3675+
.append("inner", new org.bson.Document("id", innerId).append("value", "boooh"));
3676+
3677+
template.getDb().getCollection(template.getCollectionName(Outer.class)).insertOne(source);
3678+
3679+
Outer target = template.findOne(query(where("inner.id").is(innerId)), Outer.class);
3680+
assertThat(target).isNotNull();
3681+
assertThat(target.id).isEqualTo(outerId);
3682+
assertThat(target.inner.id).isEqualTo(innerId);
3683+
}
3684+
36663685
private AtomicReference<ImmutableVersioned> createAfterSaveReference() {
36673686

36683687
AtomicReference<ImmutableVersioned> saved = new AtomicReference<>();
@@ -4178,4 +4197,16 @@ static class ImmutableAudited {
41784197
@Id String id;
41794198
@LastModifiedDate Instant modified;
41804199
}
4200+
4201+
static class Outer {
4202+
4203+
@Id ObjectId id;
4204+
Inner inner;
4205+
}
4206+
4207+
static class Inner {
4208+
4209+
@Field("id") String id;
4210+
String value;
4211+
}
41814212
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,18 @@ public void getMappedObjectShouldNotMapTypeHint() {
819819
assertThat(mappedObject).containsEntry("className", "foo");
820820
}
821821

822+
@Test // DATAMONGO-2193
823+
public void shouldNotConvertHexStringToObjectIdForRenamedNestedIdField() {
824+
825+
String idHex = new ObjectId().toHexString();
826+
Query query = new Query(where("nested.id").is(idHex));
827+
828+
org.bson.Document document = mapper.getMappedObject(query.getQueryObject(),
829+
context.getPersistentEntity(RootForClassWithExplicitlyRenamedIdField.class));
830+
831+
assertThat(document).isEqualTo(new org.bson.Document("nested.id", idHex));
832+
}
833+
822834
@Document
823835
public class Foo {
824836
@Id private ObjectId id;

0 commit comments

Comments
 (0)