DATAMONGO-2451 - Fix value conversion for id properties used in sort expression.
Previously we falsely converted the sort value (1/-1) into the id types target value when a Field annotation had been present. Original pull request: #822.
This commit is contained in:
committed by
Mark Paluch
parent
1729744a9e
commit
37ee677a67
@@ -127,7 +127,7 @@ public class DefaultIndexOperations implements IndexOperations {
|
||||
indexOptions = addPartialFilterIfPresent(indexOptions, indexDefinition.getIndexOptions(), entity);
|
||||
indexOptions = addDefaultCollationIfRequired(indexOptions, entity);
|
||||
|
||||
Document mappedKeys = mapper.getMappedObject(indexDefinition.getIndexKeys(), entity);
|
||||
Document mappedKeys = mapper.getMappedSort(indexDefinition.getIndexKeys(), entity);
|
||||
return collection.createIndex(mappedKeys, indexOptions);
|
||||
});
|
||||
}
|
||||
@@ -223,7 +223,7 @@ public class DefaultIndexOperations implements IndexOperations {
|
||||
|
||||
Assert.isInstanceOf(Document.class, sourceOptions.get(PARTIAL_FILTER_EXPRESSION_KEY));
|
||||
return ops.partialFilterExpression(
|
||||
mapper.getMappedObject((Document) sourceOptions.get(PARTIAL_FILTER_EXPRESSION_KEY), entity));
|
||||
mapper.getMappedSort((Document) sourceOptions.get(PARTIAL_FILTER_EXPRESSION_KEY), entity));
|
||||
}
|
||||
|
||||
private static IndexOptions addDefaultCollationIfRequired(IndexOptions ops, MongoPersistentEntity<?> entity) {
|
||||
|
||||
@@ -174,7 +174,13 @@ public class QueryMapper {
|
||||
return new Document();
|
||||
}
|
||||
|
||||
Document mappedSort = getMappedObject(sortObject, entity);
|
||||
Document mappedSort = new Document();
|
||||
for(Map.Entry<String,Object> entry : BsonUtils.asMap(sortObject).entrySet()) {
|
||||
|
||||
Field field = createPropertyField(entity, entry.getKey(), mappingContext);
|
||||
mappedSort.put(field.getMappedKey(), entry.getValue());
|
||||
}
|
||||
|
||||
mapMetaAttributes(mappedSort, entity, MetaMapping.WHEN_PRESENT);
|
||||
return mappedSort;
|
||||
}
|
||||
|
||||
@@ -44,7 +44,6 @@ import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
@@ -3711,6 +3710,20 @@ public class MongoTemplateTests {
|
||||
assertThat(target.inner.id).isEqualTo(innerId);
|
||||
}
|
||||
|
||||
@Test // DATAMONGO-2451
|
||||
public void sortOnIdFieldWithExplicitTypeShouldWork() {
|
||||
|
||||
template.dropCollection(WithIdAndFieldAnnotation.class);
|
||||
|
||||
WithIdAndFieldAnnotation f = new WithIdAndFieldAnnotation();
|
||||
f.id = new ObjectId().toHexString();
|
||||
f.value = "value";
|
||||
|
||||
template.save(f);
|
||||
|
||||
assertThat(template.find(new BasicQuery("{}").with(Sort.by("id")), WithIdAndFieldAnnotation.class)).isNotEmpty();
|
||||
}
|
||||
|
||||
private AtomicReference<ImmutableVersioned> createAfterSaveReference() {
|
||||
|
||||
AtomicReference<ImmutableVersioned> saved = new AtomicReference<>();
|
||||
@@ -4245,4 +4258,14 @@ public class MongoTemplateTests {
|
||||
@Field("id") String id;
|
||||
String value;
|
||||
}
|
||||
|
||||
@Data
|
||||
static class WithIdAndFieldAnnotation {
|
||||
|
||||
@Id //
|
||||
@Field(name = "_id") //
|
||||
String id;
|
||||
String value;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user