Fixes a bug in interval filter serialization (#49793)

There is a possible NPE in IntervalFilter xcontent serialization when scripts are
used, and `equals` and `hashCode` are also incorrectly implemented for script
filters.  This commit fixes both.
This commit is contained in:
Alan Woodward 2019-12-04 08:47:40 +00:00
parent 996cddd98b
commit 408f25e016
2 changed files with 15 additions and 6 deletions

View File

@ -749,12 +749,13 @@ public abstract class IntervalsSourceProvider implements NamedWriteable, ToXCont
if (o == null || getClass() != o.getClass()) return false;
IntervalFilter that = (IntervalFilter) o;
return Objects.equals(type, that.type) &&
Objects.equals(script, that.script) &&
Objects.equals(filter, that.filter);
}
@Override
public int hashCode() {
return Objects.hash(type, filter);
return Objects.hash(type, filter, script);
}
@Override
@ -773,10 +774,13 @@ public abstract class IntervalsSourceProvider implements NamedWriteable, ToXCont
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(type);
builder.startObject();
if (filter != null) {
builder.startObject(type);
filter.toXContent(builder, params);
builder.endObject();
} else {
builder.field(Script.SCRIPT_PARSE_FIELD.getPreferredName(), script);
}
builder.endObject();
return builder;
}

View File

@ -36,6 +36,7 @@ import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptContext;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.test.AbstractQueryTestCase;
import java.io.IOException;
@ -114,8 +115,12 @@ public class IntervalQueryBuilderTests extends AbstractQueryTestCase<IntervalQue
private IntervalsSourceProvider.IntervalFilter createRandomFilter(int depth) {
if (depth < 3 && randomInt(20) > 18) {
if (randomBoolean()) {
return new IntervalsSourceProvider.IntervalFilter(createRandomSource(depth + 1), randomFrom(filters));
}
return new IntervalsSourceProvider.IntervalFilter(
new Script(ScriptType.INLINE, "mockscript", "1", Collections.emptyMap()));
}
return null;
}