Skip to content

Commit 04daef2

Browse files
switch conversion from property to type information
1 parent dd65bf0 commit 04daef2

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@
6363
import org.springframework.data.mapping.model.SpELExpressionParameterValueProvider;
6464
import org.springframework.data.mongodb.CodecRegistryProvider;
6565
import org.springframework.data.mongodb.MongoDatabaseFactory;
66+
import org.springframework.data.mongodb.core.mapping.DocumentPointer;
6667
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
6768
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
68-
import org.springframework.data.mongodb.core.mapping.DocumentPointer;
6969
import org.springframework.data.mongodb.core.mapping.Unwrapped;
7070
import org.springframework.data.mongodb.core.mapping.Unwrapped.OnEmpty;
7171
import org.springframework.data.mongodb.core.mapping.event.AfterConvertCallback;
@@ -364,7 +364,7 @@ private <S extends Object> S read(ConversionContext context, MongoPersistentEnti
364364
SpELExpressionEvaluator evaluator = new DefaultSpELExpressionEvaluator(bson, spELContext);
365365
DocumentAccessor documentAccessor = new DocumentAccessor(bson);
366366

367-
if(bson.get("_id") != null) {
367+
if (bson.get("_id") != null) {
368368

369369
Object existing = context.getPath().getPathItem(bson.get("_id"), entity.getCollection(), entity.getType());
370370
if (existing != null) {
@@ -458,7 +458,8 @@ private void readProperties(ConversionContext context, MongoPersistentEntity<?>
458458
callback = getDbRefResolverCallback(context, documentAccessor, evaluator);
459459
}
460460

461-
readAssociation(prop.getRequiredAssociation(), accessor, documentAccessor, dbRefProxyHandler, callback, context, evaluator);
461+
readAssociation(prop.getRequiredAssociation(), accessor, documentAccessor, dbRefProxyHandler, callback, context,
462+
evaluator);
462463
continue;
463464
}
464465

@@ -485,7 +486,8 @@ private void readProperties(ConversionContext context, MongoPersistentEntity<?>
485486
callback = getDbRefResolverCallback(context, documentAccessor, evaluator);
486487
}
487488

488-
readAssociation(prop.getRequiredAssociation(), accessor, documentAccessor, dbRefProxyHandler, callback, context, evaluator);
489+
readAssociation(prop.getRequiredAssociation(), accessor, documentAccessor, dbRefProxyHandler, callback, context,
490+
evaluator);
489491
continue;
490492
}
491493

@@ -501,7 +503,8 @@ private DbRefResolverCallback getDbRefResolverCallback(ConversionContext context
501503
}
502504

503505
private void readAssociation(Association<MongoPersistentProperty> association, PersistentPropertyAccessor<?> accessor,
504-
DocumentAccessor documentAccessor, DbRefProxyHandler handler, DbRefResolverCallback callback, ConversionContext context, SpELExpressionEvaluator evaluator) {
506+
DocumentAccessor documentAccessor, DbRefProxyHandler handler, DbRefResolverCallback callback,
507+
ConversionContext context, SpELExpressionEvaluator evaluator) {
505508

506509
MongoPersistentProperty property = association.getInverse();
507510
final Object value = documentAccessor.get(property);
@@ -524,13 +527,16 @@ public Object getPointer() {
524527
}
525528
}, property.getActualType()));
526529
} else {
527-
accessor.setProperty(property, dbRefResolver.resolveReference(property, value, referenceReader,
528-
(document, prop) -> context.convert(document, prop.getTypeInformation())));
530+
accessor.setProperty(property,
531+
dbRefResolver.resolveReference(property, value, referenceReader, context::convert));
529532
}
530533
return;
531534
}
532535

533536
DBRef dbref = value instanceof DBRef ? (DBRef) value : null;
537+
538+
// TODO: accessor.setProperty(property, dbRefResolver.resolveReference(property, value, referenceReader,
539+
// context::convert));
534540
accessor.setProperty(property, dbRefResolver.resolveDbRef(property, dbref, callback, handler));
535541
}
536542

@@ -863,15 +869,15 @@ protected Bson createMap(Map<Object, Object> map, MongoPersistentProperty proper
863869
if (conversions.isSimpleType(key.getClass())) {
864870

865871
String simpleKey = prepareMapKey(key.toString());
866-
if(property.isDbReference()) {
872+
if (property.isDbReference()) {
867873
document.put(simpleKey, value != null ? createDBRef(value, property) : null);
868874
} else {
869875
if (conversionService.canConvert(value.getClass(), DocumentPointer.class)) {
870876
document.put(simpleKey, conversionService.convert(value, DocumentPointer.class).getPointer());
871877
} else {
872878
// just take the id as a reference
873-
document.put(simpleKey, mappingContext.getPersistentEntity(property.getAssociationTargetType()).getIdentifierAccessor(value)
874-
.getIdentifier());
879+
document.put(simpleKey, mappingContext.getPersistentEntity(property.getAssociationTargetType())
880+
.getIdentifierAccessor(value).getIdentifier());
875881
}
876882
}
877883

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReferenceReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,10 @@ Object readReference(MongoPersistentProperty property, Object value, LookupFunct
9090
}
9191

9292
if (property.isCollectionLike()) {
93-
return resultConversionFunction.apply(result, property);
93+
return resultConversionFunction.apply(result, property.getTypeInformation());
9494
}
9595

96-
return resultConversionFunction.apply(result.iterator().next(), property);
96+
return resultConversionFunction.apply(result.iterator().next(), property.getTypeInformation());
9797
}
9898

9999
private ReferenceCollection computeReferenceContext(MongoPersistentProperty property, Object value,

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/ReferenceResolver.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.bson.Document;
2121
import org.springframework.data.mongodb.core.convert.ReferenceLoader.DocumentReferenceQuery;
2222
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
23+
import org.springframework.data.util.TypeInformation;
2324
import org.springframework.lang.Nullable;
2425

2526
import com.mongodb.DBRef;
@@ -82,6 +83,6 @@ interface LookupFunction {
8283

8384
@FunctionalInterface
8485
interface ResultConversionFunction {
85-
Object apply(Object source, MongoPersistentProperty property);
86+
Object apply(Object source, TypeInformation property);
8687
}
8788
}

0 commit comments

Comments
 (0)