Skip to content

Commit 8ade609

Browse files
committed
Merge pull request #5 from mzasada/DATACOUCH-27
DATACOUCH-27: Mapping @id field with MappingCouchbaseConverter
2 parents 6896195 + c4aa364 commit 8ade609

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ target/
33

44
.classpath
55
.project
6-
.settings/*
6+
.settings
77

88
*.iml
9-
.idea/*
9+
.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: 9 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,15 @@ public void readsMixedSimpleTypes() {
176173
assertTrue(converted.attr3);
177174
}
178175

176+
@Test
177+
public void readsID() {
178+
CouchbaseDocument document = new CouchbaseDocument("001");
179+
180+
BasicCouchbasePersistentPropertyTests.Beer beer = converter.read(BasicCouchbasePersistentPropertyTests.Beer.class,
181+
document);
182+
183+
assertEquals("001", beer.getId());
184+
}
179185

180186
@Test
181187
public void writesUninitializedValues() {
@@ -336,7 +342,6 @@ public void writesAndReadsSetAndNestedSet() {
336342
assertEquals(attr2, readConverted.attr2);
337343
}
338344

339-
340345
@Test
341346
public void writesAndReadsValueClass() {
342347
CouchbaseDocument converted = new CouchbaseDocument();

0 commit comments

Comments
 (0)