Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Support DBObject and Map that as source for entity materialization and map conversion.

See #3702
Original pull request: #3704.
  • Loading branch information
mp911de committed Jul 15, 2021
1 parent 08c5e5a commit 595a346
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1909,14 +1909,11 @@ public <S extends Object> S convert(Object source, TypeInformation<? extends S>
if (typeHint.isMap()) {

if(ClassUtils.isAssignable(Document.class, typeHint.getType())) {
return (S) documentConverter.convert(this, (Bson) source, typeHint);
return (S) documentConverter.convert(this, BsonUtils.asBson(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);
if (BsonUtils.supportsBson(source)) {
return (S) mapConverter.convert(this, BsonUtils.asBson(source), typeHint);
}

throw new IllegalArgumentException(String.format("Expected map like structure but found %s", source.getClass()));
Expand All @@ -1931,8 +1928,8 @@ public <S extends Object> S convert(Object source, TypeInformation<? extends S>
String.format(INCOMPATIBLE_TYPES, source, BasicDBList.class, typeHint.getType(), getPath()));
}

if (source instanceof Bson) {
return (S) documentConverter.convert(this, (Bson) source, typeHint);
if (BsonUtils.supportsBson(source)) {
return (S) documentConverter.convert(this, BsonUtils.asBson(source), typeHint);
}

return (S) elementConverter.convert(source, typeHint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,49 @@ private static Map<String, Object> getAsMap(Object source) {
return null;
}

/**
* Returns the given source object as {@link Bson}, i.e. {@link Document}s and maps as is or throw
* {@link IllegalArgumentException}.
*
* @param source
* @return the converted/casted source object.
* @throws IllegalArgumentException if {@code source} cannot be converted/cast to {@link Bson}.
* @since 3.2.3
* @see #supportsBson(Object)
*/
@SuppressWarnings("unchecked")
public static Bson asBson(Object source) {

if (source instanceof Document) {
return (Document) source;
}

if (source instanceof BasicDBObject) {
return (BasicDBObject) source;
}

if (source instanceof DBObject) {
return new Document(((DBObject) source).toMap());
}

if (source instanceof Map) {
return new Document((Map<String, Object>) source);
}

throw new IllegalArgumentException(String.format("Cannot convert %s to Bson", source));
}

/**
* Returns the given source can be used/converted as {@link Bson}.
*
* @param source
* @return {@literal true} if the given source can be converted to {@link Bson}.
* @since 3.2.3
*/
public static boolean supportsBson(Object source) {
return source instanceof DBObject || source instanceof Map;
}

/**
* Returns given object as {@link Collection}. Will return the {@link Collection} as is if the source is a
* {@link Collection} already, will convert an array into a {@link Collection} or simply create a single element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,10 +934,11 @@ void convertsSetToBasicDBList() {
assertThat(readResult.iterator().next()).isInstanceOf(Address.class);
}

@Test // DATAMONGO-402
@Test // DATAMONGO-402, GH-3702
void readsMemberClassCorrectly() {

org.bson.Document document = new org.bson.Document("inner", new org.bson.Document("value", "FOO!"));
org.bson.Document document = new org.bson.Document("inner",
new LinkedHashMap<>(new org.bson.Document("value", "FOO!")));

Outer outer = converter.read(Outer.class, document);
assertThat(outer.inner).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Collections;

import org.bson.BsonDouble;
import org.bson.BsonInt32;
Expand All @@ -29,10 +29,16 @@
import org.bson.Document;
import org.bson.types.ObjectId;
import org.junit.jupiter.api.Test;

import org.springframework.data.mongodb.util.BsonUtils;

import com.mongodb.BasicDBList;

/**
* Unit tests for {@link BsonUtils}.
*
* @author Christoph Strobl
* @author Mark Paluch
*/
class BsonUtilsTest {

Expand Down Expand Up @@ -111,4 +117,13 @@ void asCollectionConvertsWrapsNonIterable() {

assertThat((Collection)BsonUtils.asCollection(source)).containsExactly(source);
}

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

assertThat(BsonUtils.supportsBson("foo")).isFalse();
assertThat(BsonUtils.supportsBson(new Document())).isTrue();
assertThat(BsonUtils.supportsBson(new BasicDBList())).isTrue();
assertThat(BsonUtils.supportsBson(Collections.emptyMap())).isTrue();
}
}

0 comments on commit 595a346

Please sign in to comment.