Skip to content

Commit 611140c

Browse files
committed
Fix SemanticHighlighterExtBuilderTests.toXContent
SearchExtBuilder expects toXContent to produce their encloding field name. Add test to ensure we can do a round trip with XContent. Fixes #1906. Signed-off-by: David Causse <dcausse@wikimedia.org>
1 parent 127a664 commit 611140c

2 files changed

Lines changed: 44 additions & 8 deletions

File tree

src/main/java/org/opensearch/neuralsearch/query/ext/SemanticHighlighterExtBuilder.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
*/
55
package org.opensearch.neuralsearch.query.ext;
66

7-
import java.io.IOException;
8-
import java.util.Locale;
9-
import java.util.Objects;
10-
117
import org.opensearch.core.common.io.stream.StreamInput;
128
import org.opensearch.core.common.io.stream.StreamOutput;
139
import org.opensearch.core.xcontent.XContentBuilder;
1410
import org.opensearch.core.xcontent.XContentParser;
1511
import org.opensearch.neuralsearch.highlight.SemanticHighlightingConstants;
1612
import org.opensearch.search.SearchExtBuilder;
1713

14+
import java.io.IOException;
15+
import java.util.Locale;
16+
import java.util.Objects;
17+
1818
/**
1919
* Search request ext builder that opts a request into batch semantic highlighting.
2020
*
@@ -61,7 +61,7 @@ public void writeTo(StreamOutput out) throws IOException {
6161

6262
@Override
6363
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
64-
return builder.value(enabled);
64+
return builder.field(NAME, enabled);
6565
}
6666

6767
@Override

src/test/java/org/opensearch/neuralsearch/query/ext/SemanticHighlighterExtBuilderTests.java

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@
44
*/
55
package org.opensearch.neuralsearch.query.ext;
66

7-
import java.io.IOException;
8-
import java.util.List;
9-
107
import org.opensearch.common.io.stream.BytesStreamOutput;
8+
import org.opensearch.common.xcontent.XContentType;
119
import org.opensearch.core.ParseField;
10+
import org.opensearch.core.common.bytes.BytesReference;
1211
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
1312
import org.opensearch.core.common.io.stream.StreamInput;
13+
import org.opensearch.core.xcontent.MediaType;
1414
import org.opensearch.core.xcontent.NamedXContentRegistry;
15+
import org.opensearch.core.xcontent.ToXContentObject;
16+
import org.opensearch.core.xcontent.XContentBuilder;
1517
import org.opensearch.core.xcontent.XContentParser;
1618
import org.opensearch.search.SearchExtBuilder;
1719
import org.opensearch.test.OpenSearchTestCase;
1820

21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.util.List;
24+
1925
public class SemanticHighlighterExtBuilderTests extends OpenSearchTestCase {
2026

2127
@Override
@@ -57,6 +63,14 @@ public void testRoundTripStreamingFalse() throws IOException {
5763
roundTrip(false);
5864
}
5965

66+
public void testRoundTripXContentTrue() throws IOException {
67+
roundTripXContent(true);
68+
}
69+
70+
public void testRoundTripXContentFalse() throws IOException {
71+
roundTripXContent(false);
72+
}
73+
6074
public void testParseBooleanTrue() throws IOException {
6175
SemanticHighlighterExtBuilder result = parseValue("true");
6276
assertTrue(result.isEnabled());
@@ -97,6 +111,28 @@ private void roundTrip(boolean value) throws IOException {
97111
}
98112
}
99113

114+
private void roundTripXContent(boolean value) throws IOException {
115+
SemanticHighlighterExtBuilder original = new SemanticHighlighterExtBuilder(value);
116+
MediaType xContentType = randomFrom(XContentType.values());
117+
118+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
119+
XContentBuilder builder = xContentType.contentBuilder(outputStream);
120+
builder.startObject();
121+
original.toXContent(builder, ToXContentObject.EMPTY_PARAMS);
122+
builder.endObject();
123+
BytesReference originalBytes = BytesReference.bytes(builder);
124+
125+
try (XContentParser parser = this.createParser(xContentType.xContent(), originalBytes)) {
126+
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
127+
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
128+
assertEquals(SemanticHighlighterExtBuilder.NAME, parser.currentName());
129+
parser.nextToken(); // advance to the value
130+
SearchExtBuilder deserialized = parser.namedObject(SearchExtBuilder.class, SemanticHighlighterExtBuilder.NAME, null);
131+
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
132+
assertEquals(original, deserialized);
133+
}
134+
}
135+
100136
private SemanticHighlighterExtBuilder parseValue(String json) throws IOException {
101137
XContentParser parser = createParser(org.opensearch.common.xcontent.XContentType.JSON.xContent(), json);
102138
// advance past START_TOKEN to the value

0 commit comments

Comments
 (0)