2727import java .util .Map ;
2828import java .util .Set ;
2929import java .util .concurrent .TimeUnit ;
30- import java .util .function .Supplier ;
3130import java .util .stream .Collectors ;
3231
3332import org .apache .commons .logging .Log ;
3433import org .apache .commons .logging .LogFactory ;
3534import org .jspecify .annotations .Nullable ;
3635
3736import org .springframework .core .annotation .MergedAnnotation ;
37+ import org .springframework .core .env .StandardEnvironment ;
3838import org .springframework .dao .InvalidDataAccessApiUsageException ;
3939import org .springframework .data .core .TypeInformation ;
4040import org .springframework .data .domain .Sort ;
41+ import org .springframework .data .expression .ValueEvaluationContext ;
4142import org .springframework .data .mapping .Association ;
4243import org .springframework .data .mapping .AssociationHandler ;
4344import org .springframework .data .mapping .MappingException ;
7879 * @author Mark Paluch
7980 * @author Dave Perryman
8081 * @author Stefan Tirea
82+ * @author Sangbeen Moon
8183 * @since 1.5
8284 */
8385public class MongoPersistentEntityIndexResolver implements IndexResolver {
@@ -493,7 +495,7 @@ private org.bson.Document resolveCompoundIndexKeyFromStringDefinition(String dot
493495 return new org .bson .Document (dotPath , 1 );
494496 }
495497
496- Object keyDefToUse = ExpressionUtils .evaluate (keyDefinitionString , () -> getEvaluationContextForProperty (entity ));
498+ Object keyDefToUse = ExpressionUtils .evaluate (keyDefinitionString , () -> getValueEvaluationContextForProperty (entity ));
497499
498500 org .bson .Document dbo = (keyDefToUse instanceof org .bson .Document document ) ? document
499501 : org .bson .Document .parse (ObjectUtils .nullSafeToString (keyDefToUse ));
@@ -561,7 +563,7 @@ private org.bson.Document resolveCompoundIndexKeyFromStringDefinition(String dot
561563 }
562564
563565 Duration timeout = computeIndexTimeout (index .expireAfter (),
564- () -> getEvaluationContextForProperty (persistentProperty .getOwner ()));
566+ getValueEvaluationContextForProperty (persistentProperty .getOwner ()));
565567 if (!timeout .isNegative ()) {
566568 indexDefinition .expire (timeout );
567569 }
@@ -577,7 +579,7 @@ private org.bson.Document resolveCompoundIndexKeyFromStringDefinition(String dot
577579
578580 private PartialIndexFilter evaluatePartialFilter (String filterExpression , @ Nullable PersistentEntity <?, ?> entity ) {
579581
580- Object result = ExpressionUtils .evaluate (filterExpression , () -> getEvaluationContextForProperty (entity ));
582+ Object result = ExpressionUtils .evaluate (filterExpression , () -> getValueEvaluationContextForProperty (entity ));
581583
582584 if (result instanceof org .bson .Document document ) {
583585 return PartialIndexFilter .of (document );
@@ -588,7 +590,7 @@ private PartialIndexFilter evaluatePartialFilter(String filterExpression, @Nulla
588590
589591 private org .bson .Document evaluateWildcardProjection (String projectionExpression , @ Nullable PersistentEntity <?, ?> entity ) {
590592
591- Object result = ExpressionUtils .evaluate (projectionExpression , () -> getEvaluationContextForProperty (entity ));
593+ Object result = ExpressionUtils .evaluate (projectionExpression , () -> getValueEvaluationContextForProperty (entity ));
592594
593595 if (result instanceof org .bson .Document document ) {
594596 return document ;
@@ -599,7 +601,7 @@ private org.bson.Document evaluateWildcardProjection(String projectionExpression
599601
600602 private Collation evaluateCollation (String collationExpression , @ Nullable PersistentEntity <?, ?> entity ) {
601603
602- Object result = ExpressionUtils .evaluate (collationExpression , () -> getEvaluationContextForProperty (entity ));
604+ Object result = ExpressionUtils .evaluate (collationExpression , () -> getValueEvaluationContextForProperty (entity ));
603605 if (result instanceof org .bson .Document document ) {
604606 return Collation .from (document );
605607 }
@@ -650,24 +652,19 @@ protected EvaluationContext getEvaluationContext() {
650652 }
651653
652654 /**
653- * Get the {@link EvaluationContext } for a given {@link PersistentEntity entity} the default one.
655+ * Get the {@link ValueEvaluationContext } for a given {@link PersistentEntity entity} the default one.
654656 *
655657 * @param persistentEntity can be {@literal null}
656658 * @return
657659 */
658- private EvaluationContext getEvaluationContextForProperty (@ Nullable PersistentEntity <?, ?> persistentEntity ) {
660+ private ValueEvaluationContext getValueEvaluationContextForProperty (@ Nullable PersistentEntity <?, ?> persistentEntity ) {
659661
660- if (persistentEntity == null || !( persistentEntity instanceof BasicMongoPersistentEntity ) ) {
661- return getEvaluationContext ( );
662+ if (persistentEntity instanceof BasicMongoPersistentEntity <?> mongoEntity ) {
663+ return mongoEntity . getValueEvaluationContext ( null );
662664 }
663665
664- EvaluationContext contextFromEntity = ((BasicMongoPersistentEntity <?>) persistentEntity ).getEvaluationContext (null );
665-
666- if (contextFromEntity != null && !EvaluationContextProvider .DEFAULT .equals (contextFromEntity )) {
667- return contextFromEntity ;
668- }
669-
670- return getEvaluationContext ();
666+ return ValueEvaluationContext .of (
667+ new StandardEnvironment (), getEvaluationContext ());
671668 }
672669
673670 /**
@@ -719,7 +716,7 @@ private String pathAwareIndexName(String indexName, String dotPath, @Nullable Pe
719716 String nameToUse = "" ;
720717 if (StringUtils .hasText (indexName )) {
721718
722- Object result = ExpressionUtils .evaluate (indexName , () -> getEvaluationContextForProperty (entity ));
719+ Object result = ExpressionUtils .evaluate (indexName , () -> getValueEvaluationContextForProperty (entity ));
723720
724721 if (result != null ) {
725722 nameToUse = ObjectUtils .nullSafeToString (result );
@@ -780,7 +777,7 @@ private void resolveAndAddIndexesForAssociation(Association<MongoPersistentPrope
780777 * @since 2.2
781778 * @throws IllegalArgumentException for invalid duration values.
782779 */
783- private static Duration computeIndexTimeout (String timeoutValue , Supplier < EvaluationContext > evaluationContext ) {
780+ private static Duration computeIndexTimeout (String timeoutValue , ValueEvaluationContext evaluationContext ) {
784781 return DurationUtil .evaluate (timeoutValue , evaluationContext );
785782 }
786783
0 commit comments