[7.x] Fix NPE when building exception messages for aggregations (#59156) (#59334)

This commit is contained in:
Mark Tozzi 2020-07-14 09:37:44 -04:00 committed by GitHub
parent cf752992d6
commit b357c1b77a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 6 deletions

View File

@ -18,7 +18,6 @@
*/
package org.elasticsearch.search.aggregations.support;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.search.SearchModule;
import org.elasticsearch.search.aggregations.AggregationExecutionException;
@ -123,11 +122,10 @@ public class ValuesSourceRegistry {
aggregatorRegistry.get(aggregationName)
);
if (supplier == null) {
// TODO: push building the description into ValuesSourceConfig
MappedFieldType fieldType = valuesSourceConfig.fieldContext().fieldType();
String fieldDescription = fieldType.typeName();
throw new IllegalArgumentException("Field [" + fieldType.name() + "] of type [" + fieldDescription +
"] is not supported for aggregation [" + aggregationName + "]"); }
throw new IllegalArgumentException(
valuesSourceConfig.getDescription() + " is not supported for aggregation [" + aggregationName + "]"
);
}
return supplier;
}
throw new AggregationExecutionException("Unregistered Aggregation [" + aggregationName + "]");

View File

@ -0,0 +1,68 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.search.aggregations.support;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.script.AggregationScript;
import org.elasticsearch.test.ESTestCase;
import org.mockito.Mockito;
import java.util.Collections;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ValuesSourceRegistryTests extends ESTestCase {
public void testAggregatorNotFoundException() {
final QueryShardContext queryShardContext = mock(QueryShardContext.class);
final AggregationScript.Factory mockAggScriptFactory = mock(AggregationScript.Factory.class);
when(mockAggScriptFactory.newFactory(Mockito.any(), Mockito.any())).thenReturn(mock(AggregationScript.LeafFactory.class));
when(queryShardContext.compile(Mockito.any(), Mockito.any())).thenReturn(mockAggScriptFactory);
ValuesSourceConfig fieldOnly = ValuesSourceConfig.resolve(
queryShardContext,
null,
"field",
null,
null,
null,
null,
CoreValuesSourceType.BYTES
);
ValuesSourceConfig scriptOnly = ValuesSourceConfig.resolve(
queryShardContext,
null,
null,
mockScript("fakeScript"),
null,
null,
null,
CoreValuesSourceType.BYTES
);
ValuesSourceRegistry registry = new ValuesSourceRegistry(
Collections.singletonMap("bogus", Collections.emptyList()),
null);
expectThrows(IllegalArgumentException.class, () -> registry.getAggregator(fieldOnly, "bogus"));
expectThrows(IllegalArgumentException.class, () -> registry.getAggregator(scriptOnly, "bogus"));
}
}