Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support only string format for date, root object & date range #28117

Merged
merged 5 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,10 @@ private static IndexOptions nodeIndexOptionValue(final Object propNode) {
}

public static FormatDateTimeFormatter parseDateTimeFormatter(Object node) {
return Joda.forPattern(node.toString());
if (node instanceof String) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of instance testing here I would suggest changing the method to only take a String argument (the format) and change all call sites to convert to String instead. I should be save to do so since we to it here anyway currently.
That way we also don't need to throw an exception for bad formats, Jodas "forPattern" will do so already.

return Joda.forPattern((String) node);
}
throw new IllegalArgumentException("Invalid format: [" + node.toString() + "]: expected string value");
}

public static void parseTermVector(String fieldName, String termVector, FieldMapper.Builder builder) throws MapperParsingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,22 @@ public void testMergeText() throws Exception {
() -> mapper.merge(update.mapping()));
assertEquals("mapper [date] of different type, current_type [date], merged_type [text]", e.getMessage());
}

public void testIllegalFormatField() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder()
.startObject()
.startObject("type")
.startObject("properties")
.startObject("field")
.field("type", "date")
.array("format", "test_format")
.endObject()
.endObject()
.endObject()
.endObject());

IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> parser.parse("type", new CompressedXContent(mapping)));
assertEquals("Invalid format: [[test_format]]: expected string value", e.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -443,4 +443,22 @@ public void testSerializeDefaults() throws Exception {
}
}

public void testIllegalFormatField() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder()
.startObject()
.startObject("type")
.startObject("properties")
.startObject("field")
.field("type", "date_range")
.array("format", "test_format")
.endObject()
.endObject()
.endObject()
.endObject());

IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> parser.parse("type", new CompressedXContent(mapping)));
assertEquals("Invalid format: [[test_format]]: expected string value", e.getMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,30 @@ public void testDynamicTemplates() throws Exception {
mapper = mapperService.merge("type", new CompressedXContent(mapping3), MergeReason.MAPPING_UPDATE);
assertEquals(mapping3, mapper.mappingSource().toString());
}

public void testIllegalFormatField() throws Exception {
String dynamicMapping = Strings.toString(XContentFactory.jsonBuilder()
.startObject()
.startObject("type")
.startArray("dynamic_date_formats")
.startArray().value("test_format").endArray()
.endArray()
.endObject()
.endObject());
String mapping = Strings.toString(XContentFactory.jsonBuilder()
.startObject()
.startObject("type")
.startArray("date_formats")
.startArray().value("test_format").endArray()
.endArray()
.endObject()
.endObject());

DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
for (String m : Arrays.asList(mapping, dynamicMapping)) {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> parser.parse("type", new CompressedXContent(m)));
assertEquals("Invalid format: [[test_format]]: expected string value", e.getMessage());
}
}
}