Skip to content

Commit 38dcb1a

Browse files
Polishing.
Remove unused expression parser and make sure to obtain the EvaluationContext from the index resolver if configured. Original Pull Request: #5049
1 parent 7fd9792 commit 38dcb1a

File tree

2 files changed

+60
-31
lines changed

2 files changed

+60
-31
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolver.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
import org.springframework.data.mongodb.util.spel.ExpressionUtils;
6262
import org.springframework.data.spel.EvaluationContextProvider;
6363
import org.springframework.expression.EvaluationContext;
64-
import org.springframework.expression.spel.standard.SpelExpressionParser;
6564
import org.springframework.util.Assert;
6665
import org.springframework.util.ClassUtils;
6766
import org.springframework.util.ObjectUtils;
@@ -85,7 +84,6 @@
8584
public class MongoPersistentEntityIndexResolver implements IndexResolver {
8685

8786
private static final Log LOGGER = LogFactory.getLog(MongoPersistentEntityIndexResolver.class);
88-
private static final SpelExpressionParser PARSER = new SpelExpressionParser();
8987

9088
private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
9189
private EvaluationContextProvider evaluationContextProvider = EvaluationContextProvider.DEFAULT;
@@ -495,7 +493,7 @@ private org.bson.Document resolveCompoundIndexKeyFromStringDefinition(String dot
495493
return new org.bson.Document(dotPath, 1);
496494
}
497495

498-
Object keyDefToUse = ExpressionUtils.evaluate(keyDefinitionString, () -> getValueEvaluationContextForProperty(entity));
496+
Object keyDefToUse = ExpressionUtils.evaluate(keyDefinitionString, () -> getValueEvaluationContext(entity));
499497

500498
org.bson.Document dbo = (keyDefToUse instanceof org.bson.Document document) ? document
501499
: org.bson.Document.parse(ObjectUtils.nullSafeToString(keyDefToUse));
@@ -563,7 +561,7 @@ private org.bson.Document resolveCompoundIndexKeyFromStringDefinition(String dot
563561
}
564562

565563
Duration timeout = computeIndexTimeout(index.expireAfter(),
566-
getValueEvaluationContextForProperty(persistentProperty.getOwner()));
564+
getValueEvaluationContext(persistentProperty.getOwner()));
567565
if (!timeout.isNegative()) {
568566
indexDefinition.expire(timeout);
569567
}
@@ -579,7 +577,7 @@ private org.bson.Document resolveCompoundIndexKeyFromStringDefinition(String dot
579577

580578
private PartialIndexFilter evaluatePartialFilter(String filterExpression, @Nullable PersistentEntity<?, ?> entity) {
581579

582-
Object result = ExpressionUtils.evaluate(filterExpression, () -> getValueEvaluationContextForProperty(entity));
580+
Object result = ExpressionUtils.evaluate(filterExpression, () -> getValueEvaluationContext(entity));
583581

584582
if (result instanceof org.bson.Document document) {
585583
return PartialIndexFilter.of(document);
@@ -590,7 +588,7 @@ private PartialIndexFilter evaluatePartialFilter(String filterExpression, @Nulla
590588

591589
private org.bson.Document evaluateWildcardProjection(String projectionExpression, @Nullable PersistentEntity<?, ?> entity) {
592590

593-
Object result = ExpressionUtils.evaluate(projectionExpression, () -> getValueEvaluationContextForProperty(entity));
591+
Object result = ExpressionUtils.evaluate(projectionExpression, () -> getValueEvaluationContext(entity));
594592

595593
if (result instanceof org.bson.Document document) {
596594
return document;
@@ -601,7 +599,7 @@ private org.bson.Document evaluateWildcardProjection(String projectionExpression
601599

602600
private Collation evaluateCollation(String collationExpression, @Nullable PersistentEntity<?, ?> entity) {
603601

604-
Object result = ExpressionUtils.evaluate(collationExpression, () -> getValueEvaluationContextForProperty(entity));
602+
Object result = ExpressionUtils.evaluate(collationExpression, () -> getValueEvaluationContext(entity));
605603
if (result instanceof org.bson.Document document) {
606604
return Collation.from(document);
607605
}
@@ -657,14 +655,13 @@ protected EvaluationContext getEvaluationContext() {
657655
* @param persistentEntity can be {@literal null}
658656
* @return
659657
*/
660-
private ValueEvaluationContext getValueEvaluationContextForProperty(@Nullable PersistentEntity<?, ?> persistentEntity) {
658+
ValueEvaluationContext getValueEvaluationContext(@Nullable PersistentEntity<?, ?> persistentEntity) {
661659

662-
if (persistentEntity instanceof BasicMongoPersistentEntity<?> mongoEntity) {
660+
if (persistentEntity instanceof BasicMongoPersistentEntity<?> mongoEntity && ObjectUtils.nullSafeEquals(evaluationContextProvider, EvaluationContextProvider.DEFAULT)) {
663661
return mongoEntity.getValueEvaluationContext(null);
664662
}
665663

666-
return ValueEvaluationContext.of(
667-
new StandardEnvironment(), getEvaluationContext());
664+
return ValueEvaluationContext.of(new StandardEnvironment(), getEvaluationContext());
668665
}
669666

670667
/**
@@ -716,7 +713,7 @@ private String pathAwareIndexName(String indexName, String dotPath, @Nullable Pe
716713
String nameToUse = "";
717714
if (StringUtils.hasText(indexName)) {
718715

719-
Object result = ExpressionUtils.evaluate(indexName, () -> getValueEvaluationContextForProperty(entity));
716+
Object result = ExpressionUtils.evaluate(indexName, () -> getValueEvaluationContext(entity));
720717

721718
if (result != null) {
722719
nameToUse = ObjectUtils.nullSafeToString(result);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@
2626
import java.util.LinkedHashSet;
2727
import java.util.List;
2828
import java.util.Map;
29+
import java.util.Set;
2930

3031
import org.junit.Test;
3132
import org.junit.runner.RunWith;
3233
import org.junit.runners.Suite;
3334
import org.junit.runners.Suite.SuiteClasses;
3435

3536
import org.springframework.core.annotation.AliasFor;
36-
import org.springframework.core.env.MapPropertySource;
37-
import org.springframework.core.env.StandardEnvironment;
3837
import org.springframework.dao.InvalidDataAccessApiUsageException;
3938
import org.springframework.data.annotation.Id;
4039
import org.springframework.data.core.TypeInformation;
@@ -56,6 +55,9 @@
5655
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
5756
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
5857
import org.springframework.data.mongodb.core.mapping.Unwrapped;
58+
import org.springframework.data.spel.EvaluationContextProvider;
59+
import org.springframework.expression.EvaluationContext;
60+
import org.springframework.mock.env.MockEnvironment;
5961

6062
/**
6163
* Tests for {@link MongoPersistentEntityIndexResolver}.
@@ -108,23 +110,6 @@ public void shouldResolveIndexViaClass() {
108110
assertThat(definitions).isNotEmpty();
109111
}
110112

111-
@Test // GH-4980
112-
public void shouldSupportPropertyPlaceholderInExpireAfter() {
113-
StandardEnvironment environment = new StandardEnvironment();
114-
environment.getPropertySources().addFirst(new MapPropertySource("test", Map.of("ttl.timeout", "10m")));
115-
116-
MongoMappingContext mappingContext = new MongoMappingContext();
117-
mappingContext.setEnvironment(environment);
118-
119-
MongoPersistentEntityIndexResolver resolver = new MongoPersistentEntityIndexResolver(mappingContext);
120-
121-
List<IndexDefinitionHolder> indexDefinitions = (List<IndexDefinitionHolder>) resolver
122-
.resolveIndexFor(WithExpireAfterAsPropertyPlaceholder.class);
123-
124-
assertThat(indexDefinitions).hasSize(1);
125-
assertThat(indexDefinitions.get(0).getIndexOptions()).containsEntry("expireAfterSeconds", 600L);
126-
}
127-
128113
@Test // DATAMONGO-899
129114
public void deeplyNestedIndexPathIsResolvedCorrectly() {
130115

@@ -316,6 +301,53 @@ public void resolvesPartialFilter() {
316301
org.bson.Document.parse("{'value': {'$exists': true}}"));
317302
}
318303

304+
@Test // GH-4980
305+
public void resolvesPropertyPlaceholderInExpireAfter() {
306+
307+
MockEnvironment environment = new MockEnvironment();
308+
environment.setProperty("ttl.timeout", "10m");
309+
310+
MongoMappingContext mappingContext = new MongoMappingContext();
311+
mappingContext.setInitialEntitySet(Set.of(WithExpireAfterAsPropertyPlaceholder.class));
312+
mappingContext.setEnvironment(environment);
313+
mappingContext.initialize();
314+
315+
MongoPersistentEntityIndexResolver resolver = new MongoPersistentEntityIndexResolver(mappingContext);
316+
317+
List<IndexDefinitionHolder> indexDefinitions = (List<IndexDefinitionHolder>) resolver
318+
.resolveIndexFor(WithExpireAfterAsPropertyPlaceholder.class);
319+
320+
assertThat(indexDefinitions).hasSize(1);
321+
assertThat(indexDefinitions.get(0).getIndexOptions()).containsEntry("expireAfterSeconds", 600L);
322+
}
323+
324+
@Test // GH-4980
325+
public void usesValueExpressionResolversCorrectly() {
326+
327+
MockEnvironment environment = new MockEnvironment();
328+
environment.setProperty("ttl.timeout", "10m");
329+
330+
MongoMappingContext mappingContext = new MongoMappingContext();
331+
mappingContext.setInitialEntitySet(Set.of(WithExpireAfterAsPropertyPlaceholder.class));
332+
mappingContext.setEnvironment(environment);
333+
mappingContext.initialize();
334+
335+
MongoPersistentEntityIndexResolver resolver = new MongoPersistentEntityIndexResolver(mappingContext);
336+
337+
BasicMongoPersistentEntity<?> basicMongoPersistentEntity = mock(BasicMongoPersistentEntity.class);
338+
339+
resolver.getValueEvaluationContext(basicMongoPersistentEntity);
340+
verify(basicMongoPersistentEntity).getValueEvaluationContext(any());
341+
342+
EvaluationContext evaluationContext = mock(EvaluationContext.class);
343+
EvaluationContextProvider contextProviderMock = mock(EvaluationContextProvider.class);
344+
when(contextProviderMock.getEvaluationContext(any())).thenReturn(evaluationContext);
345+
resolver.setEvaluationContextProvider(contextProviderMock);
346+
347+
assertThat(resolver.getValueEvaluationContext(basicMongoPersistentEntity).getEvaluationContext())
348+
.isEqualTo(evaluationContext);
349+
}
350+
319351
@Document("Zero")
320352
class IndexOnLevelZero {
321353
@Indexed String indexedProperty;

0 commit comments

Comments
 (0)