Skip to content

Commit 270f573

Browse files
committed
Merge pull request #9 from dharrigan/DATACOUCH-36
DATACOUCH-36 - Don't allow NULL values to be stored.
2 parents 740ddc9 + 0323d1f commit 270f573

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/main/java/org/springframework/data/couchbase/core/mapping/CouchbaseDocument.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public CouchbaseDocument(final String id, final int expiration) {
104104
* @return the {@link CouchbaseDocument} for chaining.
105105
*/
106106
public final CouchbaseDocument put(final String key, final Object value) {
107-
verifyValueType(value.getClass());
107+
verifyValueType(value);
108108

109109
payload.put(key, value);
110110
return this;
@@ -259,15 +259,19 @@ public CouchbaseDocument setId(String id) {
259259
* <p>If this is not the case, a {@link IllegalArgumentException} is
260260
* thrown.</p>
261261
*
262-
* @param clazz the class type to check and verify.
262+
* Objects that are NULL cannot be stored.
263+
*
264+
* @param value the object to verify its type.
263265
*/
264-
private void verifyValueType(final Class<?> clazz) {
266+
private void verifyValueType(final Object value) {
267+
if(value == null) {
268+
throw new IllegalArgumentException("Attribute of type null cannot be stored.");
269+
}
270+
final Class<?> clazz = value.getClass();
265271
if (simpleTypeHolder.isSimpleType(clazz)) {
266-
return;
272+
return;
267273
}
268-
269-
throw new IllegalArgumentException("Attribute of type "
270-
+ clazz.getCanonicalName() + " can not be stored and must be converted.");
274+
throw new IllegalArgumentException("Attribute of type " + clazz.getCanonicalName() + " cannot be stored and must be converted.");
271275
}
272276

273277
/**

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@ public void shouldLoadAndMapViewDocs() {
179179
assertNotNull(beer.getActive());
180180
}
181181
}
182+
183+
@Test
184+
public void shouldNotSaveNull() {
185+
final Map<String, String> things = new HashMap<String, String>();
186+
things.put("key", null);
187+
try {
188+
template.save(things);
189+
fail("We should not be able to store a NULL!");
190+
} catch(final IllegalArgumentException e) {
191+
assertTrue(true);
192+
}
193+
}
182194

183195
/**
184196
* A sample document with just an id and property.

0 commit comments

Comments
 (0)