Skip to content

Commit 1518fc2

Browse files
add tests for storting
1 parent a21c57f commit 1518fc2

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ public Document getMappedSort(Document sortObject, @Nullable MongoPersistentEnti
191191
for (Map.Entry<String, Object> entry : BsonUtils.asMap(sortObject).entrySet()) {
192192

193193
Field field = createPropertyField(entity, entry.getKey(), mappingContext);
194+
if(field.getProperty() != null && field.getProperty().isEmbedded()) {
195+
continue;
196+
}
197+
194198
mappedSort.put(field.getMappedKey(), entry.getValue());
195199
}
196200

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,73 @@ void rendersQueryOnNestedPrefixedEmbeddedCorrectly() {
10971097
assertThat(target).isEqualTo(new org.bson.Document("withPrefixedEmbedded.prefix-stringValue", "test"));
10981098
}
10991099

1100+
@Test // DATAMONGO-1902
1101+
void sortByEmbeddable() {
1102+
1103+
Query query = new Query().with(Sort.by("embeddableValue"));
1104+
1105+
org.bson.Document document = mapper.getMappedSort(query.getSortObject(),
1106+
context.getPersistentEntity(WithEmbedded.class));
1107+
1108+
assertThat(document).isEqualTo(new org.bson.Document());
1109+
}
1110+
1111+
@Test // DATAMONGO-1902
1112+
void sortByEmbeddableValue() {
1113+
1114+
// atFieldAnnotatedValue
1115+
Query query = new Query().with(Sort.by("embeddableValue.stringValue"));
1116+
1117+
org.bson.Document document = mapper.getMappedSort(query.getSortObject(),
1118+
context.getPersistentEntity(WithEmbedded.class));
1119+
1120+
assertThat(document).isEqualTo(new org.bson.Document("stringValue", 1));
1121+
}
1122+
1123+
@Test // DATAMONGO-1902
1124+
void sortByEmbeddableValueWithFieldAnnotation() {
1125+
1126+
Query query = new Query().with(Sort.by("embeddableValue.atFieldAnnotatedValue"));
1127+
1128+
org.bson.Document document = mapper.getMappedSort(query.getSortObject(),
1129+
context.getPersistentEntity(WithEmbedded.class));
1130+
1131+
assertThat(document).isEqualTo(new org.bson.Document("with-at-field-annotation", 1));
1132+
}
1133+
1134+
@Test // DATAMONGO-1902
1135+
void sortByPrefixedEmbeddableValueWithFieldAnnotation() {
1136+
1137+
Query query = new Query().with(Sort.by("embeddableValue.atFieldAnnotatedValue"));
1138+
1139+
org.bson.Document document = mapper.getMappedSort(query.getSortObject(),
1140+
context.getPersistentEntity(WithPrefixedEmbedded.class));
1141+
1142+
assertThat(document).isEqualTo(new org.bson.Document("prefix-with-at-field-annotation", 1));
1143+
}
1144+
1145+
@Test // DATAMONGO-1902
1146+
void sortByNestedEmbeddableValueWithFieldAnnotation() {
1147+
1148+
Query query = new Query().with(Sort.by("withEmbedded.embeddableValue.atFieldAnnotatedValue"));
1149+
1150+
org.bson.Document document = mapper.getMappedSort(query.getSortObject(),
1151+
context.getPersistentEntity(WrapperAroundWithEmbedded.class));
1152+
1153+
assertThat(document).isEqualTo(new org.bson.Document("withEmbedded.with-at-field-annotation", 1));
1154+
}
1155+
1156+
@Test // DATAMONGO-1902
1157+
void sortByNestedPrefixedEmbeddableValueWithFieldAnnotation() {
1158+
1159+
Query query = new Query().with(Sort.by("withPrefixedEmbedded.embeddableValue.atFieldAnnotatedValue"));
1160+
1161+
org.bson.Document document = mapper.getMappedSort(query.getSortObject(),
1162+
context.getPersistentEntity(WrapperAroundWithEmbedded.class));
1163+
1164+
assertThat(document).isEqualTo(new org.bson.Document("withPrefixedEmbedded.prefix-with-at-field-annotation", 1));
1165+
}
1166+
11001167
class WithDeepArrayNesting {
11011168

11021169
List<WithNestedArray> level0;

0 commit comments

Comments
 (0)