Skip to content

Commit f4c9cdc

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 cd8402f commit f4c9cdc

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
@@ -931,8 +931,11 @@ public MetadataBackedField with(String name) {
931931
@Override
932932
public boolean isIdField() {
933933

934-
MongoPersistentProperty idProperty = (property != null && property.isIdProperty()) ? property
935-
: entity.getIdProperty();
934+
if(property != null) {
935+
return property.isIdProperty();
936+
}
937+
938+
MongoPersistentProperty idProperty = entity.getIdProperty();
936939

937940
if (idProperty != null) {
938941

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
@@ -238,6 +238,7 @@ protected void cleanDb() {
238238
template.dropCollection(DocumentWithNestedTypeHavingStringIdProperty.class);
239239
template.dropCollection(ImmutableAudited.class);
240240
template.dropCollection(RawStringId.class);
241+
template.dropCollection(Outer.class);
241242
}
242243

243244
@Test
@@ -3687,6 +3688,24 @@ public void saveAndLoadStringThatIsAnObjectIdAsString() {
36873688
assertThat(target).isEqualTo(source);
36883689
}
36893690

3691+
@Test // DATAMONGO-2193
3692+
public void shouldNotConvertStringToObjectIdForNonIdField() {
3693+
3694+
ObjectId outerId = new ObjectId();
3695+
String innerId = new ObjectId().toHexString();
3696+
3697+
org.bson.Document source = new org.bson.Document() //
3698+
.append("_id", outerId) //
3699+
.append("inner", new org.bson.Document("id", innerId).append("value", "boooh"));
3700+
3701+
template.getDb().getCollection(template.getCollectionName(Outer.class)).insertOne(source);
3702+
3703+
Outer target = template.findOne(query(where("inner.id").is(innerId)), Outer.class);
3704+
assertThat(target).isNotNull();
3705+
assertThat(target.id).isEqualTo(outerId);
3706+
assertThat(target.inner.id).isEqualTo(innerId);
3707+
}
3708+
36903709
private AtomicReference<ImmutableVersioned> createAfterSaveReference() {
36913710

36923711
AtomicReference<ImmutableVersioned> saved = new AtomicReference<>();
@@ -4209,4 +4228,16 @@ static class RawStringId {
42094228
@MongoId String id;
42104229
String value;
42114230
}
4231+
4232+
static class Outer {
4233+
4234+
@Id ObjectId id;
4235+
Inner inner;
4236+
}
4237+
4238+
static class Inner {
4239+
4240+
@Field("id") String id;
4241+
String value;
4242+
}
42124243
}

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)