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:
Oliver Gierke
2018-08-22 11:14:19 +02:00
parent a54ab6acea
commit e52b5d4b91
2 changed files with 34 additions and 0 deletions

View File

@@ -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;
}

View File

@@ -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";
}
}
}