Skip to content

Commit

Permalink
Use PropertyResolver to resolve placeholders in KeySpace
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Sazon committed May 20, 2021
1 parent 9ac86e3 commit 40d79f0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.keyvalue.core.mapping;

import org.springframework.core.env.PropertyResolver;
import org.springframework.data.mapping.model.BasicPersistentEntity;
import org.springframework.data.util.TypeInformation;
import org.springframework.expression.Expression;
Expand All @@ -30,6 +31,7 @@
* @author Christoph Strobl
* @author Oliver Gierke
* @author Mark Paluch
* @author Tim Sazon
* @param <T>
*/
public class BasicKeyValuePersistentEntity<T, P extends KeyValuePersistentProperty<P>>
Expand Down Expand Up @@ -91,8 +93,17 @@ private static String resolveKeyspace(@Nullable KeySpaceResolver fallbackKeySpac
*/
@Override
public String getKeySpace() {
return keyspaceExpression == null //
String keySpace = keyspaceExpression == null //
? keyspace //
: keyspaceExpression.getValue(getEvaluationContext(null), String.class);

if (keySpace != null) {
PropertyResolver propertyResolver = getPropertyResolver();
if (propertyResolver != null) {
return propertyResolver.resolvePlaceholders(keySpace);
}
}

return keySpace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.spel.ExtensionAwareEvaluationContextProvider;
import org.springframework.data.spel.spi.EvaluationContextExtension;
import org.springframework.mock.env.MockEnvironment;

/**
* Unit tests for {@link BasicKeyValuePersistentEntity}.
*
* @author Mark Paluch
* @author Tim Sazon
*/
class BasicKeyValuePersistentEntityUnitTests {

Expand Down Expand Up @@ -64,9 +66,22 @@ void shouldEvaluateEntityWithoutKeyspace() {
assertThat(persistentEntity.getKeySpace()).isEqualTo(NoKeyspaceEntity.class.getName());
}

@Test // GH-375
void shouldEvaluateKeyspaceWithPropertyPlaceholders() {

BasicKeyValuePersistentEntity<?, ?> persistentEntity = (BasicKeyValuePersistentEntity<?, ?>) mappingContext
.getPersistentEntity(PropertyEntity.class);
persistentEntity.setPropertyResolver(new MockEnvironment().withProperty("myProperty", "value"));

assertThat(persistentEntity.getKeySpace()).isEqualTo("value");
}

@KeySpace("#{myProperty}")
private static class ExpressionEntity {}

@KeySpace("${myProperty}")
private static class PropertyEntity {}

@KeySpace
private static class KeyspaceEntity {}

Expand Down

0 comments on commit 40d79f0

Please sign in to comment.