Skip to content

Commit a2a48ca

Browse files
authored
Allow document ID to be other than String (#1534)
* fixes #1529 * add author
1 parent 48742b1 commit a2a48ca

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
* @author Geoffrey Mina
8282
* @author Mark Paluch
8383
* @author Michael Reiche
84+
* @author Remi Bleuse
8485
*/
8586
public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implements ApplicationContextAware {
8687

@@ -955,7 +956,7 @@ public <R> R getPropertyValue(final CouchbasePersistentProperty property) {
955956
Object value = expression != null ? evaluator.evaluate(expression) : source.get(property.getFieldName());
956957

957958
if (property == entity.getIdProperty() && parent == null) {
958-
return (R) source.getId();
959+
return readValue(source.getId(), property.getTypeInformation(), source);
959960
}
960961
if (value == null) {
961962
return null;

src/test/java/org/springframework/data/couchbase/core/mapping/MappingCouchbaseConverterTests.java

+29
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,25 @@ void writesAndReadsDates() {
686686
assertThat(read.deleted.truncatedTo(ChronoUnit.MILLIS)).isEqualTo(deleted.truncatedTo(ChronoUnit.MILLIS));
687687
}
688688

689+
@Test
690+
void writesAndReadsIdAsUUID() {
691+
String idAsString = "fc1cc4c4-b7ea-4cb4-b43a-3fb9f574d123";
692+
UUID id = UUID.fromString(idAsString);
693+
String otherAsString = "fc1cc4c4-b7ea-4cb4-b43a-3fb9f574d456";
694+
UUID other = UUID.fromString(otherAsString);
695+
696+
UuidIdEntity entity = new UuidIdEntity(id, other);
697+
698+
CouchbaseDocument converted = new CouchbaseDocument();
699+
converter.write(entity, converted);
700+
assertThat(converted.getContent().get("other")).isEqualTo(otherAsString);
701+
assertThat(converted.getId()).isEqualTo(idAsString);
702+
703+
UuidIdEntity read = converter.read(UuidIdEntity.class, converted);
704+
assertThat(read.id).isEqualTo(id);
705+
assertThat(read.other).isEqualTo(other);
706+
}
707+
689708
@Test
690709
void writesAndReadsNestedClass() {
691710
CouchbaseDocument converted = new CouchbaseDocument();
@@ -946,6 +965,16 @@ public DateEntity(Date created, Calendar modified, LocalDateTime deleted) {
946965
}
947966
}
948967

968+
static class UuidIdEntity {
969+
@Id private UUID id;
970+
private UUID other;
971+
972+
public UuidIdEntity(UUID id, UUID other) {
973+
this.id = id;
974+
this.other = other;
975+
}
976+
}
977+
949978
@Test
950979
void idNotGenerated() {
951980
class Entity {

0 commit comments

Comments
 (0)