Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

package org.opensearch.action.admin.indices.create;

import org.opensearch.OpenSearchParseException;
import org.opensearch.action.UnavailableShardsException;
import org.opensearch.action.admin.cluster.state.ClusterStateResponse;
import org.opensearch.action.admin.indices.alias.Alias;
Expand All @@ -56,6 +57,7 @@
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.index.IndexNotFoundException;
import org.opensearch.index.IndexService;
import org.opensearch.index.IndexSettings;
Expand Down Expand Up @@ -442,6 +444,24 @@ public void testCreateIndexWithNullReplicaCountPickUpClusterReplica() {
}
}

public void testCreateIndexWithMappingsAsString() {
CreateIndexRequest request = new CreateIndexRequest("test");
OpenSearchParseException e = expectThrows(
OpenSearchParseException.class,
() -> request.source("{\"mappings\": \"{}\"}", MediaTypeRegistry.JSON)
);
assertEquals("key [mappings] must be an object", e.getMessage());
}

public void testCreateIndexWithMappingsNestedValueAsString() {
CreateIndexRequest request = new CreateIndexRequest("test");
OpenSearchParseException e = expectThrows(
OpenSearchParseException.class,
() -> request.source("{\"mappings\": {\"properties\": \"invalid\"}}", MediaTypeRegistry.JSON)
);
assertEquals("values inside key [mappings] must be an object", e.getMessage());
}

public void testCreateIndexWithContextSettingsAndTemplate() throws Exception {
int numReplicas = 1;
String indexName = "test-idx-1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,14 @@ public CreateIndexRequest source(Map<String, ?> source, DeprecationHandler depre
}
settings((Map<String, Object>) entry.getValue());
} else if (MAPPINGS.match(name, deprecationHandler)) {
if (entry.getValue() instanceof Map == false) {
throw new OpenSearchParseException("key [mappings] must be an object");
}
Map<String, Object> mappings = (Map<String, Object>) entry.getValue();
for (Map.Entry<String, Object> entry1 : mappings.entrySet()) {
if (entry1.getValue() instanceof Map == false) {
throw new OpenSearchParseException("values inside key [mappings] must be an object");
}
mapping(entry1.getKey(), (Map<String, Object>) entry1.getValue());
}
} else if (ALIASES.match(name, deprecationHandler)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,24 @@ public void testContext() throws IOException {
}
}

public void testMappingsType() throws IOException {
XContentBuilder builder = MediaTypeRegistry.contentBuilder(randomFrom(XContentType.values()));
builder.startObject().startArray("mappings").endArray().endObject();

CreateIndexRequest parsedCreateIndexRequest = new CreateIndexRequest();
OpenSearchParseException e = expectThrows(OpenSearchParseException.class, () -> parsedCreateIndexRequest.source(builder));
assertThat(e.getMessage(), equalTo("key [mappings] must be an object"));
}

public void testMappingsNestedValueType() throws IOException {
XContentBuilder builder = MediaTypeRegistry.contentBuilder(randomFrom(XContentType.values()));
builder.startObject().startObject("mappings").field("properties", "invalid_string_value").endObject().endObject();

CreateIndexRequest parsedCreateIndexRequest = new CreateIndexRequest();
OpenSearchParseException e = expectThrows(OpenSearchParseException.class, () -> parsedCreateIndexRequest.source(builder));
assertThat(e.getMessage(), equalTo("values inside key [mappings] must be an object"));
}

public static void assertMappingsEqual(Map<String, String> expected, Map<String, String> actual) throws IOException {
assertEquals(expected.keySet(), actual.keySet());

Expand Down
Loading