Skip to content

Commit

Permalink
Handle null in fetchSource includes/excludes
Browse files Browse the repository at this point in the history
This change ensures that when null is passed as the value to one of
its Nullable parameters, it is not wrapped in a String array. That
would in turn cause a NPE when attempting to serialize FetchSourceContext
as its constructor checks for null values only.

In master, the problematic behavior was corrected as part of elastic#29293
  • Loading branch information
jkakavas committed Dec 19, 2018
1 parent b54783d commit 5148f1f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,9 @@ public UpdateRequest fields(String... fields) {
*/
public UpdateRequest fetchSource(@Nullable String include, @Nullable String exclude) {
FetchSourceContext context = this.fetchSourceContext == null ? FetchSourceContext.FETCH_SOURCE : this.fetchSourceContext;
this.fetchSourceContext = new FetchSourceContext(context.fetchSource(), new String[] {include}, new String[]{exclude});
String[] includes = include == null ? Strings.EMPTY_ARRAY : new String[]{include};
String[] excludes = exclude == null ? Strings.EMPTY_ARRAY : new String[]{exclude};
this.fetchSourceContext = new FetchSourceContext(context.fetchSource(), includes, excludes);
return this;
}

Expand Down
28 changes: 28 additions & 0 deletions server/src/test/java/org/elasticsearch/update/UpdateIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,34 @@ public void testUpdate() throws Exception {
assertThat(updateResponse.getGetResult().sourceAsMap().size(), equalTo(1));
assertThat(updateResponse.getGetResult().sourceAsMap().get("field1"), equalTo(2));

// check updates with null excludes
client().prepareIndex("test", "type1", "1").setSource("field1", 1, "field2", 2).execute().actionGet();
updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1")
.setScript(new Script(ScriptType.INLINE, UPDATE_SCRIPTS, FIELD_INC_SCRIPT, Collections.singletonMap("field", "field1")))
.setFetchSource("field1", null)
.get();
assertThat(updateResponse.getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult(), notNullValue());
assertThat(updateResponse.getGetResult().getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult().sourceRef(), notNullValue());
assertThat(updateResponse.getGetResult().field("field1"), nullValue());
assertThat(updateResponse.getGetResult().sourceAsMap().size(), equalTo(1));
assertThat(updateResponse.getGetResult().sourceAsMap().get("field1"), equalTo(2));

// check updates with null includes
client().prepareIndex("test", "type1", "1").setSource("field1", 1, "field2", 2).execute().actionGet();
updateResponse = client().prepareUpdate(indexOrAlias(), "type1", "1")
.setScript(new Script(ScriptType.INLINE, UPDATE_SCRIPTS, FIELD_INC_SCRIPT, Collections.singletonMap("field", "field1")))
.setFetchSource(null, "field1")
.get();
assertThat(updateResponse.getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult(), notNullValue());
assertThat(updateResponse.getGetResult().getIndex(), equalTo("test"));
assertThat(updateResponse.getGetResult().sourceRef(), notNullValue());
assertThat(updateResponse.getGetResult().field("field2"), nullValue());
assertThat(updateResponse.getGetResult().sourceAsMap().size(), equalTo(1));
assertThat(updateResponse.getGetResult().sourceAsMap().get("field2"), equalTo(2));

// check updates without script
// add new field
client().prepareIndex("test", "type1", "1").setSource("field", 1).execute().actionGet();
Expand Down

0 comments on commit 5148f1f

Please sign in to comment.