Skip to content

Commit

Permalink
Fix breaking change caused by opensearch core (#1187)
Browse files Browse the repository at this point in the history
* Fix breaking change caused by opensearch core

Signed-off-by: zane-neo <[email protected]>

* Use builder.toString() to get a builder string value and format code

Signed-off-by: zane-neo <[email protected]>

---------

Signed-off-by: zane-neo <[email protected]>
  • Loading branch information
zane-neo authored Aug 9, 2023
1 parent 7bdc5e4 commit d3d4122
Show file tree
Hide file tree
Showing 66 changed files with 129 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.opensearch.common.util.CollectionUtils;
import org.opensearch.commons.authuser.User;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.util.CollectionUtils;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.text.StringSubstitutor;
import org.opensearch.common.Strings;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
Expand Down Expand Up @@ -93,7 +92,7 @@ static Connector fromStream(StreamInput in) throws IOException {
}

static Connector createConnector(XContentBuilder builder, String connectorProtocol) throws IOException {
String jsonStr = Strings.toString(builder);
String jsonStr = builder.toString();
return createConnector(jsonStr, connectorProtocol);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.common.util.CollectionUtils;
import org.opensearch.core.common.util.CollectionUtils;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
Expand Down
7 changes: 3 additions & 4 deletions common/src/test/java/org/opensearch/ml/common/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package org.opensearch.ml.common;

import org.opensearch.common.Strings;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.XContentFactory;
Expand All @@ -26,15 +25,15 @@ public static <T> void testParse(ToXContentObject obj, Function<XContentParser,
}

public static <T> void testParse(ToXContentObject obj, Function<XContentParser, T> function, boolean wrapWithObject) throws IOException {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
XContentBuilder builder = XContentFactory.jsonBuilder();
if (wrapWithObject) {
builder.startObject();
}
obj.toXContent(builder, ToXContent.EMPTY_PARAMS);
if (wrapWithObject) {
builder.endObject();
}
String jsonStr = Strings.toString(builder);
String jsonStr = builder.toString();
testParseFromString(obj, jsonStr, function);
}

Expand All @@ -46,7 +45,7 @@ public static <T> void testParseFromString(ToXContentObject obj, String jsonStr,
}

public static String contentObjectToString(ToXContentObject obj) throws IOException {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
XContentBuilder builder = XContentFactory.jsonBuilder();
obj.toXContent(builder, ToXContent.EMPTY_PARAMS);
return xContentBuilderToString(builder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
package org.opensearch.ml.common.dataframe;

import org.junit.Test;
import org.opensearch.common.Strings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.XContentBuilder;

import java.io.IOException;
Expand All @@ -29,11 +27,11 @@ public void booleanValue() {
@Test
public void testToXContent() throws IOException {
BooleanValue value = new BooleanValue(true);
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
XContentBuilder builder = XContentFactory.jsonBuilder();
value.toXContent(builder);

assertNotNull(builder);
String jsonStr = Strings.toString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"BOOLEAN\",\"value\":true}", jsonStr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

import org.junit.Before;
import org.junit.Test;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.ml.common.TestHelper;
Expand Down Expand Up @@ -51,11 +49,11 @@ public void writeToAndReadStream() throws IOException {

@Test
public void testToXContent() throws IOException {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
XContentBuilder builder = XContentFactory.jsonBuilder();
columnMeta.toXContent(builder);

assertNotNull(builder);
String jsonStr = Strings.toString(builder);
String jsonStr = builder.toString();
assertEquals("{\"name\":\"columnMetaName\",\"column_type\":\"STRING\"}", jsonStr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.ml.common.TestHelper;
Expand Down Expand Up @@ -227,13 +225,13 @@ public void select_Exception_InvalidColumn(){

@Test
public void testToXContent() throws IOException {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
defaultDataFrame.toXContent(builder);
builder.endObject();

assertNotNull(builder);
String jsonStr = Strings.toString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_metas\":[" +
"{\"name\":\"c1\",\"column_type\":\"STRING\"}," +
"{\"name\":\"c2\",\"column_type\":\"INTEGER\"}," +
Expand Down Expand Up @@ -274,4 +272,4 @@ public void testParse_WrongExtraField() throws IOException {
"[{\"column_type\":\"INTEGER\",\"value\":2}]}]}";
TestHelper.testParseFromString(dataFrame, jsonStr, function);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import java.io.IOException;

import org.junit.Test;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.XContentBuilder;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -47,11 +45,11 @@ public void writeTo() throws IOException {
@Test
public void testToXContent() throws IOException {
DoubleValue doubleValue = new DoubleValue(5.0D);
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
XContentBuilder builder = XContentFactory.jsonBuilder();
doubleValue.toXContent(builder);

assertNotNull(builder);
String jsonStr = Strings.toString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"DOUBLE\",\"value\":5.0}", jsonStr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
package org.opensearch.ml.common.dataframe;

import org.junit.Test;
import org.opensearch.common.Strings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.XContentBuilder;

import java.io.IOException;
Expand All @@ -30,11 +28,11 @@ public void floatValue() {
@Test
public void testToXContent() throws IOException {
FloatValue floatValue = new FloatValue(2.1f);
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
XContentBuilder builder = XContentFactory.jsonBuilder();
floatValue.toXContent(builder);

assertNotNull(builder);
String jsonStr = Strings.toString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"FLOAT\",\"value\":2.1}", jsonStr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
package org.opensearch.ml.common.dataframe;

import org.junit.Test;
import org.opensearch.common.Strings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.XContentBuilder;

import java.io.IOException;
Expand All @@ -29,11 +27,11 @@ public void intValue() {
@Test
public void testToXContent() throws IOException {
IntValue intValue = new IntValue(2);
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
XContentBuilder builder = XContentFactory.jsonBuilder();
intValue.toXContent(builder);

assertNotNull(builder);
String jsonStr = Strings.toString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"INTEGER\",\"value\":2}", jsonStr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
package org.opensearch.ml.common.dataframe;

import org.junit.Test;
import org.opensearch.common.Strings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.XContentBuilder;

import java.io.IOException;
Expand All @@ -29,11 +27,11 @@ public void longValue() {
@Test
public void testToXContent() throws IOException {
LongValue longValue = new LongValue((long)2);
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
XContentBuilder builder = XContentFactory.jsonBuilder();
longValue.toXContent(builder);

assertNotNull(builder);
String jsonStr = Strings.toString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"LONG\",\"value\":2}", jsonStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
package org.opensearch.ml.common.dataframe;

import org.junit.Test;
import org.opensearch.common.Strings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;

Expand All @@ -30,11 +28,11 @@ public void getValue() {
@Test
public void testToXContent() throws IOException {
NullValue value = new NullValue();
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
XContentBuilder builder = XContentFactory.jsonBuilder();
value.toXContent(builder, ToXContent.EMPTY_PARAMS);

assertNotNull(builder);
String jsonStr = Strings.toString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"NULL\"}", jsonStr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;

Expand Down Expand Up @@ -133,11 +131,11 @@ public void select() {

@Test
public void testToXContent() throws IOException {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
XContentBuilder builder = XContentFactory.jsonBuilder();
row.toXContent(builder);

assertNotNull(builder);
String jsonStr = Strings.toString(builder);
String jsonStr = builder.toString();

assertEquals("{\"values\":[{\"column_type\":\"INTEGER\",\"value\":0}]}", jsonStr);
}
Expand Down Expand Up @@ -248,4 +246,4 @@ public void testEquals_SomeTypeNotMatch() {
assertFalse(row1.equals(row2));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
package org.opensearch.ml.common.dataframe;

import org.junit.Test;
import org.opensearch.common.Strings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.XContentBuilder;

import java.io.IOException;
Expand All @@ -29,11 +27,11 @@ public void shortValue() {
@Test
public void testToXContent() throws IOException {
ShortValue shortValue = new ShortValue((short)2);
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
XContentBuilder builder = XContentFactory.jsonBuilder();
shortValue.toXContent(builder);

assertNotNull(builder);
String jsonStr = Strings.toString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"SHORT\",\"value\":2}", jsonStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
package org.opensearch.ml.common.dataframe;

import org.junit.Test;
import org.opensearch.common.Strings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;

Expand All @@ -34,11 +32,11 @@ public void stringValue_NullPointerException() {
@Test
public void testToXContent() throws IOException {
StringValue value = new StringValue("str");
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
XContentBuilder builder = XContentFactory.jsonBuilder();
value.toXContent(builder, ToXContent.EMPTY_PARAMS);

assertNotNull(builder);
String jsonStr = Strings.toString(builder);
String jsonStr = builder.toString();
assertEquals("{\"column_type\":\"STRING\",\"value\":\"str\"}", jsonStr);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.common.settings.Settings;
Expand Down Expand Up @@ -150,10 +149,10 @@ public void parse_TextEmbedding_NullResultFilter() throws IOException {

private void testParse(FunctionName algorithm, MLInputDataset inputDataset, String expectedInputStr, Consumer<MLInput> verify) throws IOException {
MLInput input = MLInput.builder().inputDataset(inputDataset).algorithm(algorithm).build();
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
XContentBuilder builder = XContentFactory.jsonBuilder();
input.toXContent(builder, ToXContent.EMPTY_PARAMS);
assertNotNull(builder);
String jsonStr = Strings.toString(builder);
String jsonStr = builder.toString();
assertEquals(expectedInputStr, jsonStr);

XContentParser parser = XContentType.JSON.xContent().createParser(new NamedXContentRegistry(new SearchModule(Settings.EMPTY,
Expand Down
Loading

0 comments on commit d3d4122

Please sign in to comment.