1919import static org .skyscreamer .jsonassert .JSONAssert .*;
2020
2121import org .json .JSONException ;
22+ import org .jspecify .annotations .NonNull ;
2223import org .jspecify .annotations .Nullable ;
24+ import org .junit .jupiter .api .BeforeEach ;
2325import org .junit .jupiter .api .DisplayName ;
2426import org .junit .jupiter .api .Nested ;
2527import org .junit .jupiter .api .Test ;
28+ import org .springframework .beans .factory .annotation .Autowired ;
29+ import org .springframework .context .ApplicationContext ;
30+ import org .springframework .context .annotation .Bean ;
31+ import org .springframework .context .annotation .Configuration ;
2632import org .springframework .data .annotation .Id ;
2733import org .springframework .data .annotation .Version ;
2834import org .springframework .data .elasticsearch .annotations .Document ;
3844import org .springframework .data .mapping .model .PropertyNameFieldNamingStrategy ;
3945import org .springframework .data .mapping .model .SimpleTypeHolder ;
4046import org .springframework .data .util .TypeInformation ;
47+ import org .springframework .test .context .junit .jupiter .SpringJUnitConfig ;
4148import org .springframework .util .ReflectionUtils ;
4249
4350/**
@@ -60,9 +67,9 @@ class PropertiesTests {
6067@ Test
6168public void shouldThrowExceptionGivenVersionPropertyIsNotLong () {
6269
63- TypeInformation <EntityWithWrongVersionType > typeInformation = TypeInformation
70+ TypeInformation <@ NonNull EntityWithWrongVersionType > typeInformation = TypeInformation
6471.of (EntityWithWrongVersionType .class );
65- SimpleElasticsearchPersistentEntity <EntityWithWrongVersionType > entity = new SimpleElasticsearchPersistentEntity <>(
72+ SimpleElasticsearchPersistentEntity <@ NonNull EntityWithWrongVersionType > entity = new SimpleElasticsearchPersistentEntity <>(
6673typeInformation , contextConfiguration );
6774
6875assertThatThrownBy (() -> createProperty (entity , "version" )).isInstanceOf (MappingException .class );
@@ -71,9 +78,9 @@ public void shouldThrowExceptionGivenVersionPropertyIsNotLong() {
7178@ Test
7279public void shouldThrowExceptionGivenMultipleVersionPropertiesArePresent () {
7380
74- TypeInformation <EntityWithMultipleVersionField > typeInformation = TypeInformation
81+ TypeInformation <@ NonNull EntityWithMultipleVersionField > typeInformation = TypeInformation
7582.of (EntityWithMultipleVersionField .class );
76- SimpleElasticsearchPersistentEntity <EntityWithMultipleVersionField > entity = new SimpleElasticsearchPersistentEntity <>(
83+ SimpleElasticsearchPersistentEntity <@ NonNull EntityWithMultipleVersionField > entity = new SimpleElasticsearchPersistentEntity <>(
7784typeInformation , contextConfiguration );
7885SimpleElasticsearchPersistentProperty persistentProperty1 = createProperty (entity , "version1" );
7986SimpleElasticsearchPersistentProperty persistentProperty2 = createProperty (entity , "version2" );
@@ -100,9 +107,9 @@ void shouldFindPropertiesByMappedName() {
100107@ Test
101108// DATAES-799
102109void shouldReportThatThereIsNoSeqNoPrimaryTermPropertyWhenThereIsNoSuchProperty () {
103- TypeInformation <EntityWithoutSeqNoPrimaryTerm > typeInformation = TypeInformation
110+ TypeInformation <@ NonNull EntityWithoutSeqNoPrimaryTerm > typeInformation = TypeInformation
104111.of (EntityWithoutSeqNoPrimaryTerm .class );
105- SimpleElasticsearchPersistentEntity <EntityWithoutSeqNoPrimaryTerm > entity = new SimpleElasticsearchPersistentEntity <>(
112+ SimpleElasticsearchPersistentEntity <@ NonNull EntityWithoutSeqNoPrimaryTerm > entity = new SimpleElasticsearchPersistentEntity <>(
106113typeInformation , contextConfiguration );
107114
108115assertThat (entity .hasSeqNoPrimaryTermProperty ()).isFalse ();
@@ -111,9 +118,9 @@ void shouldReportThatThereIsNoSeqNoPrimaryTermPropertyWhenThereIsNoSuchProperty(
111118@ Test
112119// DATAES-799
113120void shouldReportThatThereIsSeqNoPrimaryTermPropertyWhenThereIsSuchProperty () {
114- TypeInformation <EntityWithSeqNoPrimaryTerm > typeInformation = TypeInformation
121+ TypeInformation <@ NonNull EntityWithSeqNoPrimaryTerm > typeInformation = TypeInformation
115122.of (EntityWithSeqNoPrimaryTerm .class );
116- SimpleElasticsearchPersistentEntity <EntityWithSeqNoPrimaryTerm > entity = new SimpleElasticsearchPersistentEntity <>(
123+ SimpleElasticsearchPersistentEntity <@ NonNull EntityWithSeqNoPrimaryTerm > entity = new SimpleElasticsearchPersistentEntity <>(
117124typeInformation , contextConfiguration );
118125
119126entity .addPersistentProperty (createProperty (entity , "seqNoPrimaryTerm" ));
@@ -125,9 +132,9 @@ void shouldReportThatThereIsSeqNoPrimaryTermPropertyWhenThereIsSuchProperty() {
125132// DATAES-799
126133void shouldReturnSeqNoPrimaryTermPropertyWhenThereIsSuchProperty () {
127134
128- TypeInformation <EntityWithSeqNoPrimaryTerm > typeInformation = TypeInformation
135+ TypeInformation <@ NonNull EntityWithSeqNoPrimaryTerm > typeInformation = TypeInformation
129136.of (EntityWithSeqNoPrimaryTerm .class );
130- SimpleElasticsearchPersistentEntity <EntityWithSeqNoPrimaryTerm > entity = new SimpleElasticsearchPersistentEntity <>(
137+ SimpleElasticsearchPersistentEntity <@ NonNull EntityWithSeqNoPrimaryTerm > entity = new SimpleElasticsearchPersistentEntity <>(
131138typeInformation , contextConfiguration );
132139entity .addPersistentProperty (createProperty (entity , "seqNoPrimaryTerm" ));
133140EntityWithSeqNoPrimaryTerm instance = new EntityWithSeqNoPrimaryTerm ();
@@ -144,9 +151,9 @@ void shouldReturnSeqNoPrimaryTermPropertyWhenThereIsSuchProperty() {
144151@ Test
145152// DATAES-799
146153void shouldNotAllowMoreThanOneSeqNoPrimaryTermProperties () {
147- TypeInformation <EntityWithSeqNoPrimaryTerm > typeInformation = TypeInformation
154+ TypeInformation <@ NonNull EntityWithSeqNoPrimaryTerm > typeInformation = TypeInformation
148155.of (EntityWithSeqNoPrimaryTerm .class );
149- SimpleElasticsearchPersistentEntity <EntityWithSeqNoPrimaryTerm > entity = new SimpleElasticsearchPersistentEntity <>(
156+ SimpleElasticsearchPersistentEntity <@ NonNull EntityWithSeqNoPrimaryTerm > entity = new SimpleElasticsearchPersistentEntity <>(
150157typeInformation , contextConfiguration );
151158entity .addPersistentProperty (createProperty (entity , "seqNoPrimaryTerm" ));
152159
@@ -164,7 +171,24 @@ void shouldAllowFieldsWithIdPropertyNames() {
164171
165172@ Nested
166173@ DisplayName ("index settings" )
174+ @ SpringJUnitConfig ({ SettingsTests .Config .class })
167175class SettingsTests {
176+ @ Autowired private ApplicationContext applicationContext ;
177+
178+ @ Configuration
179+ static class Config {
180+ @ Bean
181+ public SpelTestBean spelTestBean () {
182+ return new SpelTestBean ();
183+ }
184+ }
185+
186+ @ BeforeEach
187+ void setUp () {
188+ ((SimpleElasticsearchMappingContext ) elasticsearchConverter
189+ .get ().getMappingContext ()).setApplicationContext (applicationContext );
190+
191+ }
168192
169193@ Test // #1719
170194@ DisplayName ("should error if index sorting parameters do not have the same number of arguments" )
@@ -205,6 +229,24 @@ void shouldWriteSortParametersToSettingsObject() throws JSONException {
205229String json = entity .getDefaultSettings ().toJson ();
206230assertEquals (expected , json , false );
207231}
232+
233+ @ Test // #3187
234+ @ DisplayName ("should evaluate SpEL expression in settingPath" )
235+ void shouldEvaluateSpElExpressionInSettingPath () {
236+
237+ var settingPath = elasticsearchConverter .get ().getMappingContext ()
238+ .getRequiredPersistentEntity (SettingPathWithSpel .class ).settingPath ();
239+
240+ assertThat (settingPath ).isEqualTo (SpelTestBean .SETTING_PATH );
241+ }
242+
243+ private static class SpelTestBean {
244+ public static String SETTING_PATH = "test-setting-path" ;
245+
246+ public String settingPath () {
247+ return SETTING_PATH ;
248+ }
249+ }
208250}
209251
210252@ Nested
@@ -271,7 +313,7 @@ void shouldWriteTypeHintsWhenConfiguredExplicitlyOnEntityAndGlobalSettingIsFalse
271313}
272314}
273315
274- // region helper functions
316+ // region helper
275317private static SimpleElasticsearchPersistentProperty createProperty (SimpleElasticsearchPersistentEntity <?> entity ,
276318String fieldName ) {
277319
@@ -282,6 +324,7 @@ private static SimpleElasticsearchPersistentProperty createProperty(SimpleElasti
282324return new SimpleElasticsearchPersistentProperty (property , entity , SimpleTypeHolder .DEFAULT );
283325
284326}
327+
285328// endregion
286329
287330// region entities
@@ -295,7 +338,7 @@ public String getVersion() {
295338return version ;
296339}
297340
298- public void setVersion (String version ) {
341+ public void setVersion (@ Nullable String version ) {
299342this .version = version ;
300343}
301344}
@@ -313,7 +356,7 @@ public Long getVersion1() {
313356return version1 ;
314357}
315358
316- public void setVersion1 (Long version1 ) {
359+ public void setVersion1 (@ Nullable Long version1 ) {
317360this .version1 = version1 ;
318361}
319362
@@ -322,7 +365,7 @@ public Long getVersion2() {
322365return version2 ;
323366}
324367
325- public void setVersion2 (Long version2 ) {
368+ public void setVersion2 (@ Nullable Long version2 ) {
326369this .version2 = version2 ;
327370}
328371}
@@ -397,5 +440,12 @@ private static class EnableTypeHintExplicitSetting {
397440@ Nullable
398441@ Id String id ;
399442}
443+
444+ @ Document (indexName = "foo" )
445+ @ Setting (settingPath = "#{@spelTestBean.settingPath}" )
446+ private static class SettingPathWithSpel {
447+ @ Nullable
448+ @ Id String id ;
449+ }
400450// endregion
401451}
0 commit comments