|
|
|
|
@ -49,7 +49,9 @@ import org.springframework.core.convert.TypeDescriptor;
@@ -49,7 +49,9 @@ import org.springframework.core.convert.TypeDescriptor;
|
|
|
|
|
import org.springframework.core.convert.support.DefaultConversionService; |
|
|
|
|
import org.springframework.data.annotation.Reference; |
|
|
|
|
import org.springframework.data.convert.CustomConversions; |
|
|
|
|
import org.springframework.data.convert.PropertyValueConverter; |
|
|
|
|
import org.springframework.data.convert.TypeMapper; |
|
|
|
|
import org.springframework.data.convert.ValueConversionContext; |
|
|
|
|
import org.springframework.data.mapping.Association; |
|
|
|
|
import org.springframework.data.mapping.InstanceCreatorMetadata; |
|
|
|
|
import org.springframework.data.mapping.MappingException; |
|
|
|
|
@ -164,12 +166,11 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
@@ -164,12 +166,11 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
|
|
|
|
this.idMapper = new QueryMapper(this); |
|
|
|
|
|
|
|
|
|
this.spELContext = new SpELContext(DocumentPropertyAccessor.INSTANCE); |
|
|
|
|
this.dbRefProxyHandler = new DefaultDbRefProxyHandler(spELContext, mappingContext, |
|
|
|
|
(prop, bson, evaluator, path) -> { |
|
|
|
|
this.dbRefProxyHandler = new DefaultDbRefProxyHandler(spELContext, mappingContext, (prop, bson, evaluator, path) -> { |
|
|
|
|
|
|
|
|
|
ConversionContext context = getConversionContext(path); |
|
|
|
|
return MappingMongoConverter.this.getValueInternal(context, prop, bson, evaluator); |
|
|
|
|
}); |
|
|
|
|
ConversionContext context = getConversionContext(path); |
|
|
|
|
return MappingMongoConverter.this.getValueInternal(context, prop, bson, evaluator); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
this.referenceLookupDelegate = new ReferenceLookupDelegate(mappingContext, spELContext); |
|
|
|
|
this.documentPointerFactory = new DocumentPointerFactory(conversionService, mappingContext); |
|
|
|
|
@ -880,7 +881,10 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
@@ -880,7 +881,10 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
|
|
|
|
Object value = accessor.getProperty(prop); |
|
|
|
|
|
|
|
|
|
if (value == null) { |
|
|
|
|
if (prop.writeNullValues()) { |
|
|
|
|
|
|
|
|
|
if (conversions.hasValueConverter(prop)) { |
|
|
|
|
dbObjectAccessor.put(prop, applyPropertyConversion(null, prop, accessor)); |
|
|
|
|
} else { |
|
|
|
|
dbObjectAccessor.put(prop, null); |
|
|
|
|
} |
|
|
|
|
} else if (!conversions.isSimpleType(value.getClass())) { |
|
|
|
|
@ -918,14 +922,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
@@ -918,14 +922,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
|
|
|
|
TypeInformation<?> type = prop.getTypeInformation(); |
|
|
|
|
|
|
|
|
|
if (conversions.hasValueConverter(prop)) { |
|
|
|
|
accessor.put(prop, conversions.getPropertyValueConversions().getValueConverter(prop).write(obj, |
|
|
|
|
new MongoConversionContext(new PropertyValueProvider<>() { |
|
|
|
|
@Nullable |
|
|
|
|
@Override |
|
|
|
|
public <T> T getPropertyValue(MongoPersistentProperty property) { |
|
|
|
|
return (T) persistentPropertyAccessor.getProperty(property); |
|
|
|
|
} |
|
|
|
|
}, prop, this, spELContext))); |
|
|
|
|
accessor.put(prop, applyPropertyConversion(obj, prop, persistentPropertyAccessor)); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -964,8 +961,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
@@ -964,8 +961,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
|
|
|
|
dbRefObj = proxy.toDBRef(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(obj !=null && conversions.hasCustomWriteTarget(obj.getClass())) { |
|
|
|
|
accessor.withCheckFieldMapping(true).put(prop, doConvert(obj, conversions.getCustomWriteTarget(obj.getClass()).get())); |
|
|
|
|
if (obj != null && conversions.hasCustomWriteTarget(obj.getClass())) { |
|
|
|
|
accessor.put(prop, doConvert(obj, conversions.getCustomWriteTarget(obj.getClass()).get())); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -1267,17 +1264,10 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
@@ -1267,17 +1264,10 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
|
|
|
|
private void writeSimpleInternal(@Nullable Object value, Bson bson, MongoPersistentProperty property, |
|
|
|
|
PersistentPropertyAccessor<?> persistentPropertyAccessor) { |
|
|
|
|
|
|
|
|
|
DocumentAccessor accessor = new DocumentAccessor(bson).withCheckFieldMapping(true); |
|
|
|
|
DocumentAccessor accessor = new DocumentAccessor(bson); |
|
|
|
|
|
|
|
|
|
if (conversions.hasValueConverter(property)) { |
|
|
|
|
accessor.put(property, conversions.getPropertyValueConversions().getValueConverter(property).write(value, |
|
|
|
|
new MongoConversionContext(new PropertyValueProvider<>() { |
|
|
|
|
@Nullable |
|
|
|
|
@Override |
|
|
|
|
public <T> T getPropertyValue(MongoPersistentProperty property) { |
|
|
|
|
return (T) persistentPropertyAccessor.getProperty(property); |
|
|
|
|
} |
|
|
|
|
}, property, this, spELContext))); |
|
|
|
|
accessor.put(property, applyPropertyConversion(value, property, persistentPropertyAccessor)); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -1285,6 +1275,23 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
@@ -1285,6 +1275,23 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
|
|
|
|
property.hasExplicitWriteTarget() ? property.getFieldType() : Object.class)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
private Object applyPropertyConversion(@Nullable Object value, MongoPersistentProperty property, |
|
|
|
|
PersistentPropertyAccessor<?> persistentPropertyAccessor) { |
|
|
|
|
MongoConversionContext context = new MongoConversionContext(new PropertyValueProvider<>() { |
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|
@Override |
|
|
|
|
public <T> T getPropertyValue(MongoPersistentProperty property) { |
|
|
|
|
return (T) persistentPropertyAccessor.getProperty(property); |
|
|
|
|
} |
|
|
|
|
}, property, this, spELContext); |
|
|
|
|
PropertyValueConverter<Object, Object, ValueConversionContext<MongoPersistentProperty>> valueConverter = conversions |
|
|
|
|
.getPropertyValueConversions().getValueConverter(property); |
|
|
|
|
return value != null ? valueConverter.write(value, context) : valueConverter.writeNull(context); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Checks whether we have a custom conversion registered for the given value into an arbitrary simple Mongo type. |
|
|
|
|
* Returns the converted value if so. If not, we perform special enum handling or simply return the value as is. |
|
|
|
|
@ -1925,14 +1932,18 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
@@ -1925,14 +1932,18 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
|
|
|
|
String expression = property.getSpelExpression(); |
|
|
|
|
Object value = expression != null ? evaluator.evaluate(expression) : accessor.get(property); |
|
|
|
|
|
|
|
|
|
if (value == null) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CustomConversions conversions = context.getCustomConversions(); |
|
|
|
|
if (conversions.hasValueConverter(property)) { |
|
|
|
|
return (T) conversions.getPropertyValueConversions().getValueConverter(property).read(value, |
|
|
|
|
new MongoConversionContext(this, property, context.getSourceConverter(), spELContext)); |
|
|
|
|
MongoConversionContext conversionContext = new MongoConversionContext(this, property, |
|
|
|
|
context.getSourceConverter(), spELContext); |
|
|
|
|
PropertyValueConverter<Object, Object, ValueConversionContext<MongoPersistentProperty>> valueConverter = conversions |
|
|
|
|
.getPropertyValueConversions().getValueConverter(property); |
|
|
|
|
return (T) (value != null ? valueConverter.read(value, conversionContext) |
|
|
|
|
: valueConverter.readNull(conversionContext)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (value == null) { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ConversionContext contextToUse = context.forProperty(property); |
|
|
|
|
|