Skip to content

Commit 49374db

Browse files
committed
Mapper: Throw exception if null_value is set to null
The mapping parser should throw an exception if "null_value" is set to `null`. Fixes #7273 ```bash PUT /foo { "mappings": { "bar": { "properties": { "exception": { "null_value": null, "type": "integer" } } } } } ``` ``` { "error": "MapperParsingException[mapping [bar]]; nested: MapperParsingException[Property [null_value] cannot be null.]; ", "status": 400 } ```
1 parent 617815d commit 49374db

File tree

11 files changed

+95
-0
lines changed

11 files changed

+95
-0
lines changed

src/main/java/org/elasticsearch/index/mapper/core/BooleanFieldMapper.java

+3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext
111111
String propName = Strings.toUnderscoreCase(entry.getKey());
112112
Object propNode = entry.getValue();
113113
if (propName.equals("null_value")) {
114+
if (propNode == null) {
115+
throw new MapperParsingException("Property [null_value] cannot be null.");
116+
}
114117
builder.nullValue(nodeBooleanValue(propNode));
115118
}
116119
}

src/main/java/org/elasticsearch/index/mapper/core/ByteFieldMapper.java

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext
107107
String propName = Strings.toUnderscoreCase(entry.getKey());
108108
Object propNode = entry.getValue();
109109
if (propName.equals("null_value")) {
110+
if (propNode == null) {
111+
throw new MapperParsingException("Property [null_value] cannot be null.");
112+
}
110113
builder.nullValue(nodeByteValue(propNode));
111114
}
112115
}

src/main/java/org/elasticsearch/index/mapper/core/DateFieldMapper.java

+3
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ public static class TypeParser implements Mapper.TypeParser {
150150
String propName = Strings.toUnderscoreCase(entry.getKey());
151151
Object propNode = entry.getValue();
152152
if (propName.equals("null_value")) {
153+
if (propNode == null) {
154+
throw new MapperParsingException("Property [null_value] cannot be null.");
155+
}
153156
builder.nullValue(propNode.toString());
154157
} else if (propName.equals("format")) {
155158
builder.dateTimeFormatter(parseDateTimeFormatter(propName, propNode));

src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.java

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext
110110
String propName = entry.getKey();
111111
Object propNode = entry.getValue();
112112
if (propName.equals("nullValue") || propName.equals("null_value")) {
113+
if (propNode == null) {
114+
throw new MapperParsingException("Property [null_value] cannot be null.");
115+
}
113116
builder.nullValue(nodeDoubleValue(propNode));
114117
}
115118
}

src/main/java/org/elasticsearch/index/mapper/core/FloatFieldMapper.java

+3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext
111111
String propName = Strings.toUnderscoreCase(entry.getKey());
112112
Object propNode = entry.getValue();
113113
if (propName.equals("null_value")) {
114+
if (propNode == null) {
115+
throw new MapperParsingException("Property [null_value] cannot be null.");
116+
}
114117
builder.nullValue(nodeFloatValue(propNode));
115118
}
116119
}

src/main/java/org/elasticsearch/index/mapper/core/IntegerFieldMapper.java

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext
107107
String propName = Strings.toUnderscoreCase(entry.getKey());
108108
Object propNode = entry.getValue();
109109
if (propName.equals("null_value")) {
110+
if (propNode == null) {
111+
throw new MapperParsingException("Property [null_value] cannot be null.");
112+
}
110113
builder.nullValue(nodeIntegerValue(propNode));
111114
}
112115
}

src/main/java/org/elasticsearch/index/mapper/core/LongFieldMapper.java

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext
107107
String propName = Strings.toUnderscoreCase(entry.getKey());
108108
Object propNode = entry.getValue();
109109
if (propName.equals("null_value")) {
110+
if (propNode == null) {
111+
throw new MapperParsingException("Property [null_value] cannot be null.");
112+
}
110113
builder.nullValue(nodeLongValue(propNode));
111114
}
112115
}

src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.java

+3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext
109109
String propName = Strings.toUnderscoreCase(entry.getKey());
110110
Object propNode = entry.getValue();
111111
if (propName.equals("null_value")) {
112+
if (propNode == null) {
113+
throw new MapperParsingException("Property [null_value] cannot be null.");
114+
}
112115
builder.nullValue(nodeShortValue(propNode));
113116
}
114117
}

src/main/java/org/elasticsearch/index/mapper/core/StringFieldMapper.java

+3
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext
155155
String propName = Strings.toUnderscoreCase(entry.getKey());
156156
Object propNode = entry.getValue();
157157
if (propName.equals("null_value")) {
158+
if (propNode == null) {
159+
throw new MapperParsingException("Property [null_value] cannot be null.");
160+
}
158161
builder.nullValue(propNode.toString());
159162
} else if (propName.equals("search_quote_analyzer")) {
160163
NamedAnalyzer analyzer = parserContext.analysisService().analyzer(propNode.toString());

src/main/java/org/elasticsearch/index/mapper/ip/IpFieldMapper.java

+3
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext
138138
String propName = Strings.toUnderscoreCase(entry.getKey());
139139
Object propNode = entry.getValue();
140140
if (propName.equals("null_value")) {
141+
if (propNode == null) {
142+
throw new MapperParsingException("Property [null_value] cannot be null.");
143+
}
141144
builder.nullValue(propNode.toString());
142145
}
143146
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.elasticsearch.index.mapper.null_value;
2+
3+
/*
4+
* Licensed to Elasticsearch under one or more contributor
5+
* license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright
7+
* ownership. Elasticsearch licenses this file to you under
8+
* the Apache License, Version 2.0 (the "License"); you may
9+
* not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
23+
import org.elasticsearch.common.settings.ImmutableSettings;
24+
import org.elasticsearch.common.xcontent.XContentFactory;
25+
import org.elasticsearch.index.mapper.MapperParsingException;
26+
import org.elasticsearch.index.service.IndexService;
27+
import org.elasticsearch.test.ElasticsearchSingleNodeTest;
28+
import org.junit.Test;
29+
30+
import static org.hamcrest.Matchers.*;
31+
32+
/**
33+
*/
34+
public class NullValueTests extends ElasticsearchSingleNodeTest {
35+
36+
@Test
37+
public void testNullNull_Value() throws Exception {
38+
IndexService indexService = createIndex("test", ImmutableSettings.settingsBuilder().build());
39+
String[] typesToTest = {"integer", "long", "double", "float", "short", "date", "ip", "string", "boolean", "byte"};
40+
41+
for (String type : typesToTest) {
42+
String mapping = XContentFactory.jsonBuilder()
43+
.startObject()
44+
.startObject("type")
45+
.startObject("properties")
46+
.startObject("numeric")
47+
.field("type", type)
48+
.field("null_value", (String) null)
49+
.endObject()
50+
.endObject()
51+
.endObject()
52+
.endObject().string();
53+
54+
try {
55+
indexService.mapperService().documentMapperParser().parse(mapping);
56+
fail("Test should have failed because [null_value] was null.");
57+
} catch (MapperParsingException e) {
58+
assertThat(e.getMessage(), equalTo("Property [null_value] cannot be null."));
59+
}
60+
61+
}
62+
63+
64+
}
65+
}

0 commit comments

Comments
 (0)