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
This commit is contained in:
Mark Paluch
2023-06-20 14:59:39 +02:00
parent 5c1abe82c0
commit fc24a30398
3 changed files with 70 additions and 27 deletions

View File

@@ -15,6 +15,9 @@
*/
package org.springframework.vault.repository.mapping;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.data.keyvalue.annotation.KeySpace;
import org.springframework.data.keyvalue.core.mapping.BasicKeyValuePersistentEntity;
import org.springframework.data.keyvalue.core.mapping.KeySpaceResolver;
import org.springframework.data.util.TypeInformation;
@@ -43,10 +46,29 @@ public class BasicVaultPersistentEntity<T> extends BasicKeyValuePersistentEntity
/**
* Creates new {@link BasicVaultPersistentEntity}.
* @param information must not be {@literal null}.
* @param fallbackKeySpaceResolver can be {@literal null}.
* @param keySpaceResolver can be {@literal null}.
*/
public BasicVaultPersistentEntity(TypeInformation<T> information, KeySpaceResolver fallbackKeySpaceResolver) {
super(information, fallbackKeySpaceResolver);
public BasicVaultPersistentEntity(TypeInformation<T> information, @Nullable KeySpaceResolver keySpaceResolver) {
super(information, type -> {
if (keySpaceResolver != null) {
return keySpaceResolver.resolveKeySpace(type);
}
MergedAnnotation<KeySpace> annotation = MergedAnnotations
.from(type, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.get(KeySpace.class);
if (annotation.isPresent()
&& StringUtils.hasText(annotation.getValue("value").map(Object::toString).orElse(null))) {
// fallback to use keyspace resolution and SpEL expression handling of
// BasicKeyValuePersistentEntity.
return null;
}
return SimpleClassNameKeySpaceResolver.INSTANCE.resolveKeySpace(type);
});
Secret annotation = findAnnotation(Secret.class);

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.vault.repository.mapping;
import org.springframework.data.keyvalue.core.mapping.KeySpaceResolver;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
/**
* Most trivial implementation of {@link KeySpaceResolver} returning the
* {@link Class#getName()}.
*
* @author Mark Paluch
*/
enum SimpleClassNameKeySpaceResolver implements KeySpaceResolver {
INSTANCE;
@Override
public String resolveKeySpace(Class<?> type) {
Assert.notNull(type, "Type must not be null");
return StringUtils.uncapitalize(ClassUtils.getUserClass(type).getSimpleName());
}
}

View File

@@ -20,9 +20,7 @@ import org.springframework.data.keyvalue.core.mapping.context.KeyValueMappingCon
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.TypeInformation;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import org.springframework.lang.Nullable;
/**
* Mapping context for {@link VaultPersistentEntity Vault-specific entities}.
@@ -32,10 +30,12 @@ import org.springframework.util.StringUtils;
*/
public class VaultMappingContext extends KeyValueMappingContext<VaultPersistentEntity<?>, VaultPersistentProperty> {
private KeySpaceResolver fallbackKeySpaceResolver = SimpleClassNameKeySpaceResolver.INSTANCE;
@Nullable
private KeySpaceResolver fallbackKeySpaceResolver;
public KeySpaceResolver getFallbackKeySpaceResolver() {
return this.fallbackKeySpaceResolver;
return this.fallbackKeySpaceResolver == null ? SimpleClassNameKeySpaceResolver.INSTANCE
: this.fallbackKeySpaceResolver;
}
@Override
@@ -54,23 +54,4 @@ public class VaultMappingContext extends KeyValueMappingContext<VaultPersistentE
return new VaultPersistentProperty(property, owner, simpleTypeHolder);
}
/**
* Most trivial implementation of {@link KeySpaceResolver} returning the
* {@link Class#getName()}.
*
* @author Mark Paluch
*/
enum SimpleClassNameKeySpaceResolver implements KeySpaceResolver {
INSTANCE;
@Override
public String resolveKeySpace(Class<?> type) {
Assert.notNull(type, "Type must not be null");
return StringUtils.uncapitalize(ClassUtils.getUserClass(type).getSimpleName());
}
}
}