From 08297bec20043aaf7a8d64f666c565c01595a6b5 Mon Sep 17 00:00:00 2001 From: pieper Date: Wed, 27 Sep 2023 08:56:12 +0200 Subject: [PATCH 1/6] TermvectorsResponse fix for optionals. Signed-off-by: pieper --- .../opensearch/core/TermvectorsResponse.java | 10 ++++--- .../opensearch/core/termvectors/Term.java | 20 +++++++------- .../core/termvectors/TermVector.java | 8 +++--- .../opensearch/experiments/ParsingTests.java | 26 ++++++++++++++++++- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/core/TermvectorsResponse.java b/java-client/src/main/java/org/opensearch/client/opensearch/core/TermvectorsResponse.java index 8eb6f99b7d..a4653cff7e 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/core/TermvectorsResponse.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/core/TermvectorsResponse.java @@ -62,7 +62,7 @@ public class TermvectorsResponse implements JsonpSerializable { private final long took; - private final long version; + private final Long version; // --------------------------------------------------------------------------------------------- @@ -73,7 +73,7 @@ private TermvectorsResponse(Builder builder) { this.index = ApiTypeHelper.requireNonNull(builder.index, this, "index"); this.termVectors = ApiTypeHelper.unmodifiable(builder.termVectors); this.took = ApiTypeHelper.requireNonNull(builder.took, this, "took"); - this.version = ApiTypeHelper.requireNonNull(builder.version, this, "version"); + this.version = builder.version; } @@ -157,8 +157,10 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { generator.writeKey("took"); generator.write(this.took); - generator.writeKey("_version"); - generator.write(this.version); + if (null != this.version) { + generator.writeKey("_version"); + generator.write(this.version); + } } diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/Term.java b/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/Term.java index cb654cbeb3..f13f9dabb3 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/Term.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/Term.java @@ -32,6 +32,10 @@ package org.opensearch.client.opensearch.core.termvectors; +import jakarta.json.stream.JsonGenerator; +import java.util.List; +import java.util.function.Function; +import javax.annotation.Nullable; import org.opensearch.client.json.JsonpDeserializable; import org.opensearch.client.json.JsonpDeserializer; import org.opensearch.client.json.JsonpMapper; @@ -41,10 +45,6 @@ import org.opensearch.client.util.ApiTypeHelper; import org.opensearch.client.util.ObjectBuilder; import org.opensearch.client.util.ObjectBuilderBase; -import jakarta.json.stream.JsonGenerator; -import java.util.List; -import java.util.function.Function; -import javax.annotation.Nullable; // typedef: _global.termvectors.Term @@ -57,7 +57,7 @@ public class Term implements JsonpSerializable { @Nullable private final Double score; - private final int termFreq; + private final Integer termFreq; private final List tokens; @@ -70,8 +70,8 @@ private Term(Builder builder) { this.docFreq = builder.docFreq; this.score = builder.score; - this.termFreq = ApiTypeHelper.requireNonNull(builder.termFreq, this, "termFreq"); - this.tokens = ApiTypeHelper.unmodifiableRequired(builder.tokens, this, "tokens"); + this.termFreq = builder.termFreq; + this.tokens = ApiTypeHelper.unmodifiable(builder.tokens); this.ttf = builder.ttf; } @@ -139,8 +139,10 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { generator.write(this.score); } - generator.writeKey("term_freq"); - generator.write(this.termFreq); + if (null != this.termFreq) { + generator.writeKey("term_freq"); + generator.write(this.termFreq); + } if (ApiTypeHelper.isDefined(this.tokens)) { generator.writeKey("tokens"); diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/TermVector.java b/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/TermVector.java index 3ce0d1e1a6..9770432ffb 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/TermVector.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/TermVector.java @@ -58,7 +58,7 @@ public class TermVector implements JsonpSerializable { private TermVector(Builder builder) { - this.fieldStatistics = ApiTypeHelper.requireNonNull(builder.fieldStatistics, this, "fieldStatistics"); + this.fieldStatistics = builder.fieldStatistics; this.terms = ApiTypeHelper.unmodifiableRequired(builder.terms, this, "terms"); } @@ -92,8 +92,10 @@ public void serialize(JsonGenerator generator, JsonpMapper mapper) { protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { - generator.writeKey("field_statistics"); - this.fieldStatistics.serialize(generator, mapper); + if (null != this.fieldStatistics) { + generator.writeKey("field_statistics"); + this.fieldStatistics.serialize(generator, mapper); + } if (ApiTypeHelper.isDefined(this.terms)) { generator.writeKey("terms"); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java index ccb3e4b5d3..3b6a4dedfe 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java @@ -33,13 +33,13 @@ package org.opensearch.client.opensearch.experiments; import java.util.List; - import org.junit.Test; import org.opensearch.client.opensearch._types.Time; import org.opensearch.client.opensearch._types.analysis.Analyzer; import org.opensearch.client.opensearch._types.analysis.TokenFilterDefinition; import org.opensearch.client.opensearch._types.analysis.TokenizerBuilders; import org.opensearch.client.opensearch._types.analysis.TokenizerDefinition; +import org.opensearch.client.opensearch.core.TermvectorsResponse; import org.opensearch.client.opensearch.experiments.api.FooRequest; import org.opensearch.client.opensearch.indices.IndexSettings; import org.opensearch.client.opensearch.indices.IndexSettingsMapping; @@ -173,4 +173,28 @@ public void testCjk_Analyzer() { assertEquals(analyzer.cjk().stopwords(), analyzer2.cjk().stopwords()); assertEquals(analyzer.cjk().stopwordsPath(), analyzer2.cjk().stopwordsPath()); } + @Test + public void testTermvectorsResponseOptionals() { + // Build a response without any optionals + final TermvectorsResponse response = TermvectorsResponse.of(b -> b + .index("index") + .id("id") + .found(true) + .took(0) + .termVectors("key1", tvb -> tvb + .terms("term1", tb -> tb + .score(0.3) + ) + ) + ); + + String str = toJson(response); + assertEquals("{\"found\":true,\"_id\":\"id\",\"_index\":\"index\"," + +"\"term_vectors\":{\"key1\":{\"terms\":{\"term1\":{\"score\":0.3}}}},\"took\":0}", str); + + final TermvectorsResponse response2 = fromJson(str, TermvectorsResponse._DESERIALIZER); + assertEquals(response.index(), response2.index()); + } + + } From 06a068552d99819295979cf393f5321c38c68a31 Mon Sep 17 00:00:00 2001 From: pieper Date: Wed, 27 Sep 2023 17:21:53 +0200 Subject: [PATCH 2/6] Add Changelog. Signed-off-by: pieper --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad8be5bda9..ded1dd2867 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,7 +29,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Fixed - Fix version and build ([#254](https://github.com/opensearch-project/opensearch-java/pull/254)) - Fix PutMappingRequest by removing unsupported fields ([#597](https://github.com/opensearch-project/opensearch-java/pull/597)) - +- [BUG] Fix TermvectorsResponse for optional fields. ([#642](https://github.com/opensearch-project/opensearch-java/pull/642)) ### Security ## [Unreleased 2.x] From aa5e13ffed5be8812503b53bcad2802782a93923 Mon Sep 17 00:00:00 2001 From: pieper Date: Fri, 29 Sep 2023 06:54:51 +0200 Subject: [PATCH 3/6] Tabs vs. spaces fix. Signed-off-by: pieper --- .../client/opensearch/core/TermvectorsResponse.java | 8 ++++---- .../client/opensearch/core/termvectors/Term.java | 8 ++++---- .../client/opensearch/core/termvectors/TermVector.java | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/core/TermvectorsResponse.java b/java-client/src/main/java/org/opensearch/client/opensearch/core/TermvectorsResponse.java index a4653cff7e..f2b0bc49aa 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/core/TermvectorsResponse.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/core/TermvectorsResponse.java @@ -157,10 +157,10 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { generator.writeKey("took"); generator.write(this.took); - if (null != this.version) { - generator.writeKey("_version"); - generator.write(this.version); - } + if (null != this.version) { + generator.writeKey("_version"); + generator.write(this.version); + } } diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/Term.java b/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/Term.java index f13f9dabb3..d82d483a44 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/Term.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/Term.java @@ -139,10 +139,10 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { generator.write(this.score); } - if (null != this.termFreq) { - generator.writeKey("term_freq"); - generator.write(this.termFreq); - } + if (null != this.termFreq) { + generator.writeKey("term_freq"); + generator.write(this.termFreq); + } if (ApiTypeHelper.isDefined(this.tokens)) { generator.writeKey("tokens"); diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/TermVector.java b/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/TermVector.java index 9770432ffb..26d0c006f9 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/TermVector.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/core/termvectors/TermVector.java @@ -92,10 +92,10 @@ public void serialize(JsonGenerator generator, JsonpMapper mapper) { protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { - if (null != this.fieldStatistics) { - generator.writeKey("field_statistics"); - this.fieldStatistics.serialize(generator, mapper); - } + if (null != this.fieldStatistics) { + generator.writeKey("field_statistics"); + this.fieldStatistics.serialize(generator, mapper); + } if (ApiTypeHelper.isDefined(this.terms)) { generator.writeKey("terms"); From ecded52628eb136e5076c2522964f620702e97b2 Mon Sep 17 00:00:00 2001 From: pieper Date: Fri, 29 Sep 2023 07:21:45 +0200 Subject: [PATCH 4/6] Version is not optional. Signed-off-by: pieper --- .../client/opensearch/core/TermvectorsResponse.java | 10 ++++------ .../client/opensearch/experiments/ParsingTests.java | 3 ++- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/core/TermvectorsResponse.java b/java-client/src/main/java/org/opensearch/client/opensearch/core/TermvectorsResponse.java index f2b0bc49aa..8eb6f99b7d 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/core/TermvectorsResponse.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/core/TermvectorsResponse.java @@ -62,7 +62,7 @@ public class TermvectorsResponse implements JsonpSerializable { private final long took; - private final Long version; + private final long version; // --------------------------------------------------------------------------------------------- @@ -73,7 +73,7 @@ private TermvectorsResponse(Builder builder) { this.index = ApiTypeHelper.requireNonNull(builder.index, this, "index"); this.termVectors = ApiTypeHelper.unmodifiable(builder.termVectors); this.took = ApiTypeHelper.requireNonNull(builder.took, this, "took"); - this.version = builder.version; + this.version = ApiTypeHelper.requireNonNull(builder.version, this, "version"); } @@ -157,10 +157,8 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { generator.writeKey("took"); generator.write(this.took); - if (null != this.version) { - generator.writeKey("_version"); - generator.write(this.version); - } + generator.writeKey("_version"); + generator.write(this.version); } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java index 3b6a4dedfe..fa209af32a 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/experiments/ParsingTests.java @@ -179,6 +179,7 @@ public void testTermvectorsResponseOptionals() { final TermvectorsResponse response = TermvectorsResponse.of(b -> b .index("index") .id("id") + .version(1) .found(true) .took(0) .termVectors("key1", tvb -> tvb @@ -190,7 +191,7 @@ public void testTermvectorsResponseOptionals() { String str = toJson(response); assertEquals("{\"found\":true,\"_id\":\"id\",\"_index\":\"index\"," - +"\"term_vectors\":{\"key1\":{\"terms\":{\"term1\":{\"score\":0.3}}}},\"took\":0}", str); + +"\"term_vectors\":{\"key1\":{\"terms\":{\"term1\":{\"score\":0.3}}}},\"took\":0,\"_version\":1}", str); final TermvectorsResponse response2 = fromJson(str, TermvectorsResponse._DESERIALIZER); assertEquals(response.index(), response2.index()); From 37d02d8e77603f50a339e7e5cc77ecc14f5d7d15 Mon Sep 17 00:00:00 2001 From: pieper Date: Thu, 5 Oct 2023 07:36:06 +0200 Subject: [PATCH 5/6] Changelog fix. Signed-off-by: pieper --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c01b873cc..0f38edb5e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Fix version and build ([#254](https://github.com/opensearch-project/opensearch-java/pull/254)) - Fix PutMappingRequest by removing unsupported fields ([#597](https://github.com/opensearch-project/opensearch-java/pull/597)) - Fix parsing of GetFieldMappingResponse ([#641](https://github.com/opensearch-project/opensearch-java/pull/641)) -- [BUG] Fix TermvectorsResponse for optional fields. ([#642](https://github.com/opensearch-project/opensearch-java/pull/642)) +- Fix TermvectorsResponse for optional fields. ([#642](https://github.com/opensearch-project/opensearch-java/pull/642)) ### Security From ffb3c556fe2f47491e00b77ebc2f19b0724c2949 Mon Sep 17 00:00:00 2001 From: pieper Date: Sat, 7 Oct 2023 07:20:20 +0200 Subject: [PATCH 6/6] Period removed. Signed-off-by: pieper --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f38edb5e8..0a868f7f01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,7 +30,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Fix version and build ([#254](https://github.com/opensearch-project/opensearch-java/pull/254)) - Fix PutMappingRequest by removing unsupported fields ([#597](https://github.com/opensearch-project/opensearch-java/pull/597)) - Fix parsing of GetFieldMappingResponse ([#641](https://github.com/opensearch-project/opensearch-java/pull/641)) -- Fix TermvectorsResponse for optional fields. ([#642](https://github.com/opensearch-project/opensearch-java/pull/642)) +- Fix TermvectorsResponse for optional fields ([#642](https://github.com/opensearch-project/opensearch-java/pull/642)) ### Security