Convert identifier to String when writing entity to SecretDocument
We now properly convert the identifier value into String before writing it to SecretDocument. Closes gh-777
This commit is contained in:
@@ -409,7 +409,7 @@ public class MappingVaultConverter extends AbstractVaultConverter {
|
||||
|
||||
protected void writeInternal(Object obj, SecretDocumentAccessor sink, VaultPersistentEntity<?> entity) {
|
||||
|
||||
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(obj);
|
||||
PersistentPropertyAccessor<?> accessor = entity.getPropertyAccessor(obj);
|
||||
|
||||
VaultPersistentProperty idProperty = entity.getIdProperty();
|
||||
if (idProperty != null && !sink.hasValue(idProperty)) {
|
||||
@@ -417,13 +417,13 @@ public class MappingVaultConverter extends AbstractVaultConverter {
|
||||
Object value = accessor.getProperty(idProperty);
|
||||
|
||||
if (value != null) {
|
||||
sink.put(idProperty, value);
|
||||
sink.put(idProperty, value instanceof String ? value : conversionService.convert(value, String.class));
|
||||
}
|
||||
}
|
||||
writeProperties(entity, accessor, sink, idProperty);
|
||||
}
|
||||
|
||||
private void writeProperties(VaultPersistentEntity<?> entity, PersistentPropertyAccessor accessor,
|
||||
private void writeProperties(VaultPersistentEntity<?> entity, PersistentPropertyAccessor<?> accessor,
|
||||
SecretDocumentAccessor sink, @Nullable VaultPersistentProperty idProperty) {
|
||||
|
||||
// Write the properties
|
||||
|
||||
@@ -21,11 +21,13 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.Version;
|
||||
import org.springframework.vault.repository.mapping.VaultMappingContext;
|
||||
|
||||
@@ -310,6 +312,19 @@ class MappingVaultConverterUnitTests {
|
||||
assertThat((List<Map<String, Object>>) sink.get("nested")).contains(walter, skyler);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldConvertIdentifier() {
|
||||
|
||||
WithUuidId entity = new WithUuidId(UUID.randomUUID(), "foo");
|
||||
|
||||
SecretDocument sink = new SecretDocument();
|
||||
|
||||
this.converter.write(entity, sink);
|
||||
|
||||
assertThat(sink.getId()).isEqualTo(entity.id.toString());
|
||||
assertThat(sink.getBody()).containsEntry("name", "foo");
|
||||
}
|
||||
|
||||
static class SimpleEntity {
|
||||
|
||||
String id;
|
||||
@@ -561,6 +576,20 @@ class MappingVaultConverterUnitTests {
|
||||
|
||||
}
|
||||
|
||||
static class WithUuidId {
|
||||
|
||||
@Id
|
||||
private final UUID id;
|
||||
|
||||
private final String name;
|
||||
|
||||
public WithUuidId(UUID id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
enum DocumentToPersonConverter implements Converter<SecretDocument, Person> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
Reference in New Issue
Block a user