From a2a48caf79a7c8ce3d08690ce2e13c293468fc0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Bleuse?= Date: Tue, 9 Aug 2022 02:05:29 +0900 Subject: [PATCH] Allow document ID to be other than String (#1534) * fixes #1529 * add author --- .../convert/MappingCouchbaseConverter.java | 3 +- .../MappingCouchbaseConverterTests.java | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java b/src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java index 8dff2e25..dac2c5c2 100644 --- a/src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java +++ b/src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java @@ -81,6 +81,7 @@ import org.springframework.util.CollectionUtils; * @author Geoffrey Mina * @author Mark Paluch * @author Michael Reiche + * @author Remi Bleuse */ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implements ApplicationContextAware { @@ -955,7 +956,7 @@ public class MappingCouchbaseConverter extends AbstractCouchbaseConverter implem Object value = expression != null ? evaluator.evaluate(expression) : source.get(property.getFieldName()); if (property == entity.getIdProperty() && parent == null) { - return (R) source.getId(); + return readValue(source.getId(), property.getTypeInformation(), source); } if (value == null) { return null; diff --git a/src/test/java/org/springframework/data/couchbase/core/mapping/MappingCouchbaseConverterTests.java b/src/test/java/org/springframework/data/couchbase/core/mapping/MappingCouchbaseConverterTests.java index 5af38dbe..6157dfca 100644 --- a/src/test/java/org/springframework/data/couchbase/core/mapping/MappingCouchbaseConverterTests.java +++ b/src/test/java/org/springframework/data/couchbase/core/mapping/MappingCouchbaseConverterTests.java @@ -686,6 +686,25 @@ public class MappingCouchbaseConverterTests { assertThat(read.deleted.truncatedTo(ChronoUnit.MILLIS)).isEqualTo(deleted.truncatedTo(ChronoUnit.MILLIS)); } + @Test + void writesAndReadsIdAsUUID() { + String idAsString = "fc1cc4c4-b7ea-4cb4-b43a-3fb9f574d123"; + UUID id = UUID.fromString(idAsString); + String otherAsString = "fc1cc4c4-b7ea-4cb4-b43a-3fb9f574d456"; + UUID other = UUID.fromString(otherAsString); + + UuidIdEntity entity = new UuidIdEntity(id, other); + + CouchbaseDocument converted = new CouchbaseDocument(); + converter.write(entity, converted); + assertThat(converted.getContent().get("other")).isEqualTo(otherAsString); + assertThat(converted.getId()).isEqualTo(idAsString); + + UuidIdEntity read = converter.read(UuidIdEntity.class, converted); + assertThat(read.id).isEqualTo(id); + assertThat(read.other).isEqualTo(other); + } + @Test void writesAndReadsNestedClass() { CouchbaseDocument converted = new CouchbaseDocument(); @@ -946,6 +965,16 @@ public class MappingCouchbaseConverterTests { } } + static class UuidIdEntity { + @Id private UUID id; + private UUID other; + + public UuidIdEntity(UUID id, UUID other) { + this.id = id; + this.other = other; + } + } + @Test void idNotGenerated() { class Entity {