Skip to content

Commit bdc662e

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

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

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

+20-8
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,9 @@ protected Document getMappedKeyword(Keyword keyword, @Nullable MongoPersistentEn
376376
if (keyword.isOrOrNor() || (keyword.hasIterableValue() && !keyword.isGeometry())) {
377377

378378
Iterable<?> conditions = keyword.getValue();
379-
List<Object> newConditions = conditions instanceof Collection ? new ArrayList<>(((Collection<?>) conditions).size()) : new ArrayList<>();
379+
List<Object> newConditions = conditions instanceof Collection
380+
? new ArrayList<>(((Collection<?>) conditions).size())
381+
: new ArrayList<>();
380382

381383
for (Object condition : conditions) {
382384
newConditions.add(isDocument(condition) ? getMappedObject((Document) condition, entity)
@@ -434,8 +436,10 @@ protected Object getMappedValue(Field documentField, Object sourceValue) {
434436

435437
Object value = applyFieldTargetTypeHintToValue(documentField, sourceValue);
436438

437-
if(documentField.getProperty() != null && converter.getCustomConversions().getPropertyValueConversions().hasValueConverter(documentField.getProperty())) {
438-
return converter.getCustomConversions().getPropertyValueConversions().getValueConverter(documentField.getProperty())
439+
if (documentField.getProperty() != null
440+
&& converter.getCustomConversions().getPropertyValueConversions().hasValueConverter(documentField.getProperty())) {
441+
return converter.getCustomConversions().getPropertyValueConversions()
442+
.getValueConverter(documentField.getProperty())
439443
.write(value, new MongoConversionContext(documentField.getProperty(), converter));
440444
}
441445

@@ -619,7 +623,11 @@ protected Object delegateConvertToMongoType(Object source, @Nullable MongoPersis
619623
}
620624

621625
protected Object convertAssociation(Object source, Field field) {
622-
return convertAssociation(source, field.getProperty());
626+
Object value = convertAssociation(source, field.getProperty());
627+
if (value != null && field.isIdField() && field.getFieldType() != value.getClass()) {
628+
return convertId(value, field.getFieldType());
629+
}
630+
return value;
623631
}
624632

625633
/**
@@ -1045,6 +1053,9 @@ public TypeInformation<?> getTypeHint() {
10451053
return ClassTypeInformation.OBJECT;
10461054
}
10471055

1056+
public Class<?> getFieldType() {
1057+
return Object.class;
1058+
}
10481059
}
10491060

10501061
/**
@@ -1197,10 +1208,11 @@ private Association<MongoPersistentProperty> findAssociation() {
11971208
return null;
11981209
}
11991210

1200-
/*
1201-
* (non-Javadoc)
1202-
* @see org.springframework.data.mongodb.core.convert.QueryMapper.Field#getTargetKey()
1203-
*/
1211+
@Override
1212+
public Class<?> getFieldType() {
1213+
return property.getFieldType();
1214+
}
1215+
12041216
@Override
12051217
public String getMappedKey() {
12061218
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)