Skip to content

Commit

Permalink
Merge pull request #5 from mkhludnev/2.10-tbc
Browse files Browse the repository at this point in the history
another attempt to fix ToXContent
  • Loading branch information
macohen committed Jan 18, 2024
2 parents a7c1728 + c9c26fb commit 270f713
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
import org.opensearch.core.common.ParsingException;
import org.opensearch.core.common.io.stream.NamedWriteable;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentParser;

public interface StorableElement extends ToXContent, NamedWriteable {
public interface StorableElement extends ToXContent, NamedWriteable, ToXContentObject {
/**
* @return the element name
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ObjectParser;
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 @@ -31,6 +31,7 @@
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.core.xcontent.ObjectParser;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.common.xcontent.json.JsonXContent;
Expand Down Expand Up @@ -197,11 +198,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

@Override
public boolean isFragment() {
return false;// since toXContent already have enclosing start/endObject braces
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ public static LoggingSearchExtBuilder parse(XContentParser parser) throws IOExce

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
builder.field(LOG_SPECS.getPreferredName(), logSpecs);
return builder.endObject();
//builder.startObject(); //since it's marked as XContentFragment,
// so Strings.toString(org.opensearch.core.xcontent.MediaType, org.opensearch.core.xcontent.ToXContent, org.opensearch.core.xcontent.ToXContent.Params, boolean, boolean)
// embraces fragment
return builder.field(LOG_SPECS.getPreferredName(), logSpecs);
//return builder.endObject();
}

public Stream<LogSpec> logSpecsStream() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public void testToXContent() throws IOException {
String featureString = generateTestFeature();
StoredFeature feature = parse(featureString);
XContentBuilder builder = MediaTypeRegistry.contentBuilder(XContentType.JSON);
featureString = String.valueOf(feature.toXContent(builder, ToXContent.EMPTY_PARAMS));
//featureString = String.valueOf(feature.toXContent(builder, ToXContent.EMPTY_PARAMS));
featureString = Strings.toString(XContentType.JSON, feature);//.toXContent(builder, ToXContent.EMPTY_PARAMS));
StoredFeature featureReparsed = parse(featureString);
assertTestFeature(featureReparsed);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ public void testToXContent() throws IOException {
String featureSetString = generateRandomFeatureSet("my_set", features::add);
StoredFeatureSet featureSet = parse(featureSetString);

XContentBuilder builder = XContentFactory.jsonBuilder();//contentBuilder(XContentType.JSON);

featureSetString = Strings.toString(XContentType.JSON, featureSet);//.toXContent(builder, ToXContent.EMPTY_PARAMS));
StoredFeatureSet featureSetReparsed = parse(featureSetString);
assertFeatureSet(featureSetReparsed, features);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ public void testParse() throws IOException {

public void testToXContent() throws IOException {
LoggingSearchExtBuilder ext1 = buildTestExt();
XContentBuilder builder = XContentFactory.jsonBuilder();
ext1.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.close();
// this builder goes, nowhere
//XContentBuilder builder = XContentFactory.jsonBuilder();
// fails with "Can not write a field name, expecting a value", Strings.toString() must embrace the fragment
// ext1.toXContent(builder, ToXContent.EMPTY_PARAMS);
//builder.close();
assertEquals(getTestExtAsString(), Strings.toString(XContentType.JSON, ext1));
}

Expand Down

0 comments on commit 270f713

Please sign in to comment.