Skip to content

Commit

Permalink
remove unnecessary XContentType casting
Browse files Browse the repository at this point in the history
Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
  • Loading branch information
nknize committed Feb 10, 2023
1 parent 3288c29 commit b605a19
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ public class IngestClientIT extends OpenSearchRestHighLevelClientTestCase {
public void testPutPipeline() throws IOException {
String id = "some_pipeline_id";
XContentBuilder pipelineBuilder = buildRandomXContentPipeline();
PutPipelineRequest request = new PutPipelineRequest(
id,
BytesReference.bytes(pipelineBuilder),
(XContentType) pipelineBuilder.contentType()
);
PutPipelineRequest request = new PutPipelineRequest(id, BytesReference.bytes(pipelineBuilder), pipelineBuilder.contentType());

AcknowledgedResponse putPipelineResponse = execute(
request,
Expand All @@ -76,11 +72,7 @@ public void testGetPipeline() throws IOException {
String id = "some_pipeline_id";
XContentBuilder pipelineBuilder = buildRandomXContentPipeline();
{
PutPipelineRequest request = new PutPipelineRequest(
id,
BytesReference.bytes(pipelineBuilder),
(XContentType) pipelineBuilder.contentType()
);
PutPipelineRequest request = new PutPipelineRequest(id, BytesReference.bytes(pipelineBuilder), pipelineBuilder.contentType());
createPipeline(request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ protected static void createFieldAddingPipleine(String id, String fieldName, Str

protected static void createPipeline(String pipelineId) throws IOException {
XContentBuilder builder = buildRandomXContentPipeline();
createPipeline(new PutPipelineRequest(pipelineId, BytesReference.bytes(builder), (XContentType) builder.contentType()));
createPipeline(new PutPipelineRequest(pipelineId, BytesReference.bytes(builder), builder.contentType()));
}

protected static void createPipeline(PutPipelineRequest putPipelineRequest) throws IOException {
Expand Down Expand Up @@ -267,7 +267,7 @@ protected void putConflictPipeline() throws IOException {
final PutPipelineRequest putPipelineRequest = new PutPipelineRequest(
CONFLICT_PIPELINE_ID,
BytesReference.bytes(pipelineBuilder),
(XContentType) pipelineBuilder.contentType()
pipelineBuilder.contentType()
);
assertTrue(highLevelClient().ingest().putPipeline(putPipelineRequest, RequestOptions.DEFAULT).isAcknowledged());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import org.opensearch.common.Strings;
import org.opensearch.common.io.stream.StreamInput;
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.indices.replication.SegmentReplicationState;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import org.opensearch.common.io.stream.StreamOutput;
import org.opensearch.common.io.stream.Writeable;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.xcontent.ToXContent;
import org.opensearch.common.xcontent.ToXContentFragment;
import org.opensearch.common.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.ToXContentFragment;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.indices.replication.common.ReplicationLuceneIndex;
import org.opensearch.indices.replication.common.ReplicationState;
import org.opensearch.indices.replication.common.ReplicationTimer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,7 @@ public void testFilterWithEmptyIncludesExcludes() {
public void testThatFilterIncludesEmptyObjectWhenUsingIncludes() throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("obj").endObject().endObject();

Tuple<XContentType, Map<String, Object>> mapTuple = convertToMap(
BytesReference.bytes(builder),
true,
(XContentType) builder.contentType()
);
Tuple<? extends MediaType, Map<String, Object>> mapTuple = convertToMap(BytesReference.bytes(builder), true, builder.contentType());
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), new String[] { "obj" }, Strings.EMPTY_ARRAY);

assertThat(mapTuple.v2(), equalTo(filteredSource));
Expand All @@ -442,11 +438,7 @@ public void testThatFilterIncludesEmptyObjectWhenUsingIncludes() throws Exceptio
public void testThatFilterIncludesEmptyObjectWhenUsingExcludes() throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("obj").endObject().endObject();

Tuple<XContentType, Map<String, Object>> mapTuple = convertToMap(
BytesReference.bytes(builder),
true,
(XContentType) builder.contentType()
);
Tuple<? extends MediaType, Map<String, Object>> mapTuple = convertToMap(BytesReference.bytes(builder), true, builder.contentType());
Map<String, Object> filteredSource = XContentMapValues.filter(
mapTuple.v2(),
Strings.EMPTY_ARRAY,
Expand All @@ -460,11 +452,7 @@ public void testThatFilterIncludesEmptyObjectWhenUsingExcludes() throws Exceptio
public void testNotOmittingObjectsWithExcludedProperties() throws Exception {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("obj").field("f1", "v1").endObject().endObject();

Tuple<XContentType, Map<String, Object>> mapTuple = convertToMap(
BytesReference.bytes(builder),
true,
(XContentType) builder.contentType()
);
Tuple<? extends MediaType, Map<String, Object>> mapTuple = convertToMap(BytesReference.bytes(builder), true, builder.contentType());
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), Strings.EMPTY_ARRAY, new String[] { "obj.f1" });

assertThat(filteredSource.size(), equalTo(1));
Expand All @@ -485,11 +473,7 @@ public void testNotOmittingObjectWithNestedExcludedObject() throws Exception {
.endObject();

// implicit include
Tuple<XContentType, Map<String, Object>> mapTuple = convertToMap(
BytesReference.bytes(builder),
true,
(XContentType) builder.contentType()
);
Tuple<? extends MediaType, Map<String, Object>> mapTuple = convertToMap(BytesReference.bytes(builder), true, builder.contentType());
Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), Strings.EMPTY_ARRAY, new String[] { "*.obj2" });

assertThat(filteredSource.size(), equalTo(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public void testMultiFieldsInConsistentOrder() throws Exception {
Map<String, Object> sourceAsMap = XContentHelper.convertToMap(
docMapper.mappingSource().compressedReference(),
true,
(XContentType) builder.contentType()
builder.contentType()
).v2();
@SuppressWarnings("unchecked")
Map<String, Object> multiFields = (Map<String, Object>) XContentMapValues.extractValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotRequest;
import org.opensearch.action.support.IndicesOptions;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.test.OpenSearchTestCase;
Expand Down Expand Up @@ -97,7 +96,7 @@ public void testRestoreSnapshotRequestParsing() throws IOException {

BytesReference bytes = BytesReference.bytes(builder.endObject());

request.source(XContentHelper.convertToMap(bytes, true, (XContentType) builder.contentType()).v2());
request.source(XContentHelper.convertToMap(bytes, true, builder.contentType()).v2());

assertEquals("test-repo", request.repository());
assertEquals("test-snap", request.snapshot());
Expand Down Expand Up @@ -163,7 +162,7 @@ public void testCreateSnapshotRequestParsing() throws IOException {

BytesReference bytes = BytesReference.bytes(builder.endObject());

request.source(XContentHelper.convertToMap(bytes, true, (XContentType) builder.contentType()).v2());
request.source(XContentHelper.convertToMap(bytes, true, builder.contentType()).v2());

assertEquals("test-repo", request.repository());
assertEquals("test-snap", request.snapshot());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import org.opensearch.common.SetOnce;
import org.opensearch.common.Strings;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentHelper;
Expand Down Expand Up @@ -235,29 +234,26 @@ public void testMeta() throws IOException {
b.field("meta", Collections.singletonMap("foo", "bar"));
});
MapperService mapperService = createMapperService(mapping);
XContentType xContentType = (XContentType) mapping.contentType();
assertEquals(
XContentHelper.convertToMap(BytesReference.bytes(mapping), false, xContentType).v2(),
XContentHelper.convertToMap(mapperService.documentMapper().mappingSource().uncompressed(), false, xContentType).v2()
XContentHelper.convertToMap(BytesReference.bytes(mapping), false, mapping.contentType()).v2(),
XContentHelper.convertToMap(mapperService.documentMapper().mappingSource().uncompressed(), false, mapping.contentType()).v2()
);

mapping = fieldMapping(this::metaMapping);
xContentType = (XContentType) mapping.contentType();
merge(mapperService, mapping);
assertEquals(
XContentHelper.convertToMap(BytesReference.bytes(mapping), false, xContentType).v2(),
XContentHelper.convertToMap(mapperService.documentMapper().mappingSource().uncompressed(), false, xContentType).v2()
XContentHelper.convertToMap(BytesReference.bytes(mapping), false, mapping.contentType()).v2(),
XContentHelper.convertToMap(mapperService.documentMapper().mappingSource().uncompressed(), false, mapping.contentType()).v2()
);

mapping = fieldMapping(b -> {
metaMapping(b);
b.field("meta", Collections.singletonMap("baz", "quux"));
});
xContentType = (XContentType) mapping.contentType();
merge(mapperService, mapping);
assertEquals(
XContentHelper.convertToMap(BytesReference.bytes(mapping), false, xContentType).v2(),
XContentHelper.convertToMap(mapperService.documentMapper().mappingSource().uncompressed(), false, xContentType).v2()
XContentHelper.convertToMap(BytesReference.bytes(mapping), false, mapping.contentType()).v2(),
XContentHelper.convertToMap(mapperService.documentMapper().mappingSource().uncompressed(), false, mapping.contentType()).v2()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public static Map<String, Object> convertToMap(ToXContent part) throws IOExcepti
builder.startObject();
part.toXContent(builder, EMPTY_PARAMS);
builder.endObject();
XContentType xContentType = (XContentType) builder.contentType();
return XContentHelper.convertToMap(BytesReference.bytes(builder), false, xContentType).v2();
return XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2();
}

public static BytesReference convertToXContent(Map<String, ?> map, XContentType xContentType) throws IOException {
Expand Down

0 comments on commit b605a19

Please sign in to comment.