From 474438bb5fc46b99a8d927b6433591df134b6a6d Mon Sep 17 00:00:00 2001 From: Jai2305 Date: Mon, 2 Sep 2024 18:34:01 +0530 Subject: [PATCH] support for withJson-618 Signed-off-by: Jai2305 --- CHANGELOG.md | 1 + .../client/json/PlainDeserializable.java | 55 +++++++++++++++++++ .../_types/mapping/SourceField.java | 8 ++- .../_types/mapping/TypeMapping.java | 8 ++- .../opensearch/indices/IndexSettings.java | 7 ++- .../IndexTemplateMapping.java | 8 ++- .../json/PlainDeserializableTest.java | 42 ++++++++++++++ 7 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 java-client/src/main/java/org/opensearch/client/json/PlainDeserializable.java create mode 100644 java-client/src/test/java/org/opensearch/client/opensearch/json/PlainDeserializableTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ece8b4d62..9662556449 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -53,6 +53,7 @@ This section is for maintaining a changelog for all breaking changes for the cli - Added `queryImage` (query_image) field to `NeuralQuery`, following definition in ([Neural Query](https://opensearch.org/docs/latest/query-dsl/specialized/neural/)) ([#1137](https://github.com/opensearch-project/opensearch-java/pull/1138)) - Added `cancelAfterTimeInterval` to `SearchRequest` and `MsearchRequest` ([#1147](https://github.com/opensearch-project/opensearch-java/pull/1147)) - Added the `ml` namespace operations ([#1158](https://github.com/opensearch-project/opensearch-java/pull/1158)) +- Added `PlainDeserializable` with `withJson` method for streamlining deserialization ([#1148](https://github.com/opensearch-project/opensearch-java/pull/1148)) ### Dependencies - Bumps `commons-logging:commons-logging` from 1.3.3 to 1.3.4 diff --git a/java-client/src/main/java/org/opensearch/client/json/PlainDeserializable.java b/java-client/src/main/java/org/opensearch/client/json/PlainDeserializable.java new file mode 100644 index 0000000000..12793701a4 --- /dev/null +++ b/java-client/src/main/java/org/opensearch/client/json/PlainDeserializable.java @@ -0,0 +1,55 @@ +/* + * 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.json; + +import jakarta.json.stream.JsonParser; +import java.io.InputStream; +import java.io.Reader; + +/** Base interface to set JSON properties **/ + +public interface PlainDeserializable { + + B self(); + + /** Updates object with newly provided JSON properties + @param parser the JsonParser parser + @param mapper the JsonpMapper mapper used to deserialize values + @return this object + **/ + + default B withJson(JsonParser parser, JsonpMapper mapper) { + JsonpDeserializer deserializer = JsonpMapperBase.findDeserializer(this.getClass().getEnclosingClass()); + @SuppressWarnings("unchecked") + ObjectDeserializer objectDeserializer = (ObjectDeserializer) DelegatingDeserializer.unwrap(deserializer); + assert objectDeserializer != null; + return objectDeserializer.deserialize(self(), parser, mapper, parser.next()); + } + + /** Updates object with newly provided JSON properties + @param inputStream the stream to read from + @return this object + * **/ + default B withJson(InputStream inputStream) { + JsonpMapper defaultMapper = JsonpUtils.DEFAULT_JSONP_MAPPER; + JsonParser parser = defaultMapper.jsonProvider().createParser(inputStream); + return withJson(parser, defaultMapper); + } + + /** Updates object with newly provided JSON properties + @param reader the stream to read from + @return this object + * **/ + default B withJson(Reader reader) { + JsonpMapper defaultMapper = JsonpUtils.DEFAULT_JSONP_MAPPER; + JsonParser parser = defaultMapper.jsonProvider().createParser(reader); + return withJson(parser, defaultMapper); + } + +} diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/mapping/SourceField.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/mapping/SourceField.java index 0fcaf11cf3..6ff76552f3 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/mapping/SourceField.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/mapping/SourceField.java @@ -41,6 +41,7 @@ import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.json.PlainDeserializable; import org.opensearch.client.json.PlainJsonSerializable; import org.opensearch.client.util.ApiTypeHelper; import org.opensearch.client.util.ObjectBuilder; @@ -172,7 +173,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { * Builder for {@link SourceField}. */ - public static class Builder extends ObjectBuilderBase implements ObjectBuilder { + public static class Builder extends ObjectBuilderBase implements ObjectBuilder, PlainDeserializable { @Nullable private Boolean compress; @@ -263,6 +264,11 @@ public SourceField build() { return new SourceField(this); } + + @Override + public Builder self() { + return this; + } } // --------------------------------------------------------------------------------------------- diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/mapping/TypeMapping.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/mapping/TypeMapping.java index 7d8adceabf..cbc74c0013 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/mapping/TypeMapping.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/mapping/TypeMapping.java @@ -43,6 +43,7 @@ import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.json.PlainDeserializable; import org.opensearch.client.json.PlainJsonSerializable; import org.opensearch.client.util.ApiTypeHelper; import org.opensearch.client.util.ObjectBuilder; @@ -363,7 +364,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { * Builder for {@link TypeMapping}. */ - public static class Builder extends ObjectBuilderBase implements ObjectBuilder { + public static class Builder extends ObjectBuilderBase implements ObjectBuilder, PlainDeserializable { @Nullable private AllField allField; @@ -660,6 +661,11 @@ public TypeMapping build() { return new TypeMapping(this); } + + @Override + public Builder self() { + return this; + } } // --------------------------------------------------------------------------------------------- diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/indices/IndexSettings.java b/java-client/src/main/java/org/opensearch/client/opensearch/indices/IndexSettings.java index 9e68b6fbcc..12cdd73aa0 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/indices/IndexSettings.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/indices/IndexSettings.java @@ -41,6 +41,7 @@ import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.json.PlainDeserializable; import org.opensearch.client.json.PlainJsonSerializable; import org.opensearch.client.opensearch._types.Time; import org.opensearch.client.util.ApiTypeHelper; @@ -1108,7 +1109,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { * Builder for {@link IndexSettings}. */ - public static class Builder extends ObjectBuilderBase implements ObjectBuilder { + public static class Builder extends ObjectBuilderBase implements ObjectBuilder, PlainDeserializable { @Nullable private IndexSettings index; @@ -1919,6 +1920,10 @@ public final Builder knnAlgoParamEfSearch(@Nullable Integer value) { return this; } + @Override + public Builder self() { + return this; + } } // --------------------------------------------------------------------------------------------- diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/indices/put_index_template/IndexTemplateMapping.java b/java-client/src/main/java/org/opensearch/client/opensearch/indices/put_index_template/IndexTemplateMapping.java index a32aac2ad9..638b5e8afa 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/indices/put_index_template/IndexTemplateMapping.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/indices/put_index_template/IndexTemplateMapping.java @@ -41,6 +41,7 @@ import org.opensearch.client.json.JsonpMapper; import org.opensearch.client.json.ObjectBuilderDeserializer; import org.opensearch.client.json.ObjectDeserializer; +import org.opensearch.client.json.PlainDeserializable; import org.opensearch.client.json.PlainJsonSerializable; import org.opensearch.client.opensearch._types.mapping.TypeMapping; import org.opensearch.client.opensearch.indices.Alias; @@ -139,7 +140,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { * Builder for {@link IndexTemplateMapping}. */ - public static class Builder extends ObjectBuilderBase implements ObjectBuilder { + public static class Builder extends ObjectBuilderBase implements ObjectBuilder, PlainDeserializable { @Nullable private Map aliases; @@ -219,6 +220,11 @@ public IndexTemplateMapping build() { return new IndexTemplateMapping(this); } + + @Override + public Builder self() { + return this; + } } // --------------------------------------------------------------------------------------------- diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/json/PlainDeserializableTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/json/PlainDeserializableTest.java new file mode 100644 index 0000000000..4df9d60e36 --- /dev/null +++ b/java-client/src/test/java/org/opensearch/client/opensearch/json/PlainDeserializableTest.java @@ -0,0 +1,42 @@ +/* + * 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.json; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.List; +import org.junit.Test; +import org.opensearch.client.opensearch.indices.PutIndexTemplateRequest; +import org.opensearch.client.opensearch.indices.put_index_template.IndexTemplateMapping; + +public class PlainDeserializableTest { + @Test + public void testWithJsonPutIndexTemplateRequest() { + + String stringTemplate = + "{\"mappings\":{\"properties\":{\"age\":{\"type\":\"integer\"}}},\"settings\":{\"number_of_shards\":\"2\",\"number_of_replicas\":\"1\"}}"; + InputStream inputStream = new ByteArrayInputStream(stringTemplate.getBytes(StandardCharsets.UTF_8)); + + PutIndexTemplateRequest indexTemplateRequest = new PutIndexTemplateRequest.Builder().name("My index") + .indexPatterns("index_pattern1") + .template(new IndexTemplateMapping.Builder().withJson(inputStream).build()) + .build(); + + String expectedName = "My index"; + List expectedIndexPatterns = Arrays.asList("index_pattern1"); + String expectedNumberOfShards = "2"; + + assert expectedName.equals(indexTemplateRequest.name()); + assert expectedIndexPatterns.equals(indexTemplateRequest.indexPatterns()); + assert expectedNumberOfShards.equals(indexTemplateRequest.template().settings().numberOfShards()); + + } +}