From d804d735ab5428f18e7b32a85f92b923c66aa09f Mon Sep 17 00:00:00 2001 From: George Apaaboah <35894485+GeorgeAp@users.noreply.github.com> Date: Thu, 16 Jun 2022 21:24:52 +0200 Subject: [PATCH] Fix NPE when minBound/maxBound is not set before being called. (#3605) Signed-off-by: George Apaaboah (cherry picked from commit 34797c6cb47e45d48f36c010d1163f279fd99f71) --- .../histogram/HistogramAggregationBuilder.java | 4 ++-- .../search/aggregations/bucket/HistogramTests.java | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/HistogramAggregationBuilder.java b/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/HistogramAggregationBuilder.java index ceb7709907057..72ce0e5e831d5 100644 --- a/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/HistogramAggregationBuilder.java +++ b/server/src/main/java/org/opensearch/search/aggregations/bucket/histogram/HistogramAggregationBuilder.java @@ -227,12 +227,12 @@ public HistogramAggregationBuilder offset(double offset) { /** Get the current minimum bound that is set on this builder. */ public double minBound() { - return extendedBounds.getMin(); + return DoubleBounds.getEffectiveMin(extendedBounds); } /** Get the current maximum bound that is set on this builder. */ public double maxBound() { - return extendedBounds.getMax(); + return DoubleBounds.getEffectiveMax(extendedBounds); } protected DoubleBounds extendedBounds() { diff --git a/server/src/test/java/org/opensearch/search/aggregations/bucket/HistogramTests.java b/server/src/test/java/org/opensearch/search/aggregations/bucket/HistogramTests.java index e233bee85462c..b4ec55543f494 100644 --- a/server/src/test/java/org/opensearch/search/aggregations/bucket/HistogramTests.java +++ b/server/src/test/java/org/opensearch/search/aggregations/bucket/HistogramTests.java @@ -103,6 +103,19 @@ public void testInvalidBounds() { assertThat(ex.getMessage(), equalTo("max bound [0.4] must be greater than min bound [0.5]")); } + /** + * Check that minBound/maxBound does not throw {@link NullPointerException} when called before being set. + */ + public void testMinBoundMaxBoundDefaultValues() { + HistogramAggregationBuilder factory = new HistogramAggregationBuilder("foo"); + + double minValue = factory.minBound(); + double maxValue = factory.maxBound(); + + assertThat(minValue, equalTo(Double.POSITIVE_INFINITY)); + assertThat(maxValue, equalTo(Double.NEGATIVE_INFINITY)); + } + private List randomOrder() { List orders = new ArrayList<>(); switch (randomInt(4)) {