Skip to content

Commit

Permalink
support for withJson-618
Browse files Browse the repository at this point in the history
Signed-off-by: Jai2305 <[email protected]>
  • Loading branch information
Jai2305 committed Aug 20, 2024
1 parent 08e7e65 commit 94a080e
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
- Added support for the `parent` aggregation type ([#706](https://github.com/opensearch-project/opensearch-java/pull/706))
- Added support for phase_took & search_pipeline request params ([#1036](https://github.com/opensearch-project/opensearch-java/pull/1036))
- Added an interface PlainJsonSerializable with a default method for serialization to Json ([#1064](https://github.com/opensearch-project/opensearch-java/pull/1064))
- Added an interface PlainDeserializable with withJson method for deserialization ([#1148](https://github.com/opensearch-project/opensearch-java/pull/1148))

### Dependencies
- Bumps `io.github.classgraph:classgraph` from 4.8.173 to 4.8.174
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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> {

B get();

/** Updates object with newly provided JSON properties
@param parser the JsonParser parser
@param mapper the JsonpMapper mapper used to deserialize values
@return object
**/

default B withJson(JsonParser parser, JsonpMapper mapper) {
JsonpDeserializer<?> deserializer = JsonpMapperBase.findDeserializer(this.getClass().getEnclosingClass());
@SuppressWarnings("unchecked")
ObjectDeserializer<B> objectDeserializer = (ObjectDeserializer<B>) DelegatingDeserializer.unwrap(deserializer);
assert objectDeserializer != null;
return objectDeserializer.deserialize(get(), 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;
return withJson(defaultMapper.jsonProvider().createParser(inputStream), 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@
import java.util.Collections;
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;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.json.*;
import org.opensearch.client.opensearch._types.ErrorResponse;
import org.opensearch.client.opensearch._types.RequestBase;
import org.opensearch.client.opensearch._types.Time;
Expand Down Expand Up @@ -127,7 +122,7 @@ public Builder toBuilder() {
* Builder for {@link ScrollRequest}.
*/

public static class Builder extends ObjectBuilderBase implements ObjectBuilder<ScrollRequest> {
public static class Builder extends ObjectBuilderBase implements ObjectBuilder<ScrollRequest>, PlainDeserializable<Builder> {
@Nullable
private Time scroll;

Expand Down Expand Up @@ -173,6 +168,11 @@ public ScrollRequest build() {

return new ScrollRequest(this);
}

@Override
public Builder get() {
return this;
}
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,14 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.opensearch._types.ErrorResponse;
import org.opensearch.client.opensearch._types.ExpandWildcard;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch._types.RequestBase;
import org.opensearch.client.opensearch._types.ScriptField;
import org.opensearch.client.opensearch._types.SearchType;
import org.opensearch.client.opensearch._types.SlicedScroll;
import org.opensearch.client.opensearch._types.SortOptions;
import org.opensearch.client.opensearch._types.Time;
import org.opensearch.client.json.*;
import org.opensearch.client.opensearch._types.*;
import org.opensearch.client.opensearch._types.aggregations.Aggregation;
import org.opensearch.client.opensearch._types.mapping.RuntimeField;
import org.opensearch.client.opensearch._types.query_dsl.FieldAndFormat;
import org.opensearch.client.opensearch._types.query_dsl.Operator;
import org.opensearch.client.opensearch._types.query_dsl.Query;
import org.opensearch.client.opensearch.core.search.FieldCollapse;
import org.opensearch.client.opensearch.core.search.Highlight;
import org.opensearch.client.opensearch.core.search.Pit;
import org.opensearch.client.opensearch.core.search.Rescore;
import org.opensearch.client.opensearch.core.search.SourceConfig;
import org.opensearch.client.opensearch.core.search.Suggester;
import org.opensearch.client.opensearch.core.search.TrackHits;
import org.opensearch.client.opensearch.core.search.*;
import org.opensearch.client.transport.Endpoint;
import org.opensearch.client.transport.endpoints.SimpleEndpoint;
import org.opensearch.client.util.ApiTypeHelper;
Expand Down Expand Up @@ -1167,7 +1147,7 @@ public Builder toBuilder() {
* Builder for {@link SearchRequest}.
*/

public static class Builder extends ObjectBuilderBase implements ObjectBuilder<SearchRequest> {
public static class Builder extends ObjectBuilderBase implements ObjectBuilder<SearchRequest>, PlainDeserializable<Builder> {
@Nullable
private SourceConfig source;

Expand Down Expand Up @@ -2267,6 +2247,11 @@ public SearchRequest build() {

return new SearchRequest(this);
}

@Override
public Builder get() {
return this;
}
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,8 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.opensearch._types.ErrorResponse;
import org.opensearch.client.opensearch._types.ExpandWildcard;
import org.opensearch.client.opensearch._types.RequestBase;
import org.opensearch.client.opensearch._types.SearchType;
import org.opensearch.client.opensearch._types.Time;
import org.opensearch.client.json.*;
import org.opensearch.client.opensearch._types.*;
import org.opensearch.client.transport.Endpoint;
import org.opensearch.client.transport.endpoints.SimpleEndpoint;
import org.opensearch.client.util.ApiTypeHelper;
Expand Down Expand Up @@ -386,7 +376,7 @@ public Builder toBuilder() {
* Builder for {@link SearchTemplateRequest}.
*/

public static class Builder extends ObjectBuilderBase implements ObjectBuilder<SearchTemplateRequest> {
public static class Builder extends ObjectBuilderBase implements ObjectBuilder<SearchTemplateRequest>, PlainDeserializable<Builder> {
@Nullable
private Boolean allowNoIndices;

Expand Down Expand Up @@ -678,6 +668,11 @@ public SearchTemplateRequest build() {

return new SearchTemplateRequest(this);
}

@Override
public Builder get() {
return this;
}
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@
import java.util.Map;
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;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.json.*;
import org.opensearch.client.opensearch._types.ErrorResponse;
import org.opensearch.client.opensearch._types.RequestBase;
import org.opensearch.client.opensearch._types.Time;
Expand Down Expand Up @@ -225,7 +220,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
* Builder for {@link CreateIndexRequest}.
*/

public static class Builder extends ObjectBuilderBase implements ObjectBuilder<CreateIndexRequest> {
public static class Builder extends ObjectBuilderBase implements ObjectBuilder<CreateIndexRequest>, PlainDeserializable<Builder> {
@Nullable
private Map<String, Alias> aliases;

Expand Down Expand Up @@ -422,6 +417,11 @@ public CreateIndexRequest build() {

return new CreateIndexRequest(this);
}

@Override
public Builder get() {
return this;
}
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,7 @@
import java.util.Map;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.ObjectBuilderDeserializer;
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.json.PlainJsonSerializable;
import org.opensearch.client.json.*;
import org.opensearch.client.opensearch._types.ErrorResponse;
import org.opensearch.client.opensearch._types.RequestBase;
import org.opensearch.client.opensearch.indices.put_index_template.IndexTemplateMapping;
Expand Down Expand Up @@ -234,7 +228,7 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
* Builder for {@link PutIndexTemplateRequest}.
*/

public static class Builder extends ObjectBuilderBase implements ObjectBuilder<PutIndexTemplateRequest> {
public static class Builder extends ObjectBuilderBase implements ObjectBuilder<PutIndexTemplateRequest>, PlainDeserializable<Builder> {
@Nullable
private Map<String, JsonData> meta;

Expand Down Expand Up @@ -385,6 +379,11 @@ public PutIndexTemplateRequest build() {

return new PutIndexTemplateRequest(this);
}

@Override
public Builder get() {
return this;
}
}

// ---------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -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 org.junit.Test;
import org.opensearch.client.opensearch.indices.PutIndexTemplateRequest;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class PlainDeserializableTest {

@Test
public void testWithJsonPutIndexTemplateRequest() {

String stringTemplate =
"{\"name\":\"My index\",\"composed_of\":[\"component1\",\"component2\"],\"index_patterns\":[\"index_pattern1\"]}";
InputStream inputStream = new ByteArrayInputStream(stringTemplate.getBytes(StandardCharsets.UTF_8));

PutIndexTemplateRequest indexTemplateRequest = new PutIndexTemplateRequest.Builder().name("My index 2")
.indexPatterns("index_pattern3")
.withJson(inputStream)
.build();

String expectedName = "My index"; // My index 2 will be replaced by My index from inputStream
List<String> expectedIndexPatterns = List.of("index_pattern3", "index_pattern1");
List<String> expectedComponents = List.of("component1", "component2");

assert expectedName.equals(indexTemplateRequest.name());
assert expectedIndexPatterns.equals(indexTemplateRequest.indexPatterns());
assert expectedComponents.equals(indexTemplateRequest.composedOf());

}
}

0 comments on commit 94a080e

Please sign in to comment.