Skip to content

Commit

Permalink
Made InlineGet source field nullable
Browse files Browse the repository at this point in the history
Addresses bug in #1042

Signed-off-by: Brendon Faleiro <[email protected]>
  • Loading branch information
Brendon Faleiro committed Jun 21, 2024
1 parent d9bad17 commit 9408df8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
### Dependencies

### Changed
- Made InlineGet source field nullable. ([#1042](https://github.com/opensearch-project/opensearch-java/pull/1042))

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class InlineGet<TDocument> implements JsonpSerializable {
@Nullable
private final String routing;

@Nullable
private final TDocument source;

@Nullable
Expand All @@ -84,7 +85,7 @@ private InlineGet(Builder<TDocument> 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;

}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

}
}

// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -240,6 +244,7 @@ public final Builder<TDocument> metadata(String key, JsonData value) {
@Nullable
private String routing;

@Nullable
private TDocument source;

@Nullable
Expand Down Expand Up @@ -298,9 +303,9 @@ public final Builder<TDocument> routing(@Nullable String value) {
}

/**
* Required - API name: {@code _source}
* API name: {@code _source}
*/
public final Builder<TDocument> source(TDocument value) {
public final Builder<TDocument> source(@Nullable TDocument value) {
this.source = value;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 9408df8

Please sign in to comment.