Skip to content

Commit

Permalink
588 - [FEATURE] serialize requests to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Jai2305 committed Jul 2, 2024
1 parent 3af2d77 commit 242f5f9
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This section is for maintaining a changelog for all breaking changes for the cli

### Added
- Document HTTP/2 support ([#330](https://github.com/opensearch-project/opensearch-java/pull/330))
- Add a serializer method for classes implementing JsonpSerializable.

### Dependencies

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,33 @@
package org.opensearch.client.json;

import jakarta.json.stream.JsonGenerator;
import org.opensearch.client.json.jackson.JacksonJsonProvider;
import org.opensearch.client.json.jackson.JacksonJsonpMapper;

import java.io.StringWriter;

/**
* An object that is its own JsonP serializer
*/
public interface JsonpSerializable {

void serialize(JsonGenerator generator, JsonpMapper mapper);


/**
* A default method which returns string representation for the instances of classes
* implementing JsonpSerializable interface.<br>
* Usage : Eg for SearchRequest.class<pre>{@code SearchRequest implements JsonpSerializable{..}
* SearchRequest searchRequest = SearchRequest.of(request -> request...);
* String searchRequestString = searchRequest.writeValueAsString();}</pre> <br>
*
*/
default String writeValueAsString() {
StringWriter writer = new StringWriter();
try (JsonGenerator generator = JacksonJsonProvider.provider().createGenerator(writer)) {
serialize(generator, new JacksonJsonpMapper());
};
return writer.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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.
*/
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

package org.opensearch.client.opensearch.json;

import org.junit.Assert;
import org.junit.Test;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch._types.Result;
import org.opensearch.client.opensearch.core.IndexResponse;
import org.opensearch.client.opensearch.core.SearchRequest;
import org.opensearch.client.util.MissingRequiredPropertyException;

import java.util.Collections;

public class JsonpSerializableTest extends Assert {

// Test IndexResponse which extends WriteResponseBase which implements JsonpSerializable
@Test
public void testIndexResponse() {

String expectedStringValue = "{\"_id\":\"id\",\"_index\":\"index\",\"_primary_term\":1,\"result\":\"created\",\"_seq_no\":2,\"_shards\":{\"failed\":1.0,\"successful\":1.0,\"total\":3.0,\"failures\":[{\"index\":\"index\",\"node\":\"node\",\"reason\":{\"type\":\"query_shard_exception\",\"reason\":\"Failed to create query.\"},\"shard\":1,\"status\":\"Failed\"}],\"skipped\":1.0},\"_version\":3}";
IndexResponse indexResponse = IndexResponse.of(response -> response
.result(Result.Created)
.index("index")
.id("id")
.primaryTerm(1)
.seqNo(2)
.version(3)
.shards(shardStats -> shardStats
.total(3)
.successful(1)
.skipped(1)
.failed(1)
.failures(shardFailure->shardFailure
.index("index")
.node("node")
.shard(1)
.status("Failed")
.reason(cause->cause
.type("query_shard_exception")
.reason("Failed to create query.")
)
)
)
);

String indexResponseString = indexResponse.writeValueAsString();
assertEquals(expectedStringValue, indexResponseString);
}

// Test SearchRequest which implements JsonpSerializable
@Test
public void testSearchResponse() {

String expectedStringValue = "{\"aggregations\":{},\"query\":{\"match\":{\"name\":{\"query\":\"OpenSearch\"}}},\"terminate_after\":5}";
SearchRequest searchRequest = SearchRequest.of(request -> request
.index("index1","index2")
.aggregations(Collections.emptyMap())
.terminateAfter(5L)
.query(q -> q
.match(t -> t
.field("name")
.query(FieldValue.of("OpenSearch"))
)
)
);
String searchRequestString = searchRequest.writeValueAsString();
assertEquals(expectedStringValue, searchRequestString);

}

}

0 comments on commit 242f5f9

Please sign in to comment.