diff --git a/CHANGELOG.md b/CHANGELOG.md index 4fcf0bb496..557befb07d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Dependencies ### Changed +- Made InlineGet source field nullable. ([#1042](https://github.com/opensearch-project/opensearch-java/pull/1042)) ### Deprecated diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/InlineGet.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/InlineGet.java index cdebb2c397..35b87c0045 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/InlineGet.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/InlineGet.java @@ -68,6 +68,7 @@ public class InlineGet implements JsonpSerializable { @Nullable private final String routing; + @Nullable private final TDocument source; @Nullable @@ -84,7 +85,7 @@ private InlineGet(Builder builder) { this.seqNo = builder.seqNo; this.primaryTerm = builder.primaryTerm; this.routing = builder.routing; - this.source = ApiTypeHelper.requireNonNull(builder.source, this, "source"); + this.source = builder.source; this.tDocumentSerializer = builder.tDocumentSerializer; } @@ -139,8 +140,9 @@ public final String routing() { } /** - * Required - API name: {@code _source} + * API name: {@code _source} */ + @Nullable public final TDocument source() { return this.source; } @@ -191,9 +193,11 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { generator.write(this.routing); } - generator.writeKey("_source"); - JsonpUtils.serialize(this.source, generator, tDocumentSerializer, mapper); + if (this.source != null) { + generator.writeKey("_source"); + JsonpUtils.serialize(this.source, generator, tDocumentSerializer, mapper); + } } // --------------------------------------------------------------------------------------------- @@ -240,6 +244,7 @@ public final Builder metadata(String key, JsonData value) { @Nullable private String routing; + @Nullable private TDocument source; @Nullable @@ -298,9 +303,9 @@ public final Builder routing(@Nullable String value) { } /** - * Required - API name: {@code _source} + * API name: {@code _source} */ - public final Builder source(TDocument value) { + public final Builder source(@Nullable TDocument value) { this.source = value; return this; } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/_types/InlineGetTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/_types/InlineGetTest.java new file mode 100644 index 0000000000..a5caa2b93b --- /dev/null +++ b/java-client/src/test/java/org/opensearch/client/opensearch/_types/InlineGetTest.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.opensearch._types; + +import static org.junit.Assert.assertEquals; + +import jakarta.json.stream.JsonParser; +import java.io.StringReader; +import org.junit.Test; +import org.opensearch.client.json.JsonpDeserializer; +import org.opensearch.client.json.jackson.JacksonJsonpMapper; + +public class InlineGetTest { + @Test + public void testInlineGet_withSource() { + final InlineGet inlineGet = InlineGet.of( + b -> b.found(true).seqNo(1L).primaryTerm(2L).routing("routing").source("{\"name\":\"John Doe\"}") + ); + + final String jsonString = "{\"found\":true,\"_seq_no\":1,\"_primary_term\":2,\"_routing\":\"routing\"," + + "\"_source\":\"{\\\"name\\\":\\\"John Doe\\\"}\"}"; + + final StringReader reader = new StringReader(jsonString); + final JacksonJsonpMapper mapper = new JacksonJsonpMapper(); + final JsonParser parser = mapper.jsonProvider().createParser(reader); + final InlineGet actual = InlineGet.createInlineGetDeserializer(JsonpDeserializer.stringDeserializer()).deserialize(parser, mapper); + assertEquals(inlineGet.found(), actual.found()); + assertEquals(inlineGet.seqNo(), actual.seqNo()); + assertEquals(inlineGet.primaryTerm(), actual.primaryTerm()); + assertEquals(inlineGet.routing(), actual.routing()); + assertEquals(inlineGet.source(), actual.source()); + } + + @Test + public void testInlineGet_withoutSource() { + final InlineGet inlineGet = InlineGet.of(b -> b.found(true).seqNo(1L).primaryTerm(2L).routing("routing")); + + final String jsonString = "{\"found\":true,\"_seq_no\":1,\"_primary_term\":2,\"_routing\":\"routing\"}"; + + final StringReader reader = new StringReader(jsonString); + final JacksonJsonpMapper mapper = new JacksonJsonpMapper(); + final JsonParser parser = mapper.jsonProvider().createParser(reader); + final InlineGet actual = InlineGet.createInlineGetDeserializer(JsonpDeserializer.stringDeserializer()).deserialize(parser, mapper); + assertEquals(inlineGet.found(), actual.found()); + assertEquals(inlineGet.seqNo(), actual.seqNo()); + assertEquals(inlineGet.primaryTerm(), actual.primaryTerm()); + assertEquals(inlineGet.routing(), actual.routing()); + } +}