Skip to content

Commit c62ad42

Browse files
author
Maciej Zasada
committed
DATACOUCH-27: Mapping @id field with MappingCouchbaseConverter
Fixed @id field rewriting from CouchbaseDocument into domain object. More info can be found at DATACOUCH-27 JIRA issue.
1 parent b489f09 commit c62ad42

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ target/
44
.classpath
55
.project
66
.settings/*
7+
8+
# IntelliJ IDEA
9+
*.iml
10+
.idea

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public <R> R read(Class<R> clazz, CouchbaseDocument doc) {
8282
protected <R> R read(TypeInformation<R> type, CouchbaseDocument doc) {
8383
return read(type, doc, null);
8484
}
85-
85+
8686
protected <R> R read(TypeInformation<R> type, final CouchbaseDocument source, Object parent) {
8787

8888
if (source == null) {
@@ -118,13 +118,17 @@ protected <R extends Object> R read(final CouchbasePersistentEntity<R> entity, f
118118

119119
entity.doWithProperties(new PropertyHandler<CouchbasePersistentProperty>() {
120120
public void doWithPersistentProperty(final CouchbasePersistentProperty prop) {
121-
if (!source.containsKey(prop.getFieldName()) || entity.isConstructorArgument(prop)) {
121+
if (!doesPropertyExistInSource(prop) || entity.isConstructorArgument(prop)) {
122122
return;
123123
}
124124

125125
Object obj = prop.isIdProperty() ? source.getId() : getValueInternal(prop, source, evaluator, result);
126126
wrapper.setProperty(prop, obj, useFieldAccessOnly);
127127
}
128+
129+
private boolean doesPropertyExistInSource(CouchbasePersistentProperty property) {
130+
return property.isIdProperty() || source.containsKey(property.getFieldName());
131+
}
128132
});
129133

130134
entity.doWithAssociations(new AssociationHandler<CouchbasePersistentProperty>() {
@@ -201,7 +205,6 @@ private Object getPotentiallyConvertedSimpleRead(Object value, Class<?> target)
201205
return target.isAssignableFrom(value.getClass()) ? value : conversionService.convert(value, target);
202206
}
203207

204-
205208
@Override
206209
public void write(final Object source, final CouchbaseDocument target) {
207210
if (source == null) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,8 @@ public class Beer {
9090

9191
String description;
9292

93+
public String getId() {
94+
return id;
95+
}
9396
}
9497
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public void needsIDOnEntity() {
6969
new CouchbaseDocument());
7070
}
7171

72-
7372
@Test
7473
public void writesString() {
7574
CouchbaseDocument converted = new CouchbaseDocument();
@@ -94,7 +93,6 @@ public void readsString() {
9493
assertEquals("foobar", converted.attr0);
9594
}
9695

97-
9896
@Test
9997
public void writesNumber() {
10098
CouchbaseDocument converted = new CouchbaseDocument();
@@ -119,7 +117,6 @@ public void readsNumber() {
119117
assertEquals(42, converted.attr0);
120118
}
121119

122-
123120
@Test
124121
public void writesBoolean() {
125122
CouchbaseDocument converted = new CouchbaseDocument();
@@ -176,6 +173,18 @@ public void readsMixedSimpleTypes() {
176173
assertTrue(converted.attr3);
177174
}
178175

176+
@Test
177+
public void readsID() {
178+
// given:
179+
CouchbaseDocument document = new CouchbaseDocument("001");
180+
181+
// when:
182+
BasicCouchbasePersistentPropertyTests.Beer beer = converter.read(BasicCouchbasePersistentPropertyTests.Beer.class,
183+
document);
184+
185+
// then:
186+
assertEquals("001", beer.getId());
187+
}
179188

180189
@Test
181190
public void writesUninitializedValues() {
@@ -336,7 +345,6 @@ public void writesAndReadsSetAndNestedSet() {
336345
assertEquals(attr2, readConverted.attr2);
337346
}
338347

339-
340348
@Test
341349
public void writesAndReadsValueClass() {
342350
CouchbaseDocument converted = new CouchbaseDocument();

0 commit comments

Comments
 (0)