From 2dbecd7209e248974d1d9ee6f76bce1c9a1d87e7 Mon Sep 17 00:00:00 2001 From: Thomas Farr Date: Thu, 12 Dec 2024 01:38:15 +1300 Subject: [PATCH] Fix serialization of nested aggregates under `SingleBucketAggregateBase` (#1350) * Add failing test Signed-off-by: Thomas Farr * Fix Signed-off-by: Thomas Farr --------- Signed-off-by: Thomas Farr (cherry picked from commit 29381c0f77f1a7d2dbd48b82d37d6e1cae5bf9d2) --- CHANGELOG.md | 1 + .../SingleBucketAggregateBase.java | 5 ++++- .../aggregations/FilterAggregateTest.java | 21 +++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 java-client/src/test/java/org/opensearch/client/opensearch/_types/aggregations/FilterAggregateTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f85236a36..e9126331cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Fixed an issue where `FieldSort` was not implementing `SortOptionsVariant` ([#1323](https://github.com/opensearch-project/opensearch-java/pull/1323)) - Fixed `CreateSnapshotResponse` deserialization when wait_for_completion is false ([#1332](https://github.com/opensearch-project/opensearch-java/pull/1332)) - Fixed `GetSnapshotResponse` deserialization ([#1299](https://github.com/opensearch-project/opensearch-java/pull/1299)) +- Fixed serialization of nested aggregates under `SingleBucketAggregateBase` ([#1350](https://github.com/opensearch-project/opensearch-java/pull/1350)) ### Security diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/aggregations/SingleBucketAggregateBase.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/aggregations/SingleBucketAggregateBase.java index 35ade2fbb9..d96f002654 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/aggregations/SingleBucketAggregateBase.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/aggregations/SingleBucketAggregateBase.java @@ -37,6 +37,7 @@ import java.util.Map; import java.util.function.Function; import javax.annotation.Nullable; +import org.opensearch.client.json.ExternallyTaggedUnion; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.ObjectDeserializer; @@ -71,8 +72,10 @@ public final long docCount() { } protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { - super.serializeInternal(generator, mapper); + + ExternallyTaggedUnion.serializeTypedKeysInner(this.aggregations, generator, mapper); + generator.writeKey("doc_count"); generator.write(this.docCount); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/_types/aggregations/FilterAggregateTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/_types/aggregations/FilterAggregateTest.java new file mode 100644 index 0000000000..ad031fa432 --- /dev/null +++ b/java-client/src/test/java/org/opensearch/client/opensearch/_types/aggregations/FilterAggregateTest.java @@ -0,0 +1,21 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +package org.opensearch.client.opensearch._types.aggregations; + +import org.junit.Test; +import org.opensearch.client.opensearch.model.ModelTestCase; + +public class FilterAggregateTest extends ModelTestCase { + @Test + public void serializesNestedAggregates() { + String json = "{\"sum#l2_result\":{\"value\":1.0},\"doc_count\":1}"; + FilterAggregate aggregate = fromJson(json, FilterAggregate._DESERIALIZER); + assertEquals(json, toJson(aggregate)); + } +}