Skip to content

Fix raw document conversion in Collection like properties. #3704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3702-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3702-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3702-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3702-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2036,7 +2036,19 @@ public <S extends Object> S convert(Object source, TypeInformation<? extends S>
}

if (typeHint.isMap()) {
return (S) mapConverter.convert(this, (Bson) source, typeHint);

if(ClassUtils.isAssignable(Document.class, typeHint.getType())) {
return (S) documentConverter.convert(this, (Bson) source, typeHint);
}

if(source instanceof Bson) {
return (S) mapConverter.convert(this, (Bson) source, typeHint);
}
if(source instanceof Map) {
return (S) mapConverter.convert(this, new Document((Map<String,Object>) source), typeHint);
}

throw new IllegalArgumentException(String.format("Expected map like structure but found %s", source.getClass()));
}

if (source instanceof DBRef) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2568,6 +2568,38 @@ void readsMapContainingNullValue() {
.containsEntry("item3", "i3");
}

@Test // GH-3702
void readsRawDocument() {

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)));

WithRawDocumentProperties target = converter.read(WithRawDocumentProperties.class, source);

assertThat(target.raw).isInstanceOf(org.bson.Document.class).isEqualTo( new org.bson.Document("simple", 1).append("document", new org.bson.Document("inner-doc", 1)));
}

@Test // GH-3702
void readsListOfRawDocument() {

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))));

WithRawDocumentProperties target = converter.read(WithRawDocumentProperties.class, source);

assertThat(target.listOfRaw)
.containsExactly(new org.bson.Document("simple", 1).append("document", new org.bson.Document("inner-doc", 1)));
}

@Test // GH-3692
void readsMapThatDoesNotComeAsDocument() {

org.bson.Document source = new org.bson.Document("_id", "id-1").append("mapOfObjects", Collections.singletonMap("simple", 1));

ClassWithMapProperty target = converter.read(ClassWithMapProperty.class, source);

assertThat(target.mapOfObjects).containsEntry("simple",1);

}

static class GenericType<T> {
T content;
}
Expand Down Expand Up @@ -3233,4 +3265,11 @@ static class WithFieldWrite {
write = org.springframework.data.mongodb.core.mapping.Field.Write.ALWAYS) Person writeAlwaysPerson;

}

static class WithRawDocumentProperties {

String id;
org.bson.Document raw;
List<org.bson.Document> listOfRaw;
}
}