Skip to content

Commit 7dfe460

Browse files
christophstroblmp911de
authored andcommitted
Fix field projection value conversion.
The field projection conversion should actually only map field names and avoid value conversion. In the MongoId case an inclusion parameter (1) was unintentionally converted into its String representation which causes trouble on Mongo 4.4 servers. Fixes: #3668 Original pull request: #3678.
1 parent 73a0f04 commit 7dfe460

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

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

+21-16
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,7 @@ public Document getMappedSort(Document sortObject, @Nullable MongoPersistentEnti
195195
return new Document();
196196
}
197197

198-
sortObject = filterUnwrappedObjects(sortObject, entity);
199-
200-
Document mappedSort = new Document();
201-
for (Map.Entry<String, Object> entry : BsonUtils.asMap(sortObject).entrySet()) {
202-
203-
Field field = createPropertyField(entity, entry.getKey(), mappingContext);
204-
if (field.getProperty() != null && field.getProperty().isUnwrapped()) {
205-
continue;
206-
}
207-
208-
mappedSort.put(field.getMappedKey(), entry.getValue());
209-
}
210-
198+
Document mappedSort = mapFieldsToPropertyNames(sortObject, entity);
211199
mapMetaAttributes(mappedSort, entity, MetaMapping.WHEN_PRESENT);
212200
return mappedSort;
213201
}
@@ -225,13 +213,30 @@ public Document getMappedFields(Document fieldsObject, @Nullable MongoPersistent
225213

226214
Assert.notNull(fieldsObject, "FieldsObject must not be null!");
227215

228-
fieldsObject = filterUnwrappedObjects(fieldsObject, entity);
229-
230-
Document mappedFields = getMappedObject(fieldsObject, entity);
216+
Document mappedFields = mapFieldsToPropertyNames(fieldsObject, entity);
231217
mapMetaAttributes(mappedFields, entity, MetaMapping.FORCE);
232218
return mappedFields;
233219
}
234220

221+
private Document mapFieldsToPropertyNames(Document fields, @Nullable MongoPersistentEntity<?> entity) {
222+
223+
if (fields.isEmpty()) {
224+
return new Document();
225+
226+
}
227+
Document target = new Document();
228+
for (Map.Entry<String, Object> entry : BsonUtils.asMap(filterUnwrappedObjects(fields, entity)).entrySet()) {
229+
230+
Field field = createPropertyField(entity, entry.getKey(), mappingContext);
231+
if (field.getProperty() != null && field.getProperty().isUnwrapped()) {
232+
continue;
233+
}
234+
235+
target.put(field.getMappedKey(), entry.getValue());
236+
}
237+
return target;
238+
}
239+
235240
private void mapMetaAttributes(Document source, @Nullable MongoPersistentEntity<?> entity, MetaMapping metaMapping) {
236241

237242
if (entity == null) {

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

+14
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.springframework.data.mongodb.core.mapping.Document;
5151
import org.springframework.data.mongodb.core.mapping.Field;
5252
import org.springframework.data.mongodb.core.mapping.FieldType;
53+
import org.springframework.data.mongodb.core.mapping.MongoId;
5354
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
5455
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
5556
import org.springframework.data.mongodb.core.mapping.TextScore;
@@ -1304,6 +1305,13 @@ void allowsUsingFieldPathsForPropertiesHavingCustomConversionRegistered() {
13041305
assertThat(mapper.getMappedSort(query.getQueryObject(), context.getPersistentEntity(Customer.class))).isEqualTo(new org.bson.Document("address.street", "1007 Mountain Drive"));
13051306
}
13061307

1308+
@Test // GH-3668
1309+
void mapStringIdFieldProjection() {
1310+
1311+
org.bson.Document mappedFields = mapper.getMappedFields(new org.bson.Document("id", 1), context.getPersistentEntity(WithStringId.class));
1312+
assertThat(mappedFields).containsEntry("_id", 1);
1313+
}
1314+
13071315
class WithDeepArrayNesting {
13081316

13091317
List<WithNestedArray> level0;
@@ -1367,6 +1375,12 @@ class Sample {
13671375
@Id private String foo;
13681376
}
13691377

1378+
class WithStringId {
1379+
1380+
@MongoId String id;
1381+
String name;
1382+
}
1383+
13701384
class BigIntegerId {
13711385

13721386
@Id private BigInteger id;

0 commit comments

Comments
 (0)