Skip to content

Commit d1779a5

Browse files
dharrigandaschl
authored andcommitted
DATACOUCH-38 - Can't Deserialize Enum.
Enums, even although they are a simple type, must not use writeSimpleObject since there is no "handler" in writeSimpleObject for Enums. Instead, we just drop back to using the built-in ObjectMapper for handling enums. -=david=-
1 parent fa39775 commit d1779a5

File tree

2 files changed

+77
-30
lines changed

2 files changed

+77
-30
lines changed

src/main/java/org/springframework/data/couchbase/core/convert/translation/JacksonTranslationService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private void encodeRecursive(final CouchbaseStorable source, final JsonGenerator
8888
continue;
8989
}
9090

91-
if (simpleTypeHolder.isSimpleType(value.getClass())) {
91+
if (simpleTypeHolder.isSimpleType(value.getClass()) && !Enum.class.isAssignableFrom(value.getClass())) {
9292
generator.writeObject(value);
9393
} else {
9494
ObjectMapper mapper = new ObjectMapper();

src/test/java/org/springframework/data/couchbase/core/CouchbaseTemplateTests.java

Lines changed: 76 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.couchbase.client.CouchbaseClient;
2020
import com.couchbase.client.protocol.views.Query;
2121
import com.couchbase.client.protocol.views.Stale;
22+
import com.fasterxml.jackson.annotation.JsonFormat;
2223
import org.junit.Test;
2324
import org.junit.runner.RunWith;
2425
import org.springframework.beans.factory.annotation.Autowired;
@@ -68,26 +69,26 @@ public void saveSimpleEntityCorrectly() throws Exception {
6869
String result = (String) client.get(id);
6970

7071
String expected = "{\"_class\":\"org.springframework.data.couchbase.core.Beer\""
71-
+ ",\"is_active\":false,\"name\":\"The Awesome Stout\"}";
72+
+ ",\"is_active\":false,\"name\":\"The Awesome Stout\"}";
7273
assertNotNull(result);
7374
assertEquals(expected, result);
7475
}
7576

7677
@Test
7778
public void saveDocumentWithExpiry() throws Exception {
78-
String id = "simple-doc-with-expiry";
79-
DocumentWithExpiry doc = new DocumentWithExpiry(id);
80-
template.save(doc);
81-
assertNotNull(client.get(id));
82-
Thread.sleep(3000);
83-
assertNull(client.get(id));
79+
String id = "simple-doc-with-expiry";
80+
DocumentWithExpiry doc = new DocumentWithExpiry(id);
81+
template.save(doc);
82+
assertNotNull(client.get(id));
83+
Thread.sleep(3000);
84+
assertNull(client.get(id));
8485
}
8586

8687
@Test
8788
public void insertDoesNotOverride() {
88-
String id ="double-insert-test";
89+
String id = "double-insert-test";
8990
String expected = "{\"_class\":\"org.springframework.data.couchbase.core."
90-
+ "CouchbaseTemplateTests$SimplePerson\",\"name\":\"Mr. A\"}";
91+
+ "CouchbaseTemplateTests$SimplePerson\",\"name\":\"Mr. A\"}";
9192

9293
SimplePerson doc = new SimplePerson(id, "Mr. A");
9394
template.insert(doc);
@@ -103,10 +104,10 @@ public void insertDoesNotOverride() {
103104

104105
@Test
105106
public void updateDoesNotInsert() {
106-
String id ="update-does-not-insert";
107-
SimplePerson doc = new SimplePerson(id, "Nice Guy");
108-
template.update(doc);
109-
assertNull(client.get(id));
107+
String id = "update-does-not-insert";
108+
SimplePerson doc = new SimplePerson(id, "Nice Guy");
109+
template.update(doc);
110+
assertNull(client.get(id));
110111
}
111112

112113

@@ -127,7 +128,7 @@ public void removeDocument() {
127128

128129
@Test
129130
public void storeListsAndMaps() {
130-
String id ="persons:lots-of-names";
131+
String id = "persons:lots-of-names";
131132
List<String> names = new ArrayList<String>();
132133
names.add("Michael");
133134
names.add("Thomas");
@@ -142,9 +143,9 @@ public void storeListsAndMaps() {
142143
template.save(complex);
143144

144145
String expected = "{\"_class\":\"org.springframework.data.couchbase.core."
145-
+ "CouchbaseTemplateTests$ComplexPerson\",\"info1\":{\"foo\":true,\"bar\""
146-
+ ":false},\"votes\":[],\"firstnames\":[\"Michael\",\"Thomas\"],\"info2\":"
147-
+ "{}}";
146+
+ "CouchbaseTemplateTests$ComplexPerson\",\"info1\":{\"foo\":true,\"bar\""
147+
+ ":false},\"votes\":[],\"firstnames\":[\"Michael\",\"Thomas\"],\"info2\":"
148+
+ "{}}";
148149
assertEquals(expected, client.get(id));
149150

150151
ComplexPerson response = template.findById(id, ComplexPerson.class);
@@ -180,7 +181,7 @@ public void shouldLoadAndMapViewDocs() {
180181
final List<Beer> beers = template.findByView("test_beers", "by_name", query, Beer.class);
181182
assertTrue(beers.size() > 0);
182183

183-
for(Beer beer : beers) {
184+
for (Beer beer : beers) {
184185
assertNotNull(beer.getId());
185186
assertNotNull(beer.getName());
186187
assertNotNull(beer.getActive());
@@ -194,7 +195,7 @@ public void shouldNotSaveNull() {
194195
try {
195196
template.save(things);
196197
fail("We should not be able to store a NULL!");
197-
} catch(final IllegalArgumentException e) {
198+
} catch (final IllegalArgumentException e) {
198199
assertTrue(true);
199200
}
200201
}
@@ -208,38 +209,50 @@ public void shouldDeserialiseLongs() {
208209
assertNotNull(simpleWithLong);
209210
assertEquals(time, simpleWithLong.getValue());
210211
}
211-
212+
213+
@Test
214+
public void shouldDeserialiseEnums() {
215+
SimpleWithEnum simpleWithEnum = new SimpleWithEnum("simpleWithEnum:enum", SimpleWithEnum.Type.BIG);
216+
template.save(simpleWithEnum);
217+
simpleWithEnum = template.findById("simpleWithEnum:enum", SimpleWithEnum.class);
218+
assertNotNull(simpleWithEnum);
219+
assertEquals(simpleWithEnum.getType(), SimpleWithEnum.Type.BIG);
220+
}
221+
212222
/**
213223
* A sample document with just an id and property.
214224
*/
215225
@Document
216226
static class SimplePerson {
227+
217228
@Id
218229
private final String id;
219230
@Field
220231
private final String name;
221232

222233
public SimplePerson(String id, String name) {
223-
this.id = id;
224-
this.name = name;
225-
}
234+
this.id = id;
235+
this.name = name;
236+
}
226237
}
227-
238+
228239
/**
229240
* A sample document that expires in 2 seconds.
230241
*/
231-
@Document(expiry=2)
242+
@Document(expiry = 2)
232243
static class DocumentWithExpiry {
244+
233245
@Id
234246
private final String id;
235-
247+
236248
public DocumentWithExpiry(String id) {
237-
this.id = id;
249+
this.id = id;
238250
}
239251
}
240252

241253
@Document
242254
static class ComplexPerson {
255+
243256
@Id
244257
private final String id;
245258
@Field
@@ -253,8 +266,8 @@ static class ComplexPerson {
253266
private final Map<String, Integer> info2;
254267

255268
public ComplexPerson(String id, List<String> firstnames,
256-
List<Integer> votes, Map<String, Boolean> info1,
257-
Map<String, Integer> info2) {
269+
List<Integer> votes, Map<String, Boolean> info1,
270+
Map<String, Integer> info2) {
258271
this.id = id;
259272
this.firstnames = firstnames;
260273
this.votes = votes;
@@ -308,4 +321,38 @@ void setValue(final long value) {
308321
this.value = value;
309322
}
310323
}
324+
325+
static class SimpleWithEnum {
326+
327+
@Id
328+
private String id;
329+
330+
private enum Type {
331+
BIG
332+
}
333+
334+
private Type type;
335+
336+
SimpleWithEnum(final String id, final Type type) {
337+
this.id = id;
338+
this.type = type;
339+
}
340+
341+
String getId() {
342+
return id;
343+
}
344+
345+
void setId(final String id) {
346+
this.id = id;
347+
}
348+
349+
Type getType() {
350+
return type;
351+
}
352+
353+
void setType(final Type type) {
354+
this.type = type;
355+
}
356+
}
357+
311358
}

0 commit comments

Comments
 (0)