Skip to content

Commit

Permalink
Convert identifier to String when writing entity to SecretDocument
Browse files Browse the repository at this point in the history
We now properly convert the identifier value into String before writing it to SecretDocument.

Closes gh-777
  • Loading branch information
mp911de committed Apr 19, 2023
1 parent 2939169 commit 281338a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -409,21 +409,21 @@ protected void writeInternal(Object obj, SecretDocumentAccessor sink, @Nullable

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)) {

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
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;

Expand Down Expand Up @@ -310,6 +312,19 @@ void shouldWriteEntityWithListOfEntities() {
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;
Expand Down Expand Up @@ -561,6 +576,20 @@ public String getName() {

}

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;
Expand Down

0 comments on commit 281338a

Please sign in to comment.