DATAMONGO-1992 - Add mutation support for immutable types.

We now return new instances that are potentially created by wither/Kotlin copy(…) methods when reading immutable properties.
This commit is contained in:
Mark Paluch
2018-06-04 11:01:58 +02:00
committed by Oliver Gierke
parent 8cc4ef3c3f
commit 1eab66aff4
2 changed files with 53 additions and 17 deletions

View File

@@ -296,7 +296,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
MappingMongoConverter.this);
readProperties(entity, accessor, idProperty, documentAccessor, valueProvider, callback);
return instance;
return (S) accessor.getBean();
}
private Object readIdValue(ObjectPath path, DefaultSpELExpressionEvaluator evaluator,

View File

@@ -32,22 +32,7 @@ import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.*;
import org.bson.types.ObjectId;
import org.hamcrest.Matcher;
@@ -1886,6 +1871,17 @@ public class MappingMongoConverterUnitTests {
assertThat(result.nestedFloats).isEqualTo(new float[][][] { { { 1.0f, 2.0f } } });
}
@Test // DATAMONGO-1992
public void readsImmutableObjectCorrectly() {
org.bson.Document document = new org.bson.Document("_id", "foo");
ImmutableObject result = converter.read(ImmutableObject.class, document);
assertThat(result.id).isEqualTo("foo");
assertThat(result.witherUsed).isTrue();
}
static class GenericType<T> {
T content;
}
@@ -2266,4 +2262,44 @@ public class MappingMongoConverterUnitTests {
static class WithNestedLists {
float[][][] nestedFloats;
}
static class ImmutableObject {
final String id;
final String name;
final boolean witherUsed;
private ImmutableObject(String id) {
this.id = id;
this.name = null;
this.witherUsed = false;
}
private ImmutableObject(String id, String name, boolean witherUsed) {
this.id = id;
this.name = name;
this.witherUsed = witherUsed;
}
public ImmutableObject() {
this.id = null;
this.name = null;
witherUsed = false;
}
public ImmutableObject withId(String id) {
return new ImmutableObject(id, name, true);
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public boolean isWitherUsed() {
return witherUsed;
}
}
}