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:
parent
996cddd98b
commit
408f25e016
|
@ -749,12 +749,13 @@ public abstract class IntervalsSourceProvider implements NamedWriteable, ToXCont
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
IntervalFilter that = (IntervalFilter) o;
|
IntervalFilter that = (IntervalFilter) o;
|
||||||
return Objects.equals(type, that.type) &&
|
return Objects.equals(type, that.type) &&
|
||||||
|
Objects.equals(script, that.script) &&
|
||||||
Objects.equals(filter, that.filter);
|
Objects.equals(filter, that.filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(type, filter);
|
return Objects.hash(type, filter, script);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -773,10 +774,13 @@ public abstract class IntervalsSourceProvider implements NamedWriteable, ToXCont
|
||||||
@Override
|
@Override
|
||||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||||
builder.startObject();
|
builder.startObject();
|
||||||
builder.field(type);
|
if (filter != null) {
|
||||||
builder.startObject();
|
builder.startObject(type);
|
||||||
filter.toXContent(builder, params);
|
filter.toXContent(builder, params);
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
|
} else {
|
||||||
|
builder.field(Script.SCRIPT_PARSE_FIELD.getPreferredName(), script);
|
||||||
|
}
|
||||||
builder.endObject();
|
builder.endObject();
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,7 @@ import org.elasticsearch.index.mapper.MapperService;
|
||||||
import org.elasticsearch.script.Script;
|
import org.elasticsearch.script.Script;
|
||||||
import org.elasticsearch.script.ScriptContext;
|
import org.elasticsearch.script.ScriptContext;
|
||||||
import org.elasticsearch.script.ScriptService;
|
import org.elasticsearch.script.ScriptService;
|
||||||
|
import org.elasticsearch.script.ScriptType;
|
||||||
import org.elasticsearch.test.AbstractQueryTestCase;
|
import org.elasticsearch.test.AbstractQueryTestCase;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -114,7 +115,11 @@ public class IntervalQueryBuilderTests extends AbstractQueryTestCase<IntervalQue
|
||||||
|
|
||||||
private IntervalsSourceProvider.IntervalFilter createRandomFilter(int depth) {
|
private IntervalsSourceProvider.IntervalFilter createRandomFilter(int depth) {
|
||||||
if (depth < 3 && randomInt(20) > 18) {
|
if (depth < 3 && randomInt(20) > 18) {
|
||||||
return new IntervalsSourceProvider.IntervalFilter(createRandomSource(depth + 1), randomFrom(filters));
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue