Skip to content

Commit

Permalink
Fix serialization of time_zone field in CompositeDateHistogramAggrega…
Browse files Browse the repository at this point in the history
…tionSource (#1362)

* Fix serialization of time_zone field in CompositeDateHistogramAggregationSource

Signed-off-by: Fabien Crespel <[email protected]>

* Add test for CompositeDateHistogramAggregationSource

Signed-off-by: Fabien Crespel <[email protected]>

---------

Signed-off-by: Fabien Crespel <[email protected]>
  • Loading branch information
fcrespel authored Dec 21, 2024
1 parent fca7ec0 commit 5aef053
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
### Removed

### Fixed
- Fixed serialization of `time_zone` field in `CompositeDateHistogramAggregationSource` ([#1362](https://github.com/opensearch-project/opensearch-java/pull/1362))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ public class CompositeDateHistogramAggregationSource extends CompositeValuesSour
@Nullable
private final Long offset;

private final String zoneId;
@Nullable
private final String timeZone;

private CompositeDateHistogramAggregationSource(Builder builder) {
super(builder);
this.calendarInterval = builder.calendarInterval;
this.fixedInterval = builder.fixedInterval;
this.offset = builder.offset;
this.zoneId = builder.zoneId;
this.timeZone = builder.timeZone;
}

public static CompositeDateHistogramAggregationSource of(Function<Builder, ObjectBuilder<CompositeDateHistogramAggregationSource>> fn) {
Expand Down Expand Up @@ -65,14 +66,22 @@ public final Time fixedInterval() {
@Nullable
public final Long offset() {
return this.offset;
}

/**
* API name: {@code time_zone}
*/
public final String timeZone() {
return this.timeZone;
}

/**
* Required - API name: {@code zone_id}
* API name: {@code time_zone}
* @deprecated Use {@link #timeZone()} instead.
*/
@Deprecated
public final String zoneId() {
return this.zoneId;
return timeZone();
}

/**
Expand Down Expand Up @@ -103,8 +112,10 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

}

generator.writeKey("zone_id");
generator.write(this.zoneId);
if (this.timeZone != null) {
generator.writeKey("time_zone");
generator.write(this.timeZone);
}
}

/**
Expand All @@ -124,7 +135,8 @@ public static class Builder extends CompositeValuesSource.AbstractBuilder<Builde
@Nullable
private Long offset;

private String zoneId;
@Nullable
private String timeZone;

/**
* API name: {@code calendar_interval}
Expand All @@ -151,12 +163,20 @@ public final Builder offset(Long value) {
}

/**
* Required - API name: {@code zone_id}
* API name: {@code time_zone}
*/
public final Builder timeZone(String value) {
this.timeZone = value;
return this;
}

/**
* API name: {@code time_zone}
* @deprecated Use {@link #timeZone(String)} instead.
*/
@Deprecated
public final Builder zoneId(String value) {
this.zoneId = value;
return this;
return timeZone(value);
}

/**
Expand Down Expand Up @@ -192,7 +212,7 @@ protected static void setupCompositeDateHistogramAggregationSourceDeserializer(
op.add(Builder::calendarInterval, Time._DESERIALIZER, "calendar_interval");
op.add(Builder::fixedInterval, Time._DESERIALIZER, "fixed_interval");
op.add(Builder::offset, JsonpDeserializer.longDeserializer(), "offset");
op.add(Builder::zoneId, JsonpDeserializer.stringDeserializer(), "time_zone");
op.add(Builder::timeZone, JsonpDeserializer.stringDeserializer(), "time_zone");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 CompositeDateHistogramAggregationSourceTest extends ModelTestCase {

@Test
public void testSerializeSpecificFields() {
String json = "{\"calendar_interval\":\"1d\",\"fixed_interval\":\"1d\",\"offset\":1,\"time_zone\":\"+01:00\"}";
CompositeDateHistogramAggregationSource aggregation = fromJson(json, CompositeDateHistogramAggregationSource._DESERIALIZER);
assertEquals(json, toJson(aggregation));
}

}

0 comments on commit 5aef053

Please sign in to comment.