Skip to content

Commit

Permalink
Fix reading back id field value when multiple id property candidates …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
christophstrobl authored and mp911de committed Jan 16, 2025
1 parent 1014817 commit 1edbdcb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3313,6 +3313,21 @@ void projectShouldReadComplexIdType(Class<?> projectionTargetType) {
.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();
Expand Down Expand Up @@ -4531,4 +4546,10 @@ public DoubleHolderDto(DoubleHolder number) {
}
}

static class WithRenamedIdPropertyAndAnotherPropertyNamedId {

@Id String abc;
String id;
}

}

0 comments on commit 1edbdcb

Please sign in to comment.