Skip to content

Commit fc24a30

Browse files
committed
Reinstate annotation- and fallback keyspace resolution across multiple Spring Data KeyValue versions.
We now are compatible with Spring Data KeyValue 3.1 when determining the keyspace. We either fall back to BasicKeyValuePersistentEntity keyspace resolution if the entity is annotated or determine the fallback ourselves. Without the change, using Spring Data 3.1 does not consider annotated entities as the role of KeySpaceResolver has changed. Closes #800
1 parent 5c1abe8 commit fc24a30

File tree

3 files changed

+70
-27
lines changed

3 files changed

+70
-27
lines changed

spring-vault-core/src/main/java/org/springframework/vault/repository/mapping/BasicVaultPersistentEntity.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package org.springframework.vault.repository.mapping;
1717

18+
import org.springframework.core.annotation.MergedAnnotation;
19+
import org.springframework.core.annotation.MergedAnnotations;
20+
import org.springframework.data.keyvalue.annotation.KeySpace;
1821
import org.springframework.data.keyvalue.core.mapping.BasicKeyValuePersistentEntity;
1922
import org.springframework.data.keyvalue.core.mapping.KeySpaceResolver;
2023
import org.springframework.data.util.TypeInformation;
@@ -43,10 +46,29 @@ public class BasicVaultPersistentEntity<T> extends BasicKeyValuePersistentEntity
4346
/**
4447
* Creates new {@link BasicVaultPersistentEntity}.
4548
* @param information must not be {@literal null}.
46-
* @param fallbackKeySpaceResolver can be {@literal null}.
49+
* @param keySpaceResolver can be {@literal null}.
4750
*/
48-
public BasicVaultPersistentEntity(TypeInformation<T> information, KeySpaceResolver fallbackKeySpaceResolver) {
49-
super(information, fallbackKeySpaceResolver);
51+
public BasicVaultPersistentEntity(TypeInformation<T> information, @Nullable KeySpaceResolver keySpaceResolver) {
52+
super(information, type -> {
53+
54+
if (keySpaceResolver != null) {
55+
return keySpaceResolver.resolveKeySpace(type);
56+
}
57+
58+
MergedAnnotation<KeySpace> annotation = MergedAnnotations
59+
.from(type, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
60+
.get(KeySpace.class);
61+
62+
if (annotation.isPresent()
63+
&& StringUtils.hasText(annotation.getValue("value").map(Object::toString).orElse(null))) {
64+
65+
// fallback to use keyspace resolution and SpEL expression handling of
66+
// BasicKeyValuePersistentEntity.
67+
return null;
68+
}
69+
70+
return SimpleClassNameKeySpaceResolver.INSTANCE.resolveKeySpace(type);
71+
});
5072

5173
Secret annotation = findAnnotation(Secret.class);
5274

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.vault.repository.mapping;
17+
18+
import org.springframework.data.keyvalue.core.mapping.KeySpaceResolver;
19+
import org.springframework.util.Assert;
20+
import org.springframework.util.ClassUtils;
21+
import org.springframework.util.StringUtils;
22+
23+
/**
24+
* Most trivial implementation of {@link KeySpaceResolver} returning the
25+
* {@link Class#getName()}.
26+
*
27+
* @author Mark Paluch
28+
*/
29+
enum SimpleClassNameKeySpaceResolver implements KeySpaceResolver {
30+
31+
INSTANCE;
32+
33+
@Override
34+
public String resolveKeySpace(Class<?> type) {
35+
36+
Assert.notNull(type, "Type must not be null");
37+
return StringUtils.uncapitalize(ClassUtils.getUserClass(type).getSimpleName());
38+
}
39+
40+
}

spring-vault-core/src/main/java/org/springframework/vault/repository/mapping/VaultMappingContext.java

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
import org.springframework.data.mapping.model.Property;
2121
import org.springframework.data.mapping.model.SimpleTypeHolder;
2222
import org.springframework.data.util.TypeInformation;
23-
import org.springframework.util.Assert;
24-
import org.springframework.util.ClassUtils;
25-
import org.springframework.util.StringUtils;
23+
import org.springframework.lang.Nullable;
2624

2725
/**
2826
* Mapping context for {@link VaultPersistentEntity Vault-specific entities}.
@@ -32,10 +30,12 @@
3230
*/
3331
public class VaultMappingContext extends KeyValueMappingContext<VaultPersistentEntity<?>, VaultPersistentProperty> {
3432

35-
private KeySpaceResolver fallbackKeySpaceResolver = SimpleClassNameKeySpaceResolver.INSTANCE;
33+
@Nullable
34+
private KeySpaceResolver fallbackKeySpaceResolver;
3635

3736
public KeySpaceResolver getFallbackKeySpaceResolver() {
38-
return this.fallbackKeySpaceResolver;
37+
return this.fallbackKeySpaceResolver == null ? SimpleClassNameKeySpaceResolver.INSTANCE
38+
: this.fallbackKeySpaceResolver;
3939
}
4040

4141
@Override
@@ -54,23 +54,4 @@ protected VaultPersistentProperty createPersistentProperty(Property property, Va
5454
return new VaultPersistentProperty(property, owner, simpleTypeHolder);
5555
}
5656

57-
/**
58-
* Most trivial implementation of {@link KeySpaceResolver} returning the
59-
* {@link Class#getName()}.
60-
*
61-
* @author Mark Paluch
62-
*/
63-
enum SimpleClassNameKeySpaceResolver implements KeySpaceResolver {
64-
65-
INSTANCE;
66-
67-
@Override
68-
public String resolveKeySpace(Class<?> type) {
69-
70-
Assert.notNull(type, "Type must not be null");
71-
return StringUtils.uncapitalize(ClassUtils.getUserClass(type).getSimpleName());
72-
}
73-
74-
}
75-
7657
}

0 commit comments

Comments
 (0)