Skip to content

Commit b007c94

Browse files
committed
added keepNull & mergeObjects parameters for insert-update document
1 parent f7adff3 commit b007c94

File tree

3 files changed

+100
-8
lines changed

3 files changed

+100
-8
lines changed

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,13 @@ protected <T> Request insertDocumentRequest(final T value, final DocumentCreateO
8888
request.putQueryParam(SILENT, params.getSilent());
8989
request.putQueryParam(OVERWRITE, params.getOverwrite());
9090
request.putQueryParam(OVERWRITE_MODE, params.getOverwriteMode() != null ? params.getOverwriteMode().getValue() : null);
91+
request.putQueryParam(ArangoRequestParam.KEEP_NULL, params.getKeepNull());
92+
request.putQueryParam(MERGE_OBJECTS, params.getMergeObjects());
9193
request.putHeaderParam(TRANSACTION_ID, params.getStreamTransactionId());
92-
request.setBody(util(Serializer.CUSTOM).serialize(value));
94+
95+
boolean serializeNullValues = OverwriteMode.update.equals(params.getOverwriteMode());
96+
request.setBody(util(Serializer.CUSTOM).serialize(value, new ArangoSerializer.Options().serializeNullValues(serializeNullValues)));
97+
9398
return request;
9499
}
95100

@@ -125,9 +130,13 @@ protected <T> Request insertDocumentsRequest(final Collection<T> values, final D
125130
request.putQueryParam(SILENT, params.getSilent());
126131
request.putQueryParam(OVERWRITE, params.getOverwrite());
127132
request.putQueryParam(OVERWRITE_MODE, params.getOverwriteMode() != null ? params.getOverwriteMode().getValue() : null);
133+
request.putQueryParam(ArangoRequestParam.KEEP_NULL, params.getKeepNull());
134+
request.putQueryParam(MERGE_OBJECTS, params.getMergeObjects());
128135
request.putHeaderParam(TRANSACTION_ID, params.getStreamTransactionId());
129-
request.setBody(util(Serializer.CUSTOM)
130-
.serialize(values, new ArangoSerializer.Options().serializeNullValues(false).stringAsJson(true)));
136+
137+
boolean serializeNullValues = OverwriteMode.update.equals(params.getOverwriteMode());
138+
request.setBody(util(Serializer.CUSTOM).serialize(values, new ArangoSerializer.Options()
139+
.serializeNullValues(serializeNullValues).stringAsJson(true)));
131140
return request;
132141
}
133142

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

+37
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public class DocumentCreateOptions {
3535
private OverwriteMode overwriteMode;
3636
private Boolean silent;
3737
private String streamTransactionId;
38+
private Boolean keepNull;
39+
private Boolean mergeObjects;
40+
3841

3942
public DocumentCreateOptions() {
4043
super();
@@ -141,4 +144,38 @@ public DocumentCreateOptions streamTransactionId(final String streamTransactionI
141144
return this;
142145
}
143146

147+
public Boolean getKeepNull() {
148+
return keepNull;
149+
}
150+
151+
/**
152+
* @param keepNull If the intention is to delete existing attributes with the patch command, the URL query parameter
153+
* keepNull can be used with a value of false. This will modify the behavior of the patch command to
154+
* remove any attributes from the existing document that are contained in the patch document with an
155+
* attribute value of null.
156+
* @return options
157+
* @apiNote only considered if {@link this#overwriteMode} is set to {@link OverwriteMode#update}
158+
* @apiNote the document key field must be not null
159+
*/
160+
public DocumentCreateOptions keepNull(Boolean keepNull) {
161+
this.keepNull = keepNull;
162+
return this;
163+
}
164+
165+
public Boolean getMergeObjects() {
166+
return mergeObjects;
167+
}
168+
169+
/**
170+
* @param mergeObjects Controls whether objects (not arrays) will be merged if present in both the existing and the patch
171+
* document. If set to false, the value in the patch document will overwrite the existing document's
172+
* value. If set to true, objects will be merged. The default is true.
173+
* @return options
174+
* @apiNote only considered if {@link this#overwriteMode} is set to {@link OverwriteMode#update}
175+
*/
176+
public DocumentCreateOptions mergeObjects(Boolean mergeObjects) {
177+
this.mergeObjects = mergeObjects;
178+
return this;
179+
}
180+
144181
}

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

+51-5
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,59 @@ public void insertDocumentOverwriteModeUpdate() {
224224
final DocumentCreateEntity<BaseDocument> meta = collection.insertDocument(doc);
225225

226226
doc.addAttribute("bar", "b");
227-
final DocumentCreateEntity<BaseDocument> upsert = collection
227+
final DocumentCreateEntity<BaseDocument> updated = collection
228228
.insertDocument(doc, new DocumentCreateOptions().overwriteMode(OverwriteMode.update).returnNew(true));
229229

230-
assertThat(upsert, is(notNullValue()));
231-
assertThat(upsert.getRev(), is(not(meta.getRev())));
232-
assertThat(upsert.getNew().getAttribute("foo").toString(), is("a"));
233-
assertThat(upsert.getNew().getAttribute("bar").toString(), is("b"));
230+
assertThat(updated, is(notNullValue()));
231+
assertThat(updated.getRev(), is(not(meta.getRev())));
232+
assertThat(updated.getNew().getAttribute("foo").toString(), is("a"));
233+
assertThat(updated.getNew().getAttribute("bar").toString(), is("b"));
234+
}
235+
236+
@Test
237+
public void insertDocumentOverwriteModeUpdateKeepNullTrue() {
238+
assumeTrue(isAtLeastVersion(3, 7));
239+
240+
final BaseDocument initialDoc = new BaseDocument();
241+
initialDoc.addAttribute("foo", "a");
242+
final DocumentCreateEntity<BaseDocument> meta = collection.insertDocument(initialDoc);
243+
244+
final BaseDocument updatedDoc = new BaseDocument(initialDoc.getKey());
245+
updatedDoc.addAttribute("foo", null);
246+
updatedDoc.addAttribute("bar", "b");
247+
final DocumentCreateEntity<BaseDocument> updated = collection
248+
.insertDocument(updatedDoc, new DocumentCreateOptions()
249+
.overwriteMode(OverwriteMode.update)
250+
.keepNull(true)
251+
.returnNew(true));
252+
253+
assertThat(updated, is(notNullValue()));
254+
assertThat(updated.getRev(), is(not(meta.getRev())));
255+
assertThat(updated.getNew().getProperties().containsKey("foo"), is(true));
256+
assertThat(updated.getNew().getAttribute("foo"), is(nullValue()));
257+
assertThat(updated.getNew().getAttribute("bar").toString(), is("b"));
258+
}
259+
260+
@Test
261+
public void insertDocumentOverwriteModeUpdateMergeObjectsFalse() {
262+
assumeTrue(isAtLeastVersion(3, 7));
263+
264+
final BaseDocument doc = new BaseDocument();
265+
Map<String, String> fieldA = Collections.singletonMap("a", "a");
266+
doc.addAttribute("foo", fieldA);
267+
final DocumentCreateEntity<BaseDocument> meta = collection.insertDocument(doc);
268+
269+
Map<String, String> fieldB = Collections.singletonMap("b", "b");
270+
doc.addAttribute("foo", fieldB);
271+
final DocumentCreateEntity<BaseDocument> updated = collection
272+
.insertDocument(doc, new DocumentCreateOptions()
273+
.overwriteMode(OverwriteMode.update)
274+
.mergeObjects(false)
275+
.returnNew(true));
276+
277+
assertThat(updated, is(notNullValue()));
278+
assertThat(updated.getRev(), is(not(meta.getRev())));
279+
assertThat(updated.getNew().getAttribute("foo"), is(fieldB));
234280
}
235281

236282
@Test

0 commit comments

Comments
 (0)