Skip to content

Commit e590215

Browse files
authored
Allow Id other than String. (#1535)
Closes #1529.
1 parent 34646c1 commit e590215

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
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

@@ -962,7 +963,7 @@ public <R> R getPropertyValue(final CouchbasePersistentProperty property) {
962963
Object value = expression != null ? evaluator.evaluate(expression) : source.get(property.getFieldName());
963964

964965
if (property == entity.getIdProperty() && parent == null) {
965-
return (R) source.getId();
966+
return readValue(source.getId(), property.getTypeInformation(), source);
966967
}
967968
if (value == null) {
968969
return null;

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

Lines changed: 29 additions & 0 deletions
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)