Skip to content

Commit

Permalink
Make sure to reject mappings with type _doc when include_type_name is…
Browse files Browse the repository at this point in the history
… false. (#38270)

`CreateIndexRequest#source(Map<String, Object>, ... )`, which is used when
deserializing index creation requests, accidentally accepts mappings that are
nested twice under the type key (as described in the bug report #38266).

This in turn causes us to be too lenient in parsing typeless mappings. In
particular, we accept the following index creation request, even though it
should not contain the type key `_doc`:

```
PUT index?include_type_name=false
{
  "mappings": {
    "_doc": {
      "properties": { ... }
    }
  }
}
```

There is a similar issue for both 'put templates' and 'put mappings' requests
as well.

This PR makes the minimal changes to detect and reject these typed mappings in
requests. It does not address #38266 generally, or attempt a larger refactor
around types in these server-side requests, as I think this should be done at a
later time.
  • Loading branch information
jtibshirani authored Feb 5, 2019
1 parent 8ebff05 commit 3ce7d2c
Show file tree
Hide file tree
Showing 29 changed files with 642 additions and 616 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1235,12 +1235,10 @@ public void testGet() throws Exception {
createIndex.setJsonEntity(
"{\n" +
" \"mappings\" : {\n" +
" \"_doc\" : {\n" +
" \"properties\" : {\n" +
" \"message\" : {\n" +
" \"type\": \"text\",\n" +
" \"store\": true\n" +
" }\n" +
" \"properties\" : {\n" +
" \"message\" : {\n" +
" \"type\": \"text\",\n" +
" \"store\": true\n" +
" }\n" +
" }\n" +
" }\n" +
Expand Down Expand Up @@ -1764,12 +1762,10 @@ public void testMultiGet() throws Exception {
createIndex.setJsonEntity(
"{\n" +
" \"mappings\" : {\n" +
" \"_doc\" : {\n" +
" \"properties\" : {\n" +
" \"foo\" : {\n" +
" \"type\": \"text\",\n" +
" \"store\": true\n" +
" }\n" +
" \"properties\" : {\n" +
" \"foo\" : {\n" +
" \"type\": \"text\",\n" +
" \"store\": true\n" +
" }\n" +
" }\n" +
" }\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,13 @@ public void testCreateIndex() throws IOException {
{
request = new CreateIndexRequest("twitter2");
//tag::create-index-mappings-map
Map<String, Object> jsonMap = new HashMap<>();
Map<String, Object> message = new HashMap<>();
message.put("type", "text");
Map<String, Object> properties = new HashMap<>();
properties.put("message", message);
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
jsonMap.put("_doc", mapping);
request.mapping(jsonMap); // <1>
request.mapping(mapping); // <1>
//end::create-index-mappings-map
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
assertTrue(createIndexResponse.isAcknowledged());
Expand All @@ -332,15 +330,11 @@ public void testCreateIndex() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("_doc");
builder.startObject("properties");
{
builder.startObject("properties");
builder.startObject("message");
{
builder.startObject("message");
{
builder.field("type", "text");
}
builder.endObject();
builder.field("type", "text");
}
builder.endObject();
}
Expand Down Expand Up @@ -381,10 +375,8 @@ public void testCreateIndex() throws IOException {
" \"number_of_replicas\" : 0\n" +
" },\n" +
" \"mappings\" : {\n" +
" \"_doc\" : {\n" +
" \"properties\" : {\n" +
" \"message\" : { \"type\" : \"text\" }\n" +
" }\n" +
" \"properties\" : {\n" +
" \"message\" : { \"type\" : \"text\" }\n" +
" }\n" +
" },\n" +
" \"aliases\" : {\n" +
Expand Down
Loading

0 comments on commit 3ce7d2c

Please sign in to comment.