Skip to content

Commit 3f27e8e

Browse files
christophstroblmp911de
authored andcommitted
Fix raw document conversion in Collection like properties.
Along the lines make sure to convert map like structures correctly if they do not come as a Document, eg. cause they got converted to a plain Map in a post load, pre convert event. Closes #3702 Original pull request: #3704.
1 parent 23177fe commit 3f27e8e

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -2040,7 +2040,19 @@ public <S extends Object> S convert(Object source, TypeInformation<? extends S>
20402040
}
20412041

20422042
if (typeHint.isMap()) {
2043-
return (S) mapConverter.convert(this, (Bson) source, typeHint);
2043+
2044+
if(ClassUtils.isAssignable(Document.class, typeHint.getType())) {
2045+
return (S) documentConverter.convert(this, (Bson) source, typeHint);
2046+
}
2047+
2048+
if(source instanceof Bson) {
2049+
return (S) mapConverter.convert(this, (Bson) source, typeHint);
2050+
}
2051+
if(source instanceof Map) {
2052+
return (S) mapConverter.convert(this, new Document((Map<String,Object>) source), typeHint);
2053+
}
2054+
2055+
throw new IllegalArgumentException(String.format("Expected map like structure but found %s", source.getClass()));
20442056
}
20452057

20462058
if (source instanceof DBRef) {

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

+39
Original file line numberDiff line numberDiff line change
@@ -2584,6 +2584,38 @@ void appliesCustomConverterEvenToSimpleTypes() {
25842584
assertThat(target.content).isInstanceOf(byte[].class);
25852585
}
25862586

2587+
@Test // GH-3702
2588+
void readsRawDocument() {
2589+
2590+
org.bson.Document source = new org.bson.Document("_id", "id-1").append("raw", new org.bson.Document("simple", 1).append("document", new org.bson.Document("inner-doc", 1)));
2591+
2592+
WithRawDocumentProperties target = converter.read(WithRawDocumentProperties.class, source);
2593+
2594+
assertThat(target.raw).isInstanceOf(org.bson.Document.class).isEqualTo( new org.bson.Document("simple", 1).append("document", new org.bson.Document("inner-doc", 1)));
2595+
}
2596+
2597+
@Test // GH-3702
2598+
void readsListOfRawDocument() {
2599+
2600+
org.bson.Document source = new org.bson.Document("_id", "id-1").append("listOfRaw", Arrays.asList(new org.bson.Document("simple", 1).append("document", new org.bson.Document("inner-doc", 1))));
2601+
2602+
WithRawDocumentProperties target = converter.read(WithRawDocumentProperties.class, source);
2603+
2604+
assertThat(target.listOfRaw)
2605+
.containsExactly(new org.bson.Document("simple", 1).append("document", new org.bson.Document("inner-doc", 1)));
2606+
}
2607+
2608+
@Test // GH-3692
2609+
void readsMapThatDoesNotComeAsDocument() {
2610+
2611+
org.bson.Document source = new org.bson.Document("_id", "id-1").append("mapOfObjects", Collections.singletonMap("simple", 1));
2612+
2613+
ClassWithMapProperty target = converter.read(ClassWithMapProperty.class, source);
2614+
2615+
assertThat(target.mapOfObjects).containsEntry("simple",1);
2616+
2617+
}
2618+
25872619
static class GenericType<T> {
25882620
T content;
25892621
}
@@ -3243,6 +3275,13 @@ public Set<Entry<String, String>> entrySet() {
32433275
}
32443276
}
32453277

3278+
static class WithRawDocumentProperties {
3279+
3280+
String id;
3281+
org.bson.Document raw;
3282+
List<org.bson.Document> listOfRaw;
3283+
}
3284+
32463285
static class WithFieldWrite {
32473286

32483287
@org.springframework.data.mongodb.core.mapping.Field(

0 commit comments

Comments
 (0)