Skip to content

Commit 37c433a

Browse files
authored
Merge pull request #19837
Ensure PutMappingRequest.buildFromSimplifiedDef input are pairs
2 parents 10d64eb + e57f76a commit 37c433a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

core/src/main/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingRequest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,17 @@ public PutMappingRequest source(Object... source) {
177177
return source(buildFromSimplifiedDef(type, source));
178178
}
179179

180+
/**
181+
* @param type the mapping type
182+
* @param source consisting of field/properties pairs (e.g. "field1",
183+
* "type=string,store=true"). If the number of arguments is not
184+
* divisible by two an {@link IllegalArgumentException} is thrown
185+
* @return the mappings definition
186+
*/
180187
public static XContentBuilder buildFromSimplifiedDef(String type, Object... source) {
188+
if (source.length % 2 != 0) {
189+
throw new IllegalArgumentException("mapping source must be pairs of fieldnames and properties definition.");
190+
}
181191
try {
182192
XContentBuilder builder = XContentFactory.jsonBuilder();
183193
builder.startObject();

core/src/test/java/org/elasticsearch/action/admin/indices/mapping/put/PutMappingRequestTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,11 @@ public void testValidation() {
5757
"Validation Failed: 1: either concrete index or unresolved indices can be set," +
5858
" concrete index: [[foo/bar]] and indices: [myindex];");
5959
}
60+
61+
public void testBuildFromSimplifiedDef() {
62+
// test that method rejects input where input varargs fieldname/properites are not paired correctly
63+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
64+
() -> PutMappingRequest.buildFromSimplifiedDef("type", "only_field"));
65+
assertEquals("mapping source must be pairs of fieldnames and properties definition.", e.getMessage());
66+
}
6067
}

0 commit comments

Comments
 (0)