Skip to content

Commit

Permalink
Ensure PutMappingRequest.buildFromSimplifiedDef fails when input isn'…
Browse files Browse the repository at this point in the history
…t pairs

The method requires pairs of fieldnames and property arguments and will fail if
the varargs input is an uneven number. We should check this and fail with an
appropriate IllegalArgumentException instead.
  • Loading branch information
cbuescher committed Aug 5, 2016
1 parent 899cdde commit e57f76a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,17 @@ public PutMappingRequest source(Object... source) {
return source(buildFromSimplifiedDef(type, source));
}

/**
* @param type the mapping type
* @param source consisting of field/properties pairs (e.g. "field1",
* "type=string,store=true"). If the number of arguments is not
* divisible by two an {@link IllegalArgumentException} is thrown
* @return the mappings definition
*/
public static XContentBuilder buildFromSimplifiedDef(String type, Object... source) {
if (source.length % 2 != 0) {
throw new IllegalArgumentException("mapping source must be pairs of fieldnames and properties definition.");
}
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,11 @@ public void testValidation() {
"Validation Failed: 1: either concrete index or unresolved indices can be set," +
" concrete index: [[foo/bar]] and indices: [myindex];");
}

public void testBuildFromSimplifiedDef() {
// test that method rejects input where input varargs fieldname/properites are not paired correctly
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> PutMappingRequest.buildFromSimplifiedDef("type", "only_field"));
assertEquals("mapping source must be pairs of fieldnames and properties definition.", e.getMessage());
}
}

0 comments on commit e57f76a

Please sign in to comment.