Skip to content

Commit

Permalink
[Refactor] From XContentType.JSON to MediaTypeRegistry.JSON (opensear…
Browse files Browse the repository at this point in the history
…ch-project#9156)

This commit rote refactors nearly all instances and usages of the
XContentType.JSON singleton instance defined in the
:libs:opensearch-x-content library to the MediaTypeRegistry.JSON
singleton instance defined in :libs:opensearch-core. This decouples the
server and test implementations from the x-content library such that
foundation classes can be further refactored from the :server module to
the :opensearch-core library without requiring a strict dependency on
the x-content library. This keeps opensearch-core library loosely
coupled with the remaining :libs:opensearch-* libraries such that
serverless and cloud-native implementations can selective import only
the libraries needed for implementation.

Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
  • Loading branch information
nknize authored Aug 9, 2023
1 parent 15b7de0 commit 7278f43
Show file tree
Hide file tree
Showing 487 changed files with 2,523 additions and 2,210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.common.util.CollectionUtils;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.Strings;
import org.opensearch.core.xcontent.DeprecationHandler;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContent;
Expand Down Expand Up @@ -119,7 +119,7 @@
* @opensearch.api
*/
final class RequestConverters {
static final XContentType REQUEST_BODY_CONTENT_TYPE = XContentType.JSON;
static final MediaType REQUEST_BODY_CONTENT_TYPE = MediaTypeRegistry.JSON;

private RequestConverters() {
// Contains only status utility methods
Expand Down Expand Up @@ -177,7 +177,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
}

if (bulkContentType == null) {
bulkContentType = XContentType.JSON;
bulkContentType = MediaTypeRegistry.JSON;
}

final byte separator = bulkContentType.xContent().streamSeparator();
Expand Down Expand Up @@ -266,7 +266,12 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
}
}
} else if (opType == DocWriteRequest.OpType.UPDATE) {
source = XContentHelper.toXContent((UpdateRequest) action, bulkContentType, ToXContent.EMPTY_PARAMS, false).toBytesRef();
source = org.opensearch.core.xcontent.XContentHelper.toXContent(
(UpdateRequest) action,
bulkContentType,
ToXContent.EMPTY_PARAMS,
false
).toBytesRef();
}

if (source != null) {
Expand Down Expand Up @@ -821,7 +826,8 @@ static HttpEntity createEntity(ToXContent toXContent, MediaType mediaType) throw
}

static HttpEntity createEntity(ToXContent toXContent, MediaType mediaType, ToXContent.Params toXContentParams) throws IOException {
BytesRef source = XContentHelper.toXContent(toXContent, mediaType, toXContentParams, false).toBytesRef();
BytesRef source = org.opensearch.core.xcontent.XContentHelper.toXContent(toXContent, mediaType, toXContentParams, false)
.toBytesRef();
return new ByteArrayEntity(source.bytes, source.offset, source.length, createContentType(mediaType));
}

Expand Down Expand Up @@ -868,12 +874,12 @@ static String endpoint(String[] indices, String endpoint, String type) {
}

/**
* Returns a {@link ContentType} from a given {@link XContentType}.
* Returns a {@link ContentType} from a given {@link MediaType}.
*
* @param mediaType the {@link MediaType}
* @return the {@link ContentType}
*/
@SuppressForbidden(reason = "Only allowed place to convert a XContentType to a ContentType")
@SuppressForbidden(reason = "Only allowed place to convert a MediaType to a ContentType")
public static ContentType createContentType(final MediaType mediaType) {
return ContentType.create(mediaType.mediaTypeWithoutParameters(), (Charset) null);
}
Expand Down Expand Up @@ -1252,7 +1258,7 @@ Params withWaitForEvents(Priority waitForEvents) {
*/
static MediaType enforceSameContentType(IndexRequest indexRequest, @Nullable MediaType mediaType) {
MediaType requestContentType = indexRequest.getContentType();
if (requestContentType != XContentType.JSON && requestContentType != XContentType.SMILE) {
if (requestContentType != MediaTypeRegistry.JSON && requestContentType != MediaTypeRegistry.fromFormat("smile")) {
throw new IllegalArgumentException(
"Unsupported content-type found for request with content-type ["
+ requestContentType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
import org.opensearch.core.ParseField;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.xcontent.ConstructingObjectParser;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.ObjectParser;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.index.mapper.Mapper;

import java.io.IOException;
Expand Down Expand Up @@ -150,7 +150,7 @@ public String fullName() {
* Returns the mappings as a map. Note that the returned map has a single key which is always the field's {@link Mapper#name}.
*/
public Map<String, Object> sourceAsMap() {
return XContentHelper.convertToMap(source, true, XContentType.JSON).v2();
return XContentHelper.convertToMap(source, true, MediaTypeRegistry.JSON).v2();
}

// pkg-private for testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,10 @@ public Settings settings() {
* Adds mapping that will be added when the index gets created.
*
* @param source The mapping source
* @param xContentType The type of content contained within the source
* @param mediaType The type of content contained within the source
*/
public PutIndexTemplateRequest mapping(String source, XContentType xContentType) {
internalMapping(XContentHelper.convertToMap(new BytesArray(source), true, xContentType).v2());
public PutIndexTemplateRequest mapping(String source, MediaType mediaType) {
internalMapping(XContentHelper.convertToMap(new BytesArray(source), true, mediaType).v2());
return this;
}

Expand Down Expand Up @@ -268,7 +268,7 @@ public PutIndexTemplateRequest mapping(Map<String, Object> source) {

private PutIndexTemplateRequest internalMapping(Map<String, Object> source) {
try {
XContentBuilder builder = MediaTypeRegistry.contentBuilder(XContentType.JSON);
XContentBuilder builder = MediaTypeRegistry.JSON.contentBuilder();
builder.map(source);
MediaType mediaType = builder.contentType();
Objects.requireNonNull(mediaType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
package org.opensearch.client.slm;

import org.opensearch.common.Nullable;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.ParseField;
import org.opensearch.core.common.Strings;
import org.opensearch.core.xcontent.ConstructingObjectParser;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
Expand Down Expand Up @@ -169,6 +169,6 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return Strings.toString(XContentType.JSON, this);
return Strings.toString(MediaTypeRegistry.JSON, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
package org.opensearch.client.slm;

import org.opensearch.common.Nullable;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.ParseField;
import org.opensearch.core.common.Strings;
import org.opensearch.core.xcontent.ConstructingObjectParser;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
Expand Down Expand Up @@ -289,7 +289,7 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return Strings.toString(XContentType.JSON, this);
return Strings.toString(MediaTypeRegistry.JSON, this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
package org.opensearch.client.slm;

import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.ParseField;
import org.opensearch.core.common.Strings;
import org.opensearch.core.xcontent.ConstructingObjectParser;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.ToXContentFragment;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
Expand Down Expand Up @@ -188,7 +188,7 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return Strings.toString(XContentType.JSON, this);
return Strings.toString(MediaTypeRegistry.JSON, this);
}

public static class SnapshotPolicyStats implements ToXContentFragment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@

import org.opensearch.common.Nullable;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.ParseField;
import org.opensearch.core.common.Strings;
import org.opensearch.core.xcontent.ConstructingObjectParser;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
Expand Down Expand Up @@ -151,6 +151,6 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return Strings.toString(XContentType.JSON, this);
return Strings.toString(MediaTypeRegistry.JSON, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

@defaultMessage Use Request#createContentType(XContentType) to be sure to pass the right MIME type
@defaultMessage Use Request#createContentType(MediaType) to be sure to pass the right MIME type
org.apache.hc.core5.http.ContentType#create(java.lang.String)
org.apache.hc.core5.http.ContentType#create(java.lang.String,java.lang.String)
org.apache.hc.core5.http.ContentType#create(java.lang.String,java.nio.charset.Charset)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
import org.opensearch.action.get.MultiGetResponse;
import org.opensearch.action.index.IndexRequest;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.unit.ByteSizeUnit;
import org.opensearch.core.common.unit.ByteSizeValue;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.search.SearchHit;
import org.hamcrest.Matcher;

Expand Down Expand Up @@ -277,12 +277,12 @@ public void testBulkProcessorConcurrentRequestsReadOnlyIndex() throws Exception
// let's make sure we get at least 1 item in the MultiGetRequest regardless of the randomising roulette
if (randomBoolean() || multiGetRequest.getItems().size() == 0) {
testDocs++;
processor.add(new IndexRequest("test").id(Integer.toString(testDocs)).source(XContentType.JSON, "field", "value"));
processor.add(new IndexRequest("test").id(Integer.toString(testDocs)).source(MediaTypeRegistry.JSON, "field", "value"));
multiGetRequest.add("test", Integer.toString(testDocs));
} else {
testReadOnlyDocs++;
processor.add(
new IndexRequest("test-ro").id(Integer.toString(testReadOnlyDocs)).source(XContentType.JSON, "field", "value")
new IndexRequest("test-ro").id(Integer.toString(testReadOnlyDocs)).source(MediaTypeRegistry.JSON, "field", "value")
);
}
}
Expand Down Expand Up @@ -333,9 +333,9 @@ public void testGlobalParametersAndSingleRequest() throws Exception {


processor.add(new IndexRequest() // <1>
.source(XContentType.JSON, "user", "some user"));
.source(MediaTypeRegistry.JSON, "user", "some user"));
processor.add(new IndexRequest("blogs").id("1") // <2>
.source(XContentType.JSON, "title", "some title"));
.source(MediaTypeRegistry.JSON, "title", "some title"));
}
// end::bulk-processor-mix-parameters
latch.await();
Expand Down Expand Up @@ -399,11 +399,11 @@ private MultiGetRequest indexDocs(BulkProcessor processor, int numDocs, String l
if (randomBoolean()) {
processor.add(
new IndexRequest(localIndex).id(Integer.toString(i))
.source(XContentType.JSON, "field", randomRealisticUnicodeOfLengthBetween(1, 30))
.source(MediaTypeRegistry.JSON, "field", randomRealisticUnicodeOfLengthBetween(1, 30))
);
} else {
BytesArray data = bytesBulkRequest(localIndex, i);
processor.add(data, globalIndex, globalPipeline, XContentType.JSON);
processor.add(data, globalIndex, globalPipeline, MediaTypeRegistry.JSON);
}
multiGetRequest.add(localIndex, Integer.toString(i));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
import org.opensearch.action.get.MultiGetRequest;
import org.opensearch.action.index.IndexRequest;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.transport.RemoteTransportException;

import java.util.Collections;
Expand Down Expand Up @@ -170,7 +170,7 @@ private static MultiGetRequest indexDocs(BulkProcessor processor, int numDocs) {
for (int i = 1; i <= numDocs; i++) {
processor.add(
new IndexRequest(INDEX_NAME).id(Integer.toString(i))
.source(XContentType.JSON, "field", randomRealisticUnicodeOfCodepointLengthBetween(1, 30))
.source(MediaTypeRegistry.JSON, "field", randomRealisticUnicodeOfCodepointLengthBetween(1, 30))
);
multiGetRequest.add(INDEX_NAME, Integer.toString(i));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.opensearch.action.bulk.BulkResponse;
import org.opensearch.action.index.IndexRequest;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.search.SearchHit;

import java.io.IOException;
Expand All @@ -59,8 +59,8 @@ public void testGlobalPipelineOnBulkRequest() throws IOException {
createFieldAddingPipleine("xyz", "fieldNameXYZ", "valueXYZ");

BulkRequest request = new BulkRequest();
request.add(new IndexRequest("test").id("1").source(XContentType.JSON, "field", "bulk1"));
request.add(new IndexRequest("test").id("2").source(XContentType.JSON, "field", "bulk2"));
request.add(new IndexRequest("test").id("1").source(MediaTypeRegistry.JSON, "field", "bulk1"));
request.add(new IndexRequest("test").id("2").source(MediaTypeRegistry.JSON, "field", "bulk2"));
request.pipeline("xyz");

bulk(request);
Expand All @@ -76,8 +76,8 @@ public void testPipelineOnRequestOverridesGlobalPipeline() throws IOException {

BulkRequest request = new BulkRequest();
request.pipeline("globalId");
request.add(new IndexRequest("test").id("1").source(XContentType.JSON, "field", "bulk1").setPipeline("perIndexId"));
request.add(new IndexRequest("test").id("2").source(XContentType.JSON, "field", "bulk2").setPipeline("perIndexId"));
request.add(new IndexRequest("test").id("1").source(MediaTypeRegistry.JSON, "field", "bulk1").setPipeline("perIndexId"));
request.add(new IndexRequest("test").id("2").source(MediaTypeRegistry.JSON, "field", "bulk2").setPipeline("perIndexId"));

bulk(request);

Expand All @@ -96,11 +96,11 @@ public void testMixPipelineOnRequestAndGlobal() throws IOException {
request.pipeline("globalId");

request.add(new IndexRequest("test").id("1")
.source(XContentType.JSON, "field", "bulk1")
.source(MediaTypeRegistry.JSON, "field", "bulk1")
.setPipeline("perIndexId")); // <1>

request.add(new IndexRequest("test").id("2")
.source(XContentType.JSON, "field", "bulk2")); // <2>
.source(MediaTypeRegistry.JSON, "field", "bulk2")); // <2>
// end::bulk-request-mix-pipeline
bulk(request);

Expand All @@ -116,8 +116,8 @@ public void testMixPipelineOnRequestAndGlobal() throws IOException {

public void testGlobalIndex() throws IOException {
BulkRequest request = new BulkRequest("global_index");
request.add(new IndexRequest().id("1").source(XContentType.JSON, "field", "bulk1"));
request.add(new IndexRequest().id("2").source(XContentType.JSON, "field", "bulk2"));
request.add(new IndexRequest().id("1").source(MediaTypeRegistry.JSON, "field", "bulk1"));
request.add(new IndexRequest().id("2").source(MediaTypeRegistry.JSON, "field", "bulk2"));

bulk(request);

Expand All @@ -128,10 +128,10 @@ public void testGlobalIndex() throws IOException {
@SuppressWarnings("unchecked")
public void testIndexGlobalAndPerRequest() throws IOException {
BulkRequest request = new BulkRequest("global_index");
request.add(new IndexRequest("local_index").id("1").source(XContentType.JSON, "field", "bulk1"));
request.add(new IndexRequest("local_index").id("1").source(MediaTypeRegistry.JSON, "field", "bulk1"));
request.add(
new IndexRequest().id("2") // will take global index
.source(XContentType.JSON, "field", "bulk2")
.source(MediaTypeRegistry.JSON, "field", "bulk2")
);

bulk(request);
Expand All @@ -143,8 +143,8 @@ public void testIndexGlobalAndPerRequest() throws IOException {
public void testGlobalRouting() throws IOException {
createIndexWithMultipleShards("index");
BulkRequest request = new BulkRequest((String) null);
request.add(new IndexRequest("index").id("1").source(XContentType.JSON, "field", "bulk1"));
request.add(new IndexRequest("index").id("2").source(XContentType.JSON, "field", "bulk1"));
request.add(new IndexRequest("index").id("1").source(MediaTypeRegistry.JSON, "field", "bulk1"));
request.add(new IndexRequest("index").id("2").source(MediaTypeRegistry.JSON, "field", "bulk1"));
request.routing("1");
bulk(request);

Expand All @@ -158,8 +158,8 @@ public void testGlobalRouting() throws IOException {
public void testMixLocalAndGlobalRouting() throws IOException {
BulkRequest request = new BulkRequest((String) null);
request.routing("globalRouting");
request.add(new IndexRequest("index").id("1").source(XContentType.JSON, "field", "bulk1"));
request.add(new IndexRequest("index").id("2").routing("localRouting").source(XContentType.JSON, "field", "bulk1"));
request.add(new IndexRequest("index").id("1").source(MediaTypeRegistry.JSON, "field", "bulk1"));
request.add(new IndexRequest("index").id("2").routing("localRouting").source(MediaTypeRegistry.JSON, "field", "bulk1"));

bulk(request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.core.common.unit.ByteSizeUnit;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.common.xcontent.support.XContentMapValues;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.indices.recovery.RecoverySettings;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.transport.RemoteClusterService;
Expand Down Expand Up @@ -125,7 +125,7 @@ public void testClusterPutSettings() throws IOException {

ClusterUpdateSettingsRequest resetRequest = new ClusterUpdateSettingsRequest();
resetRequest.transientSettings(Settings.builder().putNull(transientSettingKey));
resetRequest.persistentSettings("{\"" + persistentSettingKey + "\": null }", XContentType.JSON);
resetRequest.persistentSettings("{\"" + persistentSettingKey + "\": null }", MediaTypeRegistry.JSON);

ClusterUpdateSettingsResponse resetResponse = execute(
resetRequest,
Expand Down
Loading

0 comments on commit 7278f43

Please sign in to comment.