From 986a83d9b402162a5284f501a8f894a0a000ad42 Mon Sep 17 00:00:00 2001 From: Nicholas Walter Knize Date: Tue, 7 Feb 2023 09:46:45 -0600 Subject: [PATCH] fix build failure Signed-off-by: Nicholas Walter Knize --- .../common/xcontent/XContentHelper.java | 23 ++++++++++++++++++ .../opensearch/test/OpenSearchTestCase.java | 24 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/server/src/main/java/org/opensearch/common/xcontent/XContentHelper.java b/server/src/main/java/org/opensearch/common/xcontent/XContentHelper.java index 5319ce222c08d..bfc85ce82da74 100644 --- a/server/src/main/java/org/opensearch/common/xcontent/XContentHelper.java +++ b/server/src/main/java/org/opensearch/common/xcontent/XContentHelper.java @@ -497,7 +497,10 @@ public static BytesReference toXContent(ToXContent toXContent, XContentType xCon * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned * by the {@link ToXContent#isFragment()} method returns. + * + * @deprecated use {@link #toXContent(ToXContent, MediaType, Params, boolean)} instead */ + @Deprecated public static BytesReference toXContent(ToXContent toXContent, XContentType xContentType, Params params, boolean humanReadable) throws IOException { try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) { @@ -513,6 +516,26 @@ public static BytesReference toXContent(ToXContent toXContent, XContentType xCon } } + /** + * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided + * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned + * by the {@link ToXContent#isFragment()} method returns. + */ + public static BytesReference toXContent(ToXContent toXContent, MediaType mediaType, Params params, boolean humanReadable) + throws IOException { + try (XContentBuilder builder = XContentBuilder.builder(mediaType.xContent())) { + builder.humanReadable(humanReadable); + if (toXContent.isFragment()) { + builder.startObject(); + } + toXContent.toXContent(builder, params); + if (toXContent.isFragment()) { + builder.endObject(); + } + return BytesReference.bytes(builder); + } + } + /** * Guesses the content type based on the provided bytes. * diff --git a/test/framework/src/main/java/org/opensearch/test/OpenSearchTestCase.java b/test/framework/src/main/java/org/opensearch/test/OpenSearchTestCase.java index ecf0de521f36a..d4090fdfdfdad 100644 --- a/test/framework/src/main/java/org/opensearch/test/OpenSearchTestCase.java +++ b/test/framework/src/main/java/org/opensearch/test/OpenSearchTestCase.java @@ -94,6 +94,7 @@ import org.opensearch.common.util.concurrent.ThreadContext; import org.opensearch.common.util.set.Sets; import org.opensearch.common.xcontent.LoggingDeprecationHandler; +import org.opensearch.common.xcontent.MediaType; import org.opensearch.common.xcontent.NamedXContentRegistry; import org.opensearch.common.xcontent.ToXContent; import org.opensearch.common.xcontent.XContent; @@ -1237,7 +1238,10 @@ public GeohashGenerator() { * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned * by the {@link ToXContent#isFragment()} method returns. Shuffles the keys to make sure that parsing never relies on keys ordering. + * + * @deprecated use {@link #toShuffledXContent(ToXContent, MediaType, ToXContent.Params, boolean, String...)} instead */ + @Deprecated protected final BytesReference toShuffledXContent( ToXContent toXContent, XContentType xContentType, @@ -1253,6 +1257,26 @@ protected final BytesReference toShuffledXContent( } } + /** + * Returns the bytes that represent the XContent output of the provided {@link ToXContent} object, using the provided + * {@link XContentType}. Wraps the output into a new anonymous object according to the value returned + * by the {@link ToXContent#isFragment()} method returns. Shuffles the keys to make sure that parsing never relies on keys ordering. + */ + protected final BytesReference toShuffledXContent( + ToXContent toXContent, + MediaType mediaType, + ToXContent.Params params, + boolean humanReadable, + String... exceptFieldNames + ) throws IOException { + BytesReference bytes = XContentHelper.toXContent(toXContent, mediaType, params, humanReadable); + try (XContentParser parser = createParser(mediaType.xContent(), bytes)) { + try (XContentBuilder builder = shuffleXContent(parser, rarely(), exceptFieldNames)) { + return BytesReference.bytes(builder); + } + } + } + /** * Randomly shuffles the fields inside objects in the {@link XContentBuilder} passed in. * Recursively goes through inner objects and also shuffles them. Exceptions for this