Skip to content

Commit eb51828

Browse files
committed
DATAMONGO-1737 - BasicMongoPersistentEntity now correctly initializes comparator.
In BasicMongoPersistentEntity.verify() we now properly call the super method to make sure the comparators that honor the @field's order value are initialized properly.
1 parent e023717 commit eb51828

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentEntity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ public boolean hasTextScoreProperty() {
149149
@Override
150150
public void verify() {
151151

152+
super.verify();
153+
152154
verifyFieldUniqueness();
153155
verifyFieldTypes();
154156
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/BasicMongoPersistentPropertyUnitTests.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@
1515
*/
1616
package org.springframework.data.mongodb.core.mapping;
1717

18+
import static org.assertj.core.api.Assertions.assertThat;
1819
import static org.hamcrest.CoreMatchers.*;
19-
import static org.junit.Assert.*;
20+
import static org.junit.Assert.assertThat;
2021

2122
import java.lang.annotation.ElementType;
2223
import java.lang.annotation.Retention;
2324
import java.lang.annotation.RetentionPolicy;
2425
import java.lang.annotation.Target;
2526
import java.lang.reflect.Field;
27+
import java.util.ArrayList;
28+
import java.util.List;
2629
import java.util.Locale;
2730

2831
import org.junit.Before;
@@ -186,6 +189,19 @@ public void shouldConsiderComposedAnnotationsForFields() {
186189
assertThat(property.getFieldName(), is("myField"));
187190
}
188191

192+
@Test // DATAMONGO-1737
193+
public void honorsFieldOrderWhenIteratingOverProperties() {
194+
195+
MongoMappingContext context = new MongoMappingContext();
196+
BasicMongoPersistentEntity<?> entity = context.getPersistentEntity(Sample.class);
197+
198+
List<String> properties = new ArrayList<>();
199+
200+
entity.doWithProperties((MongoPersistentProperty property) -> properties.add(property.getName()));
201+
202+
assertThat(properties).containsExactly("first", "second", "third");
203+
}
204+
189205
private MongoPersistentProperty getPropertyFor(Field field) {
190206
return getPropertyFor(entity, field);
191207
}
@@ -213,6 +229,13 @@ class Person {
213229
@org.springframework.data.mongodb.core.mapping.Field(order = -20) String ssn;
214230
}
215231

232+
class Sample {
233+
234+
@org.springframework.data.mongodb.core.mapping.Field(order = 2) String second;
235+
@org.springframework.data.mongodb.core.mapping.Field(order = 3) String third;
236+
@org.springframework.data.mongodb.core.mapping.Field(order = 1) String first;
237+
}
238+
216239
enum UppercaseFieldNamingStrategy implements FieldNamingStrategy {
217240

218241
INSTANCE;

0 commit comments

Comments
 (0)