Browse Source

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.
pull/834/head
Christoph Strobl 6 years ago committed by Mark Paluch
parent
commit
37ee677a67
No known key found for this signature in database
GPG Key ID: 51A00FA751B91849
  1. 4
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java
  2. 8
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java
  3. 25
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

4
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java

@ -127,7 +127,7 @@ public class DefaultIndexOperations implements IndexOperations { @@ -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 { @@ -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) {

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

@ -174,7 +174,13 @@ public class QueryMapper { @@ -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;
}

25
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateTests.java

@ -44,7 +44,6 @@ import org.junit.Before; @@ -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 { @@ -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 { @@ -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;
}
}

Loading…
Cancel
Save