DATAMONGO-2066 - Skip immutable fields on property population.
We now skip immutable fields without withers on property population as they by definition have to be populated though the constructor and cannot be mutated afterwards.
This commit is contained in:
@@ -363,6 +363,10 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
continue;
|
||||
}
|
||||
|
||||
if (prop.isImmutable() && prop.getWither() == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (entity.isConstructorArgument(prop) || !documentAccessor.hasValue(prop)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,9 @@ import static org.junit.Assert.fail;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.data.mongodb.core.DocumentTestUtils.*;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.experimental.Wither;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
@@ -1913,6 +1915,20 @@ public class MappingMongoConverterUnitTests {
|
||||
assertThat(target).doesNotContainKeys("_class");
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2066
|
||||
public void doesNotPopulateImmutableFieldEvenIfInDocument() {
|
||||
|
||||
org.bson.Document source = new org.bson.Document("first", "foo").append("second", "bar");
|
||||
|
||||
SampleWithImmutableField result = converter.read(SampleWithImmutableField.class, source);
|
||||
|
||||
// No wither, no change… think Bob Marley.
|
||||
assertThat(result.first).isEqualTo("first");
|
||||
|
||||
// Wither used
|
||||
assertThat(result.second).isEqualTo("bar");
|
||||
}
|
||||
|
||||
static class GenericType<T> {
|
||||
T content;
|
||||
}
|
||||
@@ -2341,4 +2357,18 @@ public class MappingMongoConverterUnitTests {
|
||||
final @Id String id;
|
||||
String value;
|
||||
}
|
||||
|
||||
// DATAMONGO-2066
|
||||
|
||||
@AllArgsConstructor
|
||||
static class SampleWithImmutableField {
|
||||
|
||||
final String first;
|
||||
final @Wither String second;
|
||||
|
||||
public SampleWithImmutableField() {
|
||||
this.first = "first";
|
||||
this.second = "second";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user