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
This commit is contained in:
Christoph Strobl
2025-01-16 08:20:41 +01:00
committed by Mark Paluch
parent 14985a9523
commit 0beff08000
2 changed files with 22 additions and 1 deletions

View File

@@ -134,7 +134,7 @@ public class MappingMongoConverter extends AbstractMongoConverter
private static final BiPredicate<MongoPersistentEntity<?>, MongoPersistentProperty> PROPERTY_FILTER = (e,
property) -> {
if (property.isIdProperty()) {
if (e.isIdProperty(property)) {
return false;
}

View File

@@ -3313,6 +3313,21 @@ class MappingMongoConverterUnitTests {
.extracting("id").isEqualTo(idValue);
}
@Test // GH-4877
void shouldReadNonIdFieldCalledIdFromSource() {
WithRenamedIdPropertyAndAnotherPropertyNamedId source = new WithRenamedIdPropertyAndAnotherPropertyNamedId();
source.abc = "actual-id-value";
source.id = "just-a-field";
org.bson.Document document = write(source);
assertThat(document).containsEntry("_id", source.abc).containsEntry("id", source.id);
WithRenamedIdPropertyAndAnotherPropertyNamedId target = converter.read(WithRenamedIdPropertyAndAnotherPropertyNamedId.class, document);
assertThat(target.abc).isEqualTo(source.abc);
assertThat(target.id).isEqualTo(source.id);
}
org.bson.Document write(Object source) {
org.bson.Document target = new org.bson.Document();
@@ -4531,4 +4546,10 @@ class MappingMongoConverterUnitTests {
}
}
static class WithRenamedIdPropertyAndAnotherPropertyNamedId {
@Id String abc;
String id;
}
}