Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 2.11] Made InlineGet source field nullable #1056

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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());
}
}
Loading