From 88740b6754c737f4bbf6b14bcbff06c8d1af4b4c Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Thu, 16 Jun 2022 16:22:42 -0400 Subject: [PATCH] Fix NPE when minBound/maxBound is not set before being called. (#3605) (#3610) Signed-off-by: George Apaaboah (cherry picked from commit 34797c6cb47e45d48f36c010d1163f279fd99f71) Co-authored-by: George Apaaboah <35894485+GeorgeAp@users.noreply.github.com> --- .../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)) {