Skip to content

Consider null value settings for types with custom conversion #4728

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 5 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>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4710-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>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4710-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 @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4710-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 @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.4.0-SNAPSHOT</version>
<version>4.4.x-GH-4710-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public void put(MongoPersistentProperty prop, @Nullable Object value) {

Assert.notNull(prop, "MongoPersistentProperty must not be null");

if (value == null && !prop.writeNullValues()) {
return;
}

Iterator<String> parts = Arrays.asList(prop.getMongoField().getName().parts()).iterator();
Bson document = this.document;

Expand Down Expand Up @@ -172,4 +176,5 @@ private static Document getOrCreateNestedDocument(String key, Bson source) {

return nested;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
import org.springframework.core.env.StandardEnvironment;
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;
Expand Down Expand Up @@ -176,12 +178,11 @@ public MappingMongoConverter(DbRefResolver dbRefResolver,
this.idMapper = new QueryMapper(this);

this.spELContext = new SpELContext(DocumentPropertyAccessor.INSTANCE);
this.dbRefProxyHandler = new DefaultDbRefProxyHandler(mappingContext,
(prop, bson, evaluator, path) -> {
this.dbRefProxyHandler = new DefaultDbRefProxyHandler(mappingContext, (prop, bson, evaluator, path) -> {

ConversionContext context = getConversionContext(path);
return MappingMongoConverter.this.getValueInternal(context, prop, bson, evaluator);
}, expressionEvaluatorFactory::create);
ConversionContext context = getConversionContext(path);
return MappingMongoConverter.this.getValueInternal(context, prop, bson, evaluator);
}, expressionEvaluatorFactory::create);

this.referenceLookupDelegate = new ReferenceLookupDelegate(mappingContext, spELContext);
this.documentPointerFactory = new DocumentPointerFactory(conversionService, mappingContext);
Expand Down Expand Up @@ -279,6 +280,7 @@ public void setCodecRegistryProvider(@Nullable CodecRegistryProvider codecRegist
this.codecRegistryProvider = codecRegistryProvider;
}

@Override
public MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> getMappingContext() {
return mappingContext;
}
Expand Down Expand Up @@ -431,6 +433,7 @@ public Map<String, Object> getBean() {
}
}

@Override
public <S extends Object> S read(Class<S> clazz, Bson bson) {
return read(TypeInformation.of(clazz), bson);
}
Expand Down Expand Up @@ -728,6 +731,7 @@ private Object readUnwrapped(ConversionContext context, DocumentAccessor documen
return null;
}

@Override
public DBRef toDBRef(Object object, @Nullable MongoPersistentProperty referringProperty) {

org.springframework.data.mongodb.core.mapping.DBRef annotation;
Expand Down Expand Up @@ -794,6 +798,7 @@ DocumentPointer<?> createDocumentPointer(Object source, @Nullable MongoPersisten
*
* @see org.springframework.data.mongodb.core.convert.MongoWriter#write(java.lang.Object, java.lang.Object)
*/
@Override
public void write(Object obj, Bson bson) {

if (null == obj) {
Expand Down Expand Up @@ -903,7 +908,10 @@ private void writeProperties(Bson bson, MongoPersistentEntity<?> entity, Persist
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())) {
Expand All @@ -929,6 +937,7 @@ private void writeAssociation(Association<MongoPersistentProperty> association,
writePropertyInternal(value, dbObjectAccessor, inverseProp, accessor);
}

// TODO: Documentaccessor is package-private
@SuppressWarnings({ "unchecked" })
protected void writePropertyInternal(@Nullable Object obj, DocumentAccessor accessor, MongoPersistentProperty prop,
PersistentPropertyAccessor<?> persistentPropertyAccessor) {
Expand All @@ -941,14 +950,7 @@ protected void writePropertyInternal(@Nullable Object obj, DocumentAccessor acce
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;
}

Expand Down Expand Up @@ -987,6 +989,11 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
dbRefObj = proxy.toDBRef();
}

if (obj != null && conversions.hasCustomWriteTarget(obj.getClass())) {
accessor.put(prop, doConvert(obj, conversions.getCustomWriteTarget(obj.getClass()).get()));
return;
}

dbRefObj = dbRefObj != null ? dbRefObj : createDBRef(obj, prop);

accessor.put(prop, dbRefObj);
Expand Down Expand Up @@ -1284,24 +1291,35 @@ private void writeSimpleInternal(@Nullable Object value, Bson bson, String key)

private void writeSimpleInternal(@Nullable Object value, Bson bson, MongoPersistentProperty property,
PersistentPropertyAccessor<?> persistentPropertyAccessor) {

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

accessor.put(property, getPotentiallyConvertedSimpleWrite(value,
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.
Expand Down Expand Up @@ -1597,7 +1615,7 @@ public Object convertToMongoType(@Nullable Object obj, @Nullable TypeInformation
}

@Override
public Object convertToMongoType(@Nullable Object obj, MongoPersistentEntity entity) {
public Object convertToMongoType(@Nullable Object obj, MongoPersistentEntity<?> entity) {
Document newDocument = new Document();
writeInternal(obj, newDocument, entity);
return newDocument;
Expand Down Expand Up @@ -1935,21 +1953,26 @@ static class MongoDbPropertyValueProvider implements PropertyValueProvider<Mongo
this.spELContext = spELContext;
}

@Override
@Nullable
@SuppressWarnings("unchecked")
public <T> T getPropertyValue(MongoPersistentProperty property) {

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);
Expand Down Expand Up @@ -2244,6 +2267,7 @@ default <S> S findContextualEntity(MongoPersistentEntity<S> entity, Document doc
return null;
}

// TODO: ObjectPath is package-private
ObjectPath getPath();

CustomConversions getCustomConversions();
Expand Down Expand Up @@ -2404,6 +2428,7 @@ public ConversionContext withPath(ObjectPath currentPath) {
collectionConverter, mapConverter, dbRefConverter, elementConverter);
}

// TODO: ObjectPath is package-private
@Override
public ObjectPath getPath() {
return path;
Expand Down
Loading