6161import org .springframework .data .mongodb .util .spel .ExpressionUtils ;
6262import org .springframework .data .spel .EvaluationContextProvider ;
6363import org .springframework .expression .EvaluationContext ;
64- import org .springframework .expression .spel .standard .SpelExpressionParser ;
6564import org .springframework .util .Assert ;
6665import org .springframework .util .ClassUtils ;
6766import org .springframework .util .ObjectUtils ;
8584public 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 );
0 commit comments