@@ -141,7 +141,6 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
this::getWriteTarget);
|
||||
this.idMapper = new QueryMapper(this);
|
||||
|
||||
|
||||
this.spELContext = new SpELContext(DocumentPropertyAccessor.INSTANCE);
|
||||
this.dbRefProxyHandler = new DefaultDbRefProxyHandler(spELContext, mappingContext,
|
||||
(prop, bson, evaluator, path) -> {
|
||||
@@ -161,8 +160,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
|
||||
Assert.notNull(path, "ObjectPath must not be null");
|
||||
|
||||
return new ConversionContext(conversions, path, this::readDocument, this::readCollectionOrArray, this::readMap, this::readDBRef,
|
||||
this::getPotentiallyConvertedSimpleRead);
|
||||
return new ConversionContext(conversions, path, this::readDocument, this::readCollectionOrArray, this::readMap,
|
||||
this::readDBRef, this::getPotentiallyConvertedSimpleRead);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -376,8 +375,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
}
|
||||
|
||||
private <S> S populateProperties(ConversionContext context, MongoPersistentEntity<S> entity,
|
||||
DocumentAccessor documentAccessor,
|
||||
SpELExpressionEvaluator evaluator, S instance) {
|
||||
DocumentAccessor documentAccessor, SpELExpressionEvaluator evaluator, S instance) {
|
||||
|
||||
PersistentPropertyAccessor<S> accessor = new ConvertingPropertyAccessor<>(entity.getPropertyAccessor(instance),
|
||||
conversionService);
|
||||
@@ -423,8 +421,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
|
||||
@Nullable
|
||||
private Object readIdValue(ConversionContext context, SpELExpressionEvaluator evaluator,
|
||||
MongoPersistentProperty idProperty,
|
||||
Object rawId) {
|
||||
MongoPersistentProperty idProperty, Object rawId) {
|
||||
|
||||
String expression = idProperty.getSpelExpression();
|
||||
Object resolvedValue = expression != null ? evaluator.evaluate(expression) : rawId;
|
||||
@@ -434,8 +431,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
|
||||
private void readProperties(ConversionContext context, MongoPersistentEntity<?> entity,
|
||||
PersistentPropertyAccessor<?> accessor, DocumentAccessor documentAccessor,
|
||||
MongoDbPropertyValueProvider valueProvider,
|
||||
SpELExpressionEvaluator evaluator) {
|
||||
MongoDbPropertyValueProvider valueProvider, SpELExpressionEvaluator evaluator) {
|
||||
|
||||
DbRefResolverCallback callback = null;
|
||||
|
||||
@@ -505,8 +501,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
|
||||
@Nullable
|
||||
private Object readUnwrapped(ConversionContext context, DocumentAccessor documentAccessor,
|
||||
MongoPersistentProperty prop,
|
||||
MongoPersistentEntity<?> unwrappedEntity) {
|
||||
MongoPersistentProperty prop, MongoPersistentEntity<?> unwrappedEntity) {
|
||||
|
||||
if (prop.findAnnotation(Unwrapped.class).onEmpty().equals(OnEmpty.USE_EMPTY)) {
|
||||
return read(context, unwrappedEntity, (Document) documentAccessor.getDocument());
|
||||
@@ -1447,8 +1442,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
T target = null;
|
||||
if (document != null) {
|
||||
|
||||
maybeEmitEvent(
|
||||
new AfterLoadEvent<>(document, (Class<T>) type.getType(), collectionName));
|
||||
maybeEmitEvent(new AfterLoadEvent<>(document, (Class<T>) type.getType(), collectionName));
|
||||
target = (T) readDocument(context, document, type);
|
||||
}
|
||||
|
||||
@@ -1541,9 +1535,10 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
private <T extends Object> T doConvert(Object value, Class<? extends T> target, @Nullable Class<? extends T> fallback) {
|
||||
private <T extends Object> T doConvert(Object value, Class<? extends T> target,
|
||||
@Nullable Class<? extends T> fallback) {
|
||||
|
||||
if(conversionService.canConvert(value.getClass(), target) || fallback == null) {
|
||||
if (conversionService.canConvert(value.getClass(), target) || fallback == null) {
|
||||
return conversionService.convert(value, target);
|
||||
}
|
||||
return conversionService.convert(value, fallback);
|
||||
@@ -1861,9 +1856,10 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
private final ContainerValueConverter<DBRef> dbRefConverter;
|
||||
private final ValueConverter<Object> elementConverter;
|
||||
|
||||
ConversionContext(org.springframework.data.convert.CustomConversions customConversions, ObjectPath path, ContainerValueConverter<Bson> documentConverter,
|
||||
ContainerValueConverter<Collection<?>> collectionConverter, ContainerValueConverter<Bson> mapConverter,
|
||||
ContainerValueConverter<DBRef> dbRefConverter, ValueConverter<Object> elementConverter) {
|
||||
ConversionContext(org.springframework.data.convert.CustomConversions customConversions, ObjectPath path,
|
||||
ContainerValueConverter<Bson> documentConverter, ContainerValueConverter<Collection<?>> collectionConverter,
|
||||
ContainerValueConverter<Bson> mapConverter, ContainerValueConverter<DBRef> dbRefConverter,
|
||||
ValueConverter<Object> elementConverter) {
|
||||
|
||||
this.conversions = customConversions;
|
||||
this.path = path;
|
||||
@@ -1935,8 +1931,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
|
||||
Assert.notNull(currentPath, "ObjectPath must not be null");
|
||||
|
||||
return new ConversionContext(conversions, currentPath, documentConverter, collectionConverter, mapConverter, dbRefConverter,
|
||||
elementConverter);
|
||||
return new ConversionContext(conversions, currentPath, documentConverter, collectionConverter, mapConverter,
|
||||
dbRefConverter, elementConverter);
|
||||
}
|
||||
|
||||
public ObjectPath getPath() {
|
||||
|
||||
@@ -30,7 +30,6 @@ import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.bson.types.Code;
|
||||
import org.bson.types.Decimal128;
|
||||
import org.bson.types.ObjectId;
|
||||
@@ -42,6 +41,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.beans.ConversionNotSupportedException;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -529,7 +529,7 @@ class MappingMongoConverterUnitTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertsObjectsIfNecessary() {
|
||||
void convertsObjectsIfNecessary() {
|
||||
|
||||
ObjectId id = new ObjectId();
|
||||
assertThat(converter.convertToMongoType(id)).isEqualTo(id);
|
||||
@@ -2113,21 +2113,21 @@ class MappingMongoConverterUnitTests {
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2479
|
||||
public void entityCallbacksAreNotSetByDefault() {
|
||||
Assertions.assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isNull();
|
||||
void entityCallbacksAreNotSetByDefault() {
|
||||
assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isNull();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2479
|
||||
public void entityCallbacksShouldBeInitiatedOnSettingApplicationContext() {
|
||||
void entityCallbacksShouldBeInitiatedOnSettingApplicationContext() {
|
||||
|
||||
ApplicationContext ctx = new StaticApplicationContext();
|
||||
converter.setApplicationContext(ctx);
|
||||
|
||||
Assertions.assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isNotNull();
|
||||
assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isNotNull();
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2479
|
||||
public void setterForEntityCallbackOverridesContextInitializedOnes() {
|
||||
void setterForEntityCallbackOverridesContextInitializedOnes() {
|
||||
|
||||
ApplicationContext ctx = new StaticApplicationContext();
|
||||
converter.setApplicationContext(ctx);
|
||||
@@ -2135,11 +2135,11 @@ class MappingMongoConverterUnitTests {
|
||||
EntityCallbacks callbacks = EntityCallbacks.create();
|
||||
converter.setEntityCallbacks(callbacks);
|
||||
|
||||
Assertions.assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isSameAs(callbacks);
|
||||
assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isSameAs(callbacks);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2479
|
||||
public void setterForApplicationContextShouldNotOverrideAlreadySetEntityCallbacks() {
|
||||
void setterForApplicationContextShouldNotOverrideAlreadySetEntityCallbacks() {
|
||||
|
||||
EntityCallbacks callbacks = EntityCallbacks.create();
|
||||
ApplicationContext ctx = new StaticApplicationContext();
|
||||
@@ -2147,11 +2147,11 @@ class MappingMongoConverterUnitTests {
|
||||
converter.setEntityCallbacks(callbacks);
|
||||
converter.setApplicationContext(ctx);
|
||||
|
||||
Assertions.assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isSameAs(callbacks);
|
||||
assertThat(ReflectionTestUtils.getField(converter, "entityCallbacks")).isSameAs(callbacks);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2479
|
||||
public void resolveDBRefMapValueShouldInvokeCallbacks() {
|
||||
void resolveDBRefMapValueShouldInvokeCallbacks() {
|
||||
|
||||
AfterConvertCallback<Person> afterConvertCallback = spy(new ReturningAfterConvertCallback());
|
||||
converter.setEntityCallbacks(EntityCallbacks.create(afterConvertCallback));
|
||||
@@ -2168,7 +2168,7 @@ class MappingMongoConverterUnitTests {
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2300
|
||||
public void readAndConvertDBRefNestedByMapCorrectly() {
|
||||
void readAndConvertDBRefNestedByMapCorrectly() {
|
||||
|
||||
org.bson.Document cluster = new org.bson.Document("_id", 100L);
|
||||
DBRef dbRef = new DBRef("clusters", 100L);
|
||||
@@ -2434,7 +2434,6 @@ class MappingMongoConverterUnitTests {
|
||||
@Test // GH-3660
|
||||
void usesCustomConverterForMapTypesOnWrite() {
|
||||
|
||||
|
||||
converter = new MappingMongoConverter(resolver, mappingContext);
|
||||
converter.setCustomConversions(MongoCustomConversions.create(it -> {
|
||||
it.registerConverter(new TypeImplementingMapToDocumentConverter());
|
||||
@@ -2521,7 +2520,6 @@ class MappingMongoConverterUnitTests {
|
||||
assertThat(target.typeImplementingMap).isEqualTo(new TypeImplementingMap("one", 2));
|
||||
}
|
||||
|
||||
|
||||
static class GenericType<T> {
|
||||
T content;
|
||||
}
|
||||
@@ -3098,7 +3096,7 @@ class MappingMongoConverterUnitTests {
|
||||
String val1;
|
||||
int val2;
|
||||
|
||||
public TypeImplementingMap(String val1, int val2) {
|
||||
TypeImplementingMap(String val1, int val2) {
|
||||
this.val1 = val1;
|
||||
this.val2 = val2;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user