Skip to content

Commit 0beff08

Browse files
christophstroblmp911de
authored andcommitted
Fix reading back id field value when multiple id property candidates exist.
We now check if a property identifies as the entities id property when populating values read from the source document. Original pull request: #4878 Closes #4877
1 parent 14985a9 commit 0beff08

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public class MappingMongoConverter extends AbstractMongoConverter
134134
private static final BiPredicate<MongoPersistentEntity<?>, MongoPersistentProperty> PROPERTY_FILTER = (e,
135135
property) -> {
136136

137-
if (property.isIdProperty()) {
137+
if (e.isIdProperty(property)) {
138138
return false;
139139
}
140140

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

+21
Original file line numberDiff line numberDiff line change
@@ -3313,6 +3313,21 @@ void projectShouldReadComplexIdType(Class<?> projectionTargetType) {
33133313
.extracting("id").isEqualTo(idValue);
33143314
}
33153315

3316+
@Test // GH-4877
3317+
void shouldReadNonIdFieldCalledIdFromSource() {
3318+
3319+
WithRenamedIdPropertyAndAnotherPropertyNamedId source = new WithRenamedIdPropertyAndAnotherPropertyNamedId();
3320+
source.abc = "actual-id-value";
3321+
source.id = "just-a-field";
3322+
3323+
org.bson.Document document = write(source);
3324+
assertThat(document).containsEntry("_id", source.abc).containsEntry("id", source.id);
3325+
3326+
WithRenamedIdPropertyAndAnotherPropertyNamedId target = converter.read(WithRenamedIdPropertyAndAnotherPropertyNamedId.class, document);
3327+
assertThat(target.abc).isEqualTo(source.abc);
3328+
assertThat(target.id).isEqualTo(source.id);
3329+
}
3330+
33163331
org.bson.Document write(Object source) {
33173332

33183333
org.bson.Document target = new org.bson.Document();
@@ -4531,4 +4546,10 @@ public DoubleHolderDto(DoubleHolder number) {
45314546
}
45324547
}
45334548

4549+
static class WithRenamedIdPropertyAndAnotherPropertyNamedId {
4550+
4551+
@Id String abc;
4552+
String id;
4553+
}
4554+
45344555
}

0 commit comments

Comments
 (0)