Skip to content

Commit 07a27da

Browse files
add tests for storting
1 parent 4b3d93e commit 07a27da

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
@@ -1084,6 +1084,73 @@ void rendersQueryOnNestedPrefixedEmbeddedCorrectly() {
10841084
assertThat(target).isEqualTo(new org.bson.Document("withPrefixedEmbedded.prefix-stringValue", "test"));
10851085
}
10861086

1087+
@Test // DATAMONGO-1902
1088+
void sortByEmbeddable() {
1089+
1090+
Query query = new Query().with(Sort.by("embeddableValue"));
1091+
1092+
org.bson.Document document = mapper.getMappedSort(query.getSortObject(),
1093+
context.getPersistentEntity(WithEmbedded.class));
1094+
1095+
assertThat(document).isEqualTo(new org.bson.Document());
1096+
}
1097+
1098+
@Test // DATAMONGO-1902
1099+
void sortByEmbeddableValue() {
1100+
1101+
// atFieldAnnotatedValue
1102+
Query query = new Query().with(Sort.by("embeddableValue.stringValue"));
1103+
1104+
org.bson.Document document = mapper.getMappedSort(query.getSortObject(),
1105+
context.getPersistentEntity(WithEmbedded.class));
1106+
1107+
assertThat(document).isEqualTo(new org.bson.Document("stringValue", 1));
1108+
}
1109+
1110+
@Test // DATAMONGO-1902
1111+
void sortByEmbeddableValueWithFieldAnnotation() {
1112+
1113+
Query query = new Query().with(Sort.by("embeddableValue.atFieldAnnotatedValue"));
1114+
1115+
org.bson.Document document = mapper.getMappedSort(query.getSortObject(),
1116+
context.getPersistentEntity(WithEmbedded.class));
1117+
1118+
assertThat(document).isEqualTo(new org.bson.Document("with-at-field-annotation", 1));
1119+
}
1120+
1121+
@Test // DATAMONGO-1902
1122+
void sortByPrefixedEmbeddableValueWithFieldAnnotation() {
1123+
1124+
Query query = new Query().with(Sort.by("embeddableValue.atFieldAnnotatedValue"));
1125+
1126+
org.bson.Document document = mapper.getMappedSort(query.getSortObject(),
1127+
context.getPersistentEntity(WithPrefixedEmbedded.class));
1128+
1129+
assertThat(document).isEqualTo(new org.bson.Document("prefix-with-at-field-annotation", 1));
1130+
}
1131+
1132+
@Test // DATAMONGO-1902
1133+
void sortByNestedEmbeddableValueWithFieldAnnotation() {
1134+
1135+
Query query = new Query().with(Sort.by("withEmbedded.embeddableValue.atFieldAnnotatedValue"));
1136+
1137+
org.bson.Document document = mapper.getMappedSort(query.getSortObject(),
1138+
context.getPersistentEntity(WrapperAroundWithEmbedded.class));
1139+
1140+
assertThat(document).isEqualTo(new org.bson.Document("withEmbedded.with-at-field-annotation", 1));
1141+
}
1142+
1143+
@Test // DATAMONGO-1902
1144+
void sortByNestedPrefixedEmbeddableValueWithFieldAnnotation() {
1145+
1146+
Query query = new Query().with(Sort.by("withPrefixedEmbedded.embeddableValue.atFieldAnnotatedValue"));
1147+
1148+
org.bson.Document document = mapper.getMappedSort(query.getSortObject(),
1149+
context.getPersistentEntity(WrapperAroundWithEmbedded.class));
1150+
1151+
assertThat(document).isEqualTo(new org.bson.Document("withPrefixedEmbedded.prefix-with-at-field-annotation", 1));
1152+
}
1153+
10871154
class WithDeepArrayNesting {
10881155

10891156
List<WithNestedArray> level0;

0 commit comments

Comments
 (0)