Skip to content

Commit ab1c0ff

Browse files
christophstroblmp911de
authored andcommitted
Apply conversion on document reference lookup using nested property.
Closes #4033 Original pull request: #4044.
1 parent ae2846c commit ab1c0ff

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

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

+20-4
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ protected Document getMappedKeyword(Keyword keyword, @Nullable MongoPersistentEn
373373
if (keyword.isOrOrNor() || (keyword.hasIterableValue() && !keyword.isGeometry())) {
374374

375375
Iterable<?> conditions = keyword.getValue();
376-
List<Object> newConditions = conditions instanceof Collection ? new ArrayList<>(((Collection<?>) conditions).size()) : new ArrayList<>();
376+
List<Object> newConditions = conditions instanceof Collection
377+
? new ArrayList<>(((Collection<?>) conditions).size())
378+
: new ArrayList<>();
377379

378380
for (Object condition : conditions) {
379381
newConditions.add(isDocument(condition) ? getMappedObject((Document) condition, entity)
@@ -431,8 +433,10 @@ protected Object getMappedValue(Field documentField, Object sourceValue) {
431433

432434
Object value = applyFieldTargetTypeHintToValue(documentField, sourceValue);
433435

434-
if(documentField.getProperty() != null && converter.getCustomConversions().hasValueConverter(documentField.getProperty())) {
435-
return converter.getCustomConversions().getPropertyValueConversions().getValueConverter(documentField.getProperty())
436+
if (documentField.getProperty() != null
437+
&& converter.getCustomConversions().hasValueConverter(documentField.getProperty())) {
438+
return converter.getCustomConversions().getPropertyValueConversions()
439+
.getValueConverter(documentField.getProperty())
436440
.write(value, new MongoConversionContext(documentField.getProperty(), converter));
437441
}
438442

@@ -616,7 +620,11 @@ protected Object delegateConvertToMongoType(Object source, @Nullable MongoPersis
616620
}
617621

618622
protected Object convertAssociation(Object source, Field field) {
619-
return convertAssociation(source, field.getProperty());
623+
Object value = convertAssociation(source, field.getProperty());
624+
if (value != null && field.isIdField() && field.getFieldType() != value.getClass()) {
625+
return convertId(value, field.getFieldType());
626+
}
627+
return value;
620628
}
621629

622630
/**
@@ -1042,6 +1050,9 @@ public TypeInformation<?> getTypeHint() {
10421050
return TypeInformation.OBJECT;
10431051
}
10441052

1053+
public Class<?> getFieldType() {
1054+
return Object.class;
1055+
}
10451056
}
10461057

10471058
/**
@@ -1170,6 +1181,11 @@ private Association<MongoPersistentProperty> findAssociation() {
11701181
return null;
11711182
}
11721183

1184+
@Override
1185+
public Class<?> getFieldType() {
1186+
return property.getFieldType();
1187+
}
1188+
11731189
@Override
11741190
public String getMappedKey() {
11751191
return path == null ? name : path.toDotPath(isAssociation() ? getAssociationConverter() : getPropertyConverter());

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/QueryMapperUnitTests.java

+25
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,31 @@ void convertsDocumentReferenceOnIdPropertyCorrectly() {
437437
assertThat(mappedQuery).containsEntry("sample", "s1");
438438
}
439439

440+
@Test // GH-4033
441+
void convertsNestedPathToIdPropertyOfDocumentReferenceCorrectly() {
442+
443+
Query query = query(where("sample.foo").is("s1"));
444+
org.bson.Document mappedQuery = mapper.getMappedObject(query.getQueryObject(),
445+
context.getPersistentEntity(WithDocumentReference.class));
446+
447+
assertThat(mappedQuery).containsEntry("sample", "s1");
448+
}
449+
450+
@Test // GH-4033
451+
void convertsNestedPathToIdPropertyOfDocumentReferenceCorrectlyWhenItShouldBeConvertedToObjectId() {
452+
453+
ObjectId id = new ObjectId();
454+
Query query = query(where("sample.foo").is(id.toHexString()));
455+
org.bson.Document mappedQuery = mapper.getMappedObject(query.getQueryObject(),
456+
context.getPersistentEntity(WithDocumentReference.class));
457+
458+
assertThat(mappedQuery.get("sample")).satisfies(it -> {
459+
460+
assertThat(it).isInstanceOf(ObjectId.class);
461+
assertThat(((ObjectId) it).toHexString()).isEqualTo(id.toHexString());
462+
});
463+
}
464+
440465
@Test // GH-3853
441466
void convertsListDocumentReferenceOnIdPropertyCorrectly() {
442467

0 commit comments

Comments
 (0)