Repository navigation
Description
What is the bug?
On main, the five terms aggregate result types no longer expose typed buckets. Since #1957 (merged 2026-10-03) they are generated as:
public class StringTermsAggregate extends TermsAggregateBase<JsonData> // was TermsAggregateBase<StringTermsBucket>public class LongTermsAggregate extends TermsAggregateBase<JsonData> // was TermsAggregateBase<LongTermsBucket>public class DoubleTermsAggregate extends TermsAggregateBase<JsonData> // was TermsAggregateBase<DoubleTermsBucket>public class UnsignedLongTermsAggregate extends TermsAggregateBase<JsonData>public class UnmappedTermsAggregate extends TermsAggregateBase<JsonData> // was TermsAggregateBase<Void>and the bucket classes they used to reference were removed from src/generated: StringTermsBucket, LongTermsBucket, LongTermsBucketKey, DoubleTermsBucket, TermsBucketBase (and UnsignedLongTermsBucket is never generated). The same commit adjusted three tests to the untyped buckets, for example AbstractRequestIT:
prices.dterms().buckets().array().get(0).to(MultiTermsBucket.class).docCount()So a user of the next major release would read a terms bucket as JsonData and parse key / doc_count by hand, where 3.x gives bucket.key() and bucket.docCount().
3.x is not affected yet: the backport of #1957 (#2201) carried only part of the regeneration and left the terms files as they were. The code generator is identical on both branches, though, so a full regeneration of 3.x would produce the same result.
How can one reproduce the bug?
- Check out
mainand run./gradlew :java-codegen:run. git statusis clean, i.e. the generator reproduces the current output.- Open
java-client/src/generated/java/org/opensearch/client/opensearch/_types/aggregations/StringTermsAggregate.java, line 58.
What is the expected behavior?
StringTermsAggregate extends TermsAggregateBase<StringTermsBucket> etc., as on 3.x and in every release so far, with StringTermsBucket and the other bucket classes generated.
What is your host/environment?
main at 1f6b171, JDK 21, macOS. The vendored specification is the one main ships (java-codegen/opensearch-openapi.yaml).
Do you have any additional context?
Cause (as far as I could trace it). The specification changed how the terms aggregates instantiate their generic base in opensearch-project/opensearch-api-specification#1063. The generic base MultiBucketAggregateBase declares buckets as allOf: [Buckets, oneOf: [keyed map of TBucket, array of TBucket]], because some aggregations (range, date_range, filters) can return keyed buckets. Before #1063 the instantiation TermsAggregateBaseStringTermsBucket repeated that whole shape; since #1063 it declares only buckets: { type: array, items: StringTermsBucket }, which is what the server returns for terms.
TypeMapper.mapInstantiatedGeneric binds type parameters by zipping the generic schema with the instantiation and only records a binding when a TBucket node and the instantiation's node sit at the same position. With the instantiation narrowed to one branch of the union, TBucket (under oneOf[1].items) and StringTermsBucket (under items) never pair, the map stays empty, and the type parameter falls back to JsonData (typeParameters.getOrDefault(n, Types.Client.Json.JsonData)). Nothing is logged, so the change went unnoticed inside a 268-file regeneration.
The March regeneration (#1938) still used a bundle that predated #1063, which is why the problem first appeared in the April bundle that #1957 was generated from.
Related. #613 and #1737 are the 3.x face of the same mismatch: the generator also drops doc_count_error_upper_bound from TermsBucketBase, so a bucket with show_term_doc_count_error: true fails to deserialize. #2298 fixes an NullPointerException in the same method and adds an experimental model-based path; as far as I can tell from the PR and from running it, neither path changes the terms output (the PR notes "the generated code is unchanged"), and the model path reaches the same TypeMapper with the same narrowed shape.
Proposed fix. Binding the type parameter from the branch of the generic union whose container kind matches the instantiation (items ↔ items, additionalProperties ↔ additionalProperties).
Dependency on #2298. This change only works on top of #2298's first commit (28175b8, the NullPointerException fix in mapInstantiatedGeneric). On its own it fails with that exact NPE: the new binding maps StringTermsBucket while TermsAggregateBase is still being built, because the bucket's sub-aggregations (MultiBucketBase.additionalProperties: Aggregate) lead back to the terms aggregates. So #2298's first commit, or an equivalent of it, has to land first or together with this.
The binding change alone then surfaced a second problem: TermsBucketBase is labeled generic through additionalProperties: Aggregate even though it declares no type parameter, which made UnsignedLongTermsAggregate come out as TermsAggregateBase<TermsBucketBase<>>; a guard in GenericLabelingVisitor.isInstantiatedGeneric that requires the extended schema's own chain to declare a type parameter resolves it.
With both changes the generator produces the typed terms aggregates again, restores the six bucket classes (now including doc_count_error_upper_bound, which would also close #613 / #1737), changes no other generated file, and :java-codegen:test, :java-client:compileJava, spotlessCheck and TypedKeysTest pass once the three tests are returned to typed access.
I will open a PR with this shortly and link it here. It builds on the first commit of #2298; if it is easier to review as part of that PR, or a different approach to the labeling is preferred, I am glad to adjust.
Activity
minseoky commentedon Nov 23, 2026
Opened #2315 with the fix described above (TypeMapper binding from the narrowed union branch, the labeling guard, and the regenerated terms aggregates). It carries #2298's NPE fix as its first commit.