diff --git a/spring-vault-core/src/main/java/org/springframework/vault/repository/convert/MappingVaultConverter.java b/spring-vault-core/src/main/java/org/springframework/vault/repository/convert/MappingVaultConverter.java index 56903080..d56dec9c 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/repository/convert/MappingVaultConverter.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/repository/convert/MappingVaultConverter.java @@ -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 diff --git a/spring-vault-core/src/test/java/org/springframework/vault/repository/convert/MappingVaultConverterUnitTests.java b/spring-vault-core/src/test/java/org/springframework/vault/repository/convert/MappingVaultConverterUnitTests.java index 26cebe9e..00ddd8ef 100644 --- a/spring-vault-core/src/test/java/org/springframework/vault/repository/convert/MappingVaultConverterUnitTests.java +++ b/spring-vault-core/src/test/java/org/springframework/vault/repository/convert/MappingVaultConverterUnitTests.java @@ -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>) 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 { INSTANCE;