Skip to content

Commit fcea4c5

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 0ccbe1c commit fcea4c5

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
@@ -108,6 +108,9 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext
108108
String propName = Strings.toUnderscoreCase(entry.getKey());
109109
Object propNode = entry.getValue();
110110
if (propName.equals("null_value")) {
111+
if (propNode == null) {
112+
throw new MapperParsingException("Property [null_value] cannot be null.");
113+
}
111114
builder.nullValue(nodeByteValue(propNode));
112115
}
113116
}

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

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

src/main/java/org/elasticsearch/index/mapper/core/DoubleFieldMapper.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 = entry.getKey();
112112
Object propNode = entry.getValue();
113113
if (propName.equals("nullValue") || propName.equals("null_value")) {
114+
if (propNode == null) {
115+
throw new MapperParsingException("Property [null_value] cannot be null.");
116+
}
114117
builder.nullValue(nodeDoubleValue(propNode));
115118
}
116119
}

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

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

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

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

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

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

src/main/java/org/elasticsearch/index/mapper/core/ShortFieldMapper.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 = Strings.toUnderscoreCase(entry.getKey());
111111
Object propNode = entry.getValue();
112112
if (propName.equals("null_value")) {
113+
if (propNode == null) {
114+
throw new MapperParsingException("Property [null_value] cannot be null.");
115+
}
113116
builder.nullValue(nodeShortValue(propNode));
114117
}
115118
}

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
@@ -143,6 +143,9 @@ public Mapper.Builder parse(String name, Map<String, Object> node, ParserContext
143143
String propName = Strings.toUnderscoreCase(entry.getKey());
144144
Object propNode = entry.getValue();
145145
if (propName.equals("null_value")) {
146+
if (propNode == null) {
147+
throw new MapperParsingException("Property [null_value] cannot be null.");
148+
}
146149
builder.nullValue(propNode.toString());
147150
}
148151
}
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)