Skip to content

Commit d0bf0e2

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 28efb3a commit d0bf0e2

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
@@ -194,19 +194,7 @@ public Document getMappedSort(Document sortObject, @Nullable MongoPersistentEnti
194194
return new Document();
195195
}
196196

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

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

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

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

236241
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
@@ -55,6 +55,7 @@
5555
import org.springframework.data.mongodb.core.mapping.Document;
5656
import org.springframework.data.mongodb.core.mapping.Field;
5757
import org.springframework.data.mongodb.core.mapping.FieldType;
58+
import org.springframework.data.mongodb.core.mapping.MongoId;
5859
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
5960
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
6061
import org.springframework.data.mongodb.core.mapping.TextScore;
@@ -1302,6 +1303,13 @@ void allowsUsingFieldPathsForPropertiesHavingCustomConversionRegistered() {
13021303
assertThat(mapper.getMappedSort(query.getQueryObject(), context.getPersistentEntity(Customer.class))).isEqualTo(new org.bson.Document("address.street", "1007 Mountain Drive"));
13031304
}
13041305

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

13071315
List<WithNestedArray> level0;
@@ -1365,6 +1373,12 @@ class Sample {
13651373
@Id private String foo;
13661374
}
13671375

1376+
class WithStringId {
1377+
1378+
@MongoId String id;
1379+
String name;
1380+
}
1381+
13681382
class BigIntegerId {
13691383

13701384
@Id private BigInteger id;

0 commit comments

Comments
 (0)