Skip to content

Commit

Permalink
Fixed root object mapper to be strict
Browse files Browse the repository at this point in the history
  • Loading branch information
spinscale committed May 30, 2014
1 parent 09a864b commit 4c70305
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/main/java/org/elasticsearch/common/joda/Joda.java
Expand Up @@ -237,6 +237,38 @@ public static FormatDateTimeFormatter forPattern(String input, Locale locale) {
return new FormatDateTimeFormatter(input, formatter.withZone(DateTimeZone.UTC), locale);
}

public static FormatDateTimeFormatter getStrictStandardDateFormater() {
// 2014/10/10
DateTimeFormatter shortFormatter = new DateTimeFormatterBuilder()
.appendFixedDecimal(DateTimeFieldType.year(), 4)
.appendLiteral('/')
.appendFixedDecimal(DateTimeFieldType.monthOfYear(), 2)
.appendLiteral('/')
.appendFixedDecimal(DateTimeFieldType.dayOfMonth(), 2)
.toFormatter()
.withZoneUTC();

// 2014/10/10 12:12:12
DateTimeFormatter longFormatter = new DateTimeFormatterBuilder()
.appendFixedDecimal(DateTimeFieldType.year(), 4)
.appendLiteral('/')
.appendFixedDecimal(DateTimeFieldType.monthOfYear(), 2)
.appendLiteral('/')
.appendFixedDecimal(DateTimeFieldType.dayOfMonth(), 2)
.appendLiteral(' ')
.appendFixedSignedDecimal(DateTimeFieldType.hourOfDay(), 2)
.appendLiteral(':')
.appendFixedSignedDecimal(DateTimeFieldType.minuteOfHour(), 2)
.appendLiteral(':')
.appendFixedSignedDecimal(DateTimeFieldType.secondOfMinute(), 2)
.toFormatter()
.withZoneUTC();

DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder().append(longFormatter.withZone(DateTimeZone.UTC).getPrinter(), new DateTimeParser[] {longFormatter.getParser(), shortFormatter.getParser()});

return new FormatDateTimeFormatter("yyyy/MM/dd HH:mm:ss||yyyy/MM/dd", builder.toFormatter().withZone(DateTimeZone.UTC), Locale.ROOT);
}


public static final DurationFieldType Quarters = new DurationFieldType("quarters") {
private static final long serialVersionUID = -8167713675442491871L;
Expand Down
Expand Up @@ -28,6 +28,7 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.mapper.*;
import org.elasticsearch.index.mapper.core.DateFieldMapper;
import org.joda.time.format.StrictISODateTimeFormat;

import java.io.IOException;
import java.util.*;
Expand All @@ -45,7 +46,7 @@ public static class Defaults {
public static final FormatDateTimeFormatter[] DYNAMIC_DATE_TIME_FORMATTERS =
new FormatDateTimeFormatter[]{
DateFieldMapper.Defaults.DATE_TIME_FORMATTER,
Joda.forPattern("yyyy/MM/dd HH:mm:ss||yyyy/MM/dd")
Joda.getStrictStandardDateFormater()
};
public static final boolean DATE_DETECTION = true;
public static final boolean NUMERIC_DETECTION = false;
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/org/elasticsearch/deps/joda/SimpleJodaTests.java
Expand Up @@ -22,6 +22,8 @@
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
import org.elasticsearch.common.joda.Joda;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.mapper.core.DateFieldMapper;
import org.elasticsearch.index.mapper.object.RootObjectMapper;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.joda.time.DateTimeFieldType;
import org.joda.time.DateTimeZone;
Expand Down Expand Up @@ -553,6 +555,41 @@ public void testThatDefaultFormatterChecksForCorrectYearLength() throws Exceptio
assertDateFormatParsingThrowingException("strictYearMonthDay", "2014-05-5");
}

@Test
public void testThatRootObjectParsingIsStrict() throws Exception {
String[] datesThatWork = new String[] { "2014/10/10", "2014/10/10 12:12:12", "2014-05-05", "2014-05-05T12:12:12.123Z" };
String[] datesThatShouldNotWork = new String[]{ "5-05-05", "2014-5-05", "2014-05-5",
"2014-05-05T1:12:12.123Z", "2014-05-05T12:1:12.123Z", "2014-05-05T12:12:1.123Z",
"4/10/10", "2014/1/10", "2014/10/1",
"2014/10/10 1:12:12", "2014/10/10 12:1:12", "2014/10/10 12:12:1"
};

// good case
for (String date : datesThatWork) {
boolean dateParsingSuccessful = false;
for (FormatDateTimeFormatter dateTimeFormatter : RootObjectMapper.Defaults.DYNAMIC_DATE_TIME_FORMATTERS) {
try {
dateTimeFormatter.parser().parseMillis(date);
dateParsingSuccessful = true;
break;
} catch (Exception e) {}
}
if (!dateParsingSuccessful) {
fail("Parsing for date " + date + " in root object mapper failed, but shouldnt");
}
}

// bad case
for (String date : datesThatShouldNotWork) {
for (FormatDateTimeFormatter dateTimeFormatter : RootObjectMapper.Defaults.DYNAMIC_DATE_TIME_FORMATTERS) {
try {
dateTimeFormatter.parser().parseMillis(date);
fail(String.format(Locale.ROOT, "Expected exception when parsing date %s in root mapper", date));
} catch (Exception e) {}
}
}
}

private void assertValidDateFormatParsing(String pattern, String dateToParse) {
assertValidDateFormatParsing(pattern, dateToParse, dateToParse);
}
Expand Down

0 comments on commit 4c70305

Please sign in to comment.