Skip to content

Commit 3bf9895

Browse files
author
Mark
committed
added option serializeNull in DocumentUpdateOptions
1 parent bfa3e53 commit 3bf9895

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ v4.1.1 (2016-xx-xx)
44
* fixed VelocyPack bug with non-ASCII characters
55
* added missing replicationFactor in CollectionCreateOptions
66
* added missing replicationFactor in CollectionPropertiesEntity
7+
* added option serializeNull in DocumentUpdateOptions
78

89
v4.1.0 (2016-10-28)
910
---------------------------

src/main/java/com/arangodb/internal/InternalArangoCollection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public <T> Request updateDocumentRequest(final String key, final T value, final
271271
request.putQueryParam(ArangoDBConstants.RETURN_NEW, params.getReturnNew());
272272
request.putQueryParam(ArangoDBConstants.RETURN_OLD, params.getReturnOld());
273273
request.putHeaderParam(ArangoDBConstants.IF_MATCH, params.getIfMatch());
274-
request.setBody(executor.serialize(value, true));
274+
request.setBody(executor.serialize(value, params.getSerializeNull() == null || params.getSerializeNull()));
275275
return request;
276276
}
277277

src/main/java/com/arangodb/model/DocumentUpdateOptions.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class DocumentUpdateOptions {
3535
private String ifMatch;
3636
private Boolean returnNew;
3737
private Boolean returnOld;
38+
private Boolean serializeNull;
3839

3940
public DocumentUpdateOptions() {
4041
super();
@@ -146,4 +147,20 @@ public DocumentUpdateOptions returnOld(final Boolean returnOld) {
146147
return this;
147148
}
148149

150+
public Boolean getSerializeNull() {
151+
return serializeNull;
152+
}
153+
154+
/**
155+
* @param serializeNull
156+
* By default, or if this is set to true, all fields of the document which have null values are
157+
* serialized to VelocyPack otherwise they are excluded from serialization. Use this to update single
158+
* fields from a stored document.
159+
* @return options
160+
*/
161+
public DocumentUpdateOptions serializeNull(final Boolean serializeNull) {
162+
this.serializeNull = serializeNull;
163+
return this;
164+
}
165+
149166
}

src/test/java/com/arangodb/ArangoCollectionTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import static org.hamcrest.Matchers.anyOf;
2424
import static org.hamcrest.Matchers.hasItem;
25+
import static org.hamcrest.Matchers.hasItems;
2526
import static org.hamcrest.Matchers.instanceOf;
2627
import static org.hamcrest.Matchers.is;
2728
import static org.hamcrest.Matchers.not;
@@ -364,6 +365,52 @@ public void updateDocumentKeepNullFalse() {
364365
assertThat(readResult.getProperties().keySet(), not(hasItem("a")));
365366
}
366367

368+
private static class TestUpdateEntity {
369+
@SuppressWarnings("unused")
370+
private String a, b;
371+
}
372+
373+
@Test
374+
public void updateDocumentSerializeNullTrue() {
375+
final TestUpdateEntity doc = new TestUpdateEntity();
376+
doc.a = "foo";
377+
doc.b = "foo";
378+
final DocumentCreateEntity<TestUpdateEntity> createResult = db.collection(COLLECTION_NAME).insertDocument(doc);
379+
final TestUpdateEntity patchDoc = new TestUpdateEntity();
380+
patchDoc.a = "bar";
381+
final DocumentUpdateEntity<TestUpdateEntity> updateResult = db.collection(COLLECTION_NAME)
382+
.updateDocument(createResult.getKey(), patchDoc);
383+
assertThat(updateResult, is(notNullValue()));
384+
assertThat(updateResult.getKey(), is(createResult.getKey()));
385+
386+
final BaseDocument readResult = db.collection(COLLECTION_NAME).getDocument(createResult.getKey(),
387+
BaseDocument.class);
388+
assertThat(readResult.getKey(), is(createResult.getKey()));
389+
assertThat(readResult.getProperties().keySet(), hasItem("a"));
390+
assertThat(readResult.getAttribute("a").toString(), is("bar"));
391+
}
392+
393+
@Test
394+
public void updateDocumentSerializeNullFalse() {
395+
final TestUpdateEntity doc = new TestUpdateEntity();
396+
doc.a = "foo";
397+
doc.b = "foo";
398+
final DocumentCreateEntity<TestUpdateEntity> createResult = db.collection(COLLECTION_NAME).insertDocument(doc);
399+
final TestUpdateEntity patchDoc = new TestUpdateEntity();
400+
patchDoc.a = "bar";
401+
final DocumentUpdateEntity<TestUpdateEntity> updateResult = db.collection(COLLECTION_NAME)
402+
.updateDocument(createResult.getKey(), patchDoc, new DocumentUpdateOptions().serializeNull(false));
403+
assertThat(updateResult, is(notNullValue()));
404+
assertThat(updateResult.getKey(), is(createResult.getKey()));
405+
406+
final BaseDocument readResult = db.collection(COLLECTION_NAME).getDocument(createResult.getKey(),
407+
BaseDocument.class);
408+
assertThat(readResult.getKey(), is(createResult.getKey()));
409+
assertThat(readResult.getProperties().keySet(), hasItems("a", "b"));
410+
assertThat(readResult.getAttribute("a").toString(), is("bar"));
411+
assertThat(readResult.getAttribute("b").toString(), is("foo"));
412+
}
413+
367414
@SuppressWarnings("unchecked")
368415
@Test
369416
public void updateDocumentMergeObjectsTrue() {

0 commit comments

Comments
 (0)