Skip to content

Commit 1eab66a

Browse files
mp911deodrotbohm
authored andcommittedJul 12, 2018
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.
1 parent 8cc4ef3 commit 1eab66a

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed
 

‎spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ private <S extends Object> S read(final MongoPersistentEntity<S> entity, final D
296296
MappingMongoConverter.this);
297297
readProperties(entity, accessor, idProperty, documentAccessor, valueProvider, callback);
298298

299-
return instance;
299+
return (S) accessor.getBean();
300300
}
301301

302302
private Object readIdValue(ObjectPath path, DefaultSpELExpressionEvaluator evaluator,

‎spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,7 @@
3232
import java.math.BigInteger;
3333
import java.net.URL;
3434
import java.time.LocalDateTime;
35-
import java.util.ArrayList;
36-
import java.util.Arrays;
37-
import java.util.Collection;
38-
import java.util.Collections;
39-
import java.util.Date;
40-
import java.util.EnumMap;
41-
import java.util.EnumSet;
42-
import java.util.HashMap;
43-
import java.util.LinkedHashMap;
44-
import java.util.List;
45-
import java.util.Locale;
46-
import java.util.Map;
47-
import java.util.Optional;
48-
import java.util.Set;
49-
import java.util.SortedMap;
50-
import java.util.TreeMap;
35+
import java.util.*;
5136

5237
import org.bson.types.ObjectId;
5338
import org.hamcrest.Matcher;
@@ -1886,6 +1871,17 @@ public void readsNestedArraysCorrectly() {
18861871
assertThat(result.nestedFloats).isEqualTo(new float[][][] { { { 1.0f, 2.0f } } });
18871872
}
18881873

1874+
@Test // DATAMONGO-1992
1875+
public void readsImmutableObjectCorrectly() {
1876+
1877+
org.bson.Document document = new org.bson.Document("_id", "foo");
1878+
1879+
ImmutableObject result = converter.read(ImmutableObject.class, document);
1880+
1881+
assertThat(result.id).isEqualTo("foo");
1882+
assertThat(result.witherUsed).isTrue();
1883+
}
1884+
18891885
static class GenericType<T> {
18901886
T content;
18911887
}
@@ -2266,4 +2262,44 @@ static class DocWithInterfacedEnum {
22662262
static class WithNestedLists {
22672263
float[][][] nestedFloats;
22682264
}
2265+
2266+
static class ImmutableObject {
2267+
final String id;
2268+
final String name;
2269+
final boolean witherUsed;
2270+
2271+
private ImmutableObject(String id) {
2272+
this.id = id;
2273+
this.name = null;
2274+
this.witherUsed = false;
2275+
}
2276+
2277+
private ImmutableObject(String id, String name, boolean witherUsed) {
2278+
this.id = id;
2279+
this.name = name;
2280+
this.witherUsed = witherUsed;
2281+
}
2282+
2283+
public ImmutableObject() {
2284+
this.id = null;
2285+
this.name = null;
2286+
witherUsed = false;
2287+
}
2288+
2289+
public ImmutableObject withId(String id) {
2290+
return new ImmutableObject(id, name, true);
2291+
}
2292+
2293+
public String getId() {
2294+
return id;
2295+
}
2296+
2297+
public String getName() {
2298+
return name;
2299+
}
2300+
2301+
public boolean isWitherUsed() {
2302+
return witherUsed;
2303+
}
2304+
}
22692305
}

0 commit comments

Comments
 (0)
Please sign in to comment.