Skip to content

Commit fda78f4

Browse files
committed
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
1 parent 7f7e125 commit fda78f4

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

spring-vault-core/src/main/java/org/springframework/vault/repository/convert/MappingVaultConverter.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -409,21 +409,21 @@ protected void writeInternal(Object obj, SecretDocumentAccessor sink, @Nullable
409409

410410
protected void writeInternal(Object obj, SecretDocumentAccessor sink, VaultPersistentEntity<?> entity) {
411411

412-
PersistentPropertyAccessor accessor = entity.getPropertyAccessor(obj);
412+
PersistentPropertyAccessor<?> accessor = entity.getPropertyAccessor(obj);
413413

414414
VaultPersistentProperty idProperty = entity.getIdProperty();
415415
if (idProperty != null && !sink.hasValue(idProperty)) {
416416

417417
Object value = accessor.getProperty(idProperty);
418418

419419
if (value != null) {
420-
sink.put(idProperty, value);
420+
sink.put(idProperty, value instanceof String ? value : conversionService.convert(value, String.class));
421421
}
422422
}
423423
writeProperties(entity, accessor, sink, idProperty);
424424
}
425425

426-
private void writeProperties(VaultPersistentEntity<?> entity, PersistentPropertyAccessor accessor,
426+
private void writeProperties(VaultPersistentEntity<?> entity, PersistentPropertyAccessor<?> accessor,
427427
SecretDocumentAccessor sink, @Nullable VaultPersistentProperty idProperty) {
428428

429429
// Write the properties

spring-vault-core/src/test/java/org/springframework/vault/repository/convert/MappingVaultConverterUnitTests.java

+29
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.Objects;
24+
import java.util.UUID;
2425

2526
import org.junit.jupiter.api.BeforeEach;
2627
import org.junit.jupiter.api.Test;
2728

2829
import org.springframework.core.convert.converter.Converter;
30+
import org.springframework.data.annotation.Id;
2931
import org.springframework.data.annotation.Version;
3032
import org.springframework.vault.repository.mapping.VaultMappingContext;
3133

@@ -310,6 +312,19 @@ void shouldWriteEntityWithListOfEntities() {
310312
assertThat((List<Map<String, Object>>) sink.get("nested")).contains(walter, skyler);
311313
}
312314

315+
@Test
316+
void shouldConvertIdentifier() {
317+
318+
WithUuidId entity = new WithUuidId(UUID.randomUUID(), "foo");
319+
320+
SecretDocument sink = new SecretDocument();
321+
322+
this.converter.write(entity, sink);
323+
324+
assertThat(sink.getId()).isEqualTo(entity.id.toString());
325+
assertThat(sink.getBody()).containsEntry("name", "foo");
326+
}
327+
313328
static class SimpleEntity {
314329

315330
String id;
@@ -561,6 +576,20 @@ public String getName() {
561576

562577
}
563578

579+
static class WithUuidId {
580+
581+
@Id
582+
private final UUID id;
583+
584+
private final String name;
585+
586+
public WithUuidId(UUID id, String name) {
587+
this.id = id;
588+
this.name = name;
589+
}
590+
591+
}
592+
564593
enum DocumentToPersonConverter implements Converter<SecretDocument, Person> {
565594

566595
INSTANCE;

0 commit comments

Comments
 (0)