Skip to content

Commit

Permalink
Make MultiSearchItem.status optional (opensearch-project#660)
Browse files Browse the repository at this point in the history
* Add failing msearchTemplate integration test

Signed-off-by: Thomas Farr <[email protected]>

* Make `MultiSearchItem.status` optional

Signed-off-by: Thomas Farr <[email protected]>

* Add changelog entry

Signed-off-by: Thomas Farr <[email protected]>

* spotlessApply

Signed-off-by: Thomas Farr <[email protected]>

---------

Signed-off-by: Thomas Farr <[email protected]>
(cherry picked from commit d2916e4)
  • Loading branch information
Xtansia committed Oct 12, 2023
1 parent 119cabc commit d269c41
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Fixed CVE-2976 + added CVE checker ([#624](https://github.com/opensearch-project/opensearch-java/pull/624))
- 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 deserialization of MsearchTemplateResponse ([#660](https://github.com/opensearch-project/opensearch-java/pull/660))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,26 @@
import jakarta.json.stream.JsonGenerator;
import java.util.function.Function;
import java.util.function.Supplier;
import javax.annotation.Nullable;
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.opensearch.core.SearchResponse;
import org.opensearch.client.util.ApiTypeHelper;
import org.opensearch.client.util.ObjectBuilder;

// typedef: _global.msearch.MultiSearchItem

public class MultiSearchItem<TDocument> extends SearchResponse<TDocument> {
private final int status;
@Nullable
private final Integer status;

// ---------------------------------------------------------------------------------------------

private MultiSearchItem(Builder<TDocument> builder) {
super(builder);

this.status = ApiTypeHelper.requireNonNull(builder.status, this, "status");
this.status = builder.status;

}

Expand All @@ -62,17 +63,21 @@ public static <TDocument> MultiSearchItem<TDocument> of(Function<Builder<TDocume
}

/**
* Required - API name: {@code status}
* API name: {@code status}
*/
public final int status() {
@Nullable
public final Integer status() {
return this.status;
}

protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

super.serializeInternal(generator, mapper);
generator.writeKey("status");
generator.write(this.status);

if (this.status != null) {
generator.writeKey("status");
generator.write(this.status);
}

}

Expand All @@ -85,12 +90,13 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
public static class Builder<TDocument> extends SearchResponse.AbstractBuilder<TDocument, Builder<TDocument>>
implements
ObjectBuilder<MultiSearchItem<TDocument>> {
@Nullable
private Integer status;

/**
* Required - API name: {@code status}
* API name: {@code status}
*/
public final Builder<TDocument> status(int value) {
public final Builder<TDocument> status(@Nullable Integer value) {
this.status = value;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@ public void testTemplateSearchAggregations() throws Exception {

}

@Test
public void testMultiSearchTemplate() throws Exception {
var index = "test-msearch-template";
createDocuments(index);

var searchResponse = javaClient().msearchTemplate(
request -> request.searchTemplates(
r -> r.header(h -> h.index(index))
.body(
t -> t.id(TEST_SEARCH_TEMPLATE)
.params("title", JsonData.of("Document"))
.params("suggs", JsonData.of(false))
.params("aggs", JsonData.of(false))
)
),
SimpleDoc.class
);

assertEquals(1, searchResponse.responses().size());
var response = searchResponse.responses().get(0);
assertTrue(response.isResult());
assertNull(response.result().status());
assertEquals(4, response.result().hits().hits().size());
}

private SearchTemplateResponse<SimpleDoc> sendTemplateRequest(String index, String title, boolean suggs, boolean aggs)
throws IOException {
return javaClient().searchTemplate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void testMultiSearchResponse() {

assertEquals(2, response.responses().size());
assertEquals(404, response.responses().get(0).failure().status());
assertEquals(200, response.responses().get(1).result().status());
assertEquals((Integer) 200, response.responses().get(1).result().status());
}

public static class AttributedJacksonJsonpMapper extends JacksonJsonpMapper {
Expand Down

0 comments on commit d269c41

Please sign in to comment.