[Java.time] Retain prefixed date pattern in formatter (#48703)

JavaDateFormatter should keep the pattern with the prefixed 8 as it will be used for serialisation. The stripped pattern should be used for the enclosed formatters.

closes #48698
This commit is contained in:
Przemyslaw Gomulka 2019-11-27 12:29:18 +01:00 committed by GitHub
parent 0a73ba05de
commit 502873b144
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 17 deletions

View File

@ -82,11 +82,6 @@ enum DateFormat {
@Override @Override
Function<String, ZonedDateTime> getFunction(String format, ZoneId zoneId, Locale locale) { Function<String, ZonedDateTime> getFunction(String format, ZoneId zoneId, Locale locale) {
// support the 6.x BWC compatible way of parsing java 8 dates
if (format.startsWith("8")) {
format = format.substring(1);
}
boolean isUtc = ZoneOffset.UTC.equals(zoneId); boolean isUtc = ZoneOffset.UTC.equals(zoneId);
DateFormatter dateFormatter = DateFormatter.forPattern(format) DateFormatter dateFormatter = DateFormatter.forPattern(format)

View File

@ -1,8 +1,8 @@
--- ---
"Test Index and Search locale dependent mappings / dates": "Test Index and Search locale dependent mappings / dates":
- skip: - skip:
version: "all" version: " - 6.1.99"
reason: "Awaits fix: https://github.com/elastic/elasticsearch/issues/39981(Previously: JDK9 only supports this with a special sysproperty added in 6.2.0.)" reason: JDK9 only supports this with a special sysproperty added in 6.2.0
- do: - do:
indices.create: indices.create:
index: test_index index: test_index
@ -13,7 +13,7 @@
properties: properties:
date_field: date_field:
type: date type: date
format: "E, d MMM yyyy HH:mm:ss Z" format: "8E, d MMM uuuu HH:mm:ss Z"
locale: "de" locale: "de"
- do: - do:
bulk: bulk:

View File

@ -129,27 +129,28 @@ public interface DateFormatter {
DateMathParser toDateMathParser(); DateMathParser toDateMathParser();
static DateFormatter forPattern(String input) { static DateFormatter forPattern(String input) {
if (Strings.hasLength(input) == false) { if (Strings.hasLength(input) == false) {
throw new IllegalArgumentException("No date pattern provided"); throw new IllegalArgumentException("No date pattern provided");
} }
// support the 6.x BWC compatible way of parsing java 8 dates // support the 6.x BWC compatible way of parsing java 8 dates
if (input.startsWith("8")) { String format = strip8Prefix(input);
input = input.substring(1); List<String> patterns = splitCombinedPatterns(format);
}
List<String> patterns = splitCombinedPatterns(input);
List<DateFormatter> formatters = patterns.stream() List<DateFormatter> formatters = patterns.stream()
.map(DateFormatters::forPattern) .map(DateFormatters::forPattern)
.collect(Collectors.toList()); .collect(Collectors.toList());
if (formatters.size() == 1) {
return formatters.get(0);
}
return JavaDateFormatter.combined(input, formatters); return JavaDateFormatter.combined(input, formatters);
} }
static String strip8Prefix(String input) {
if (input.startsWith("8")) {
return input.substring(1);
}
return input;
}
static List<String> splitCombinedPatterns(String input) { static List<String> splitCombinedPatterns(String input) {
List<String> patterns = new ArrayList<>(); List<String> patterns = new ArrayList<>();
for (String pattern : Strings.delimitedListToStringArray(input, "||")) { for (String pattern : Strings.delimitedListToStringArray(input, "||")) {

View File

@ -339,4 +339,21 @@ public class DocumentMapper implements ToXContentFragment {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
return mapping.toXContent(builder, params); return mapping.toXContent(builder, params);
} }
@Override
public String toString() {
return "DocumentMapper{" +
"mapperService=" + mapperService +
", type='" + type + '\'' +
", typeText=" + typeText +
", mappingSource=" + mappingSource +
", mapping=" + mapping +
", documentParser=" + documentParser +
", fieldMappers=" + fieldMappers +
", objectMappers=" + objectMappers +
", hasNestedObjects=" + hasNestedObjects +
", deleteTombstoneMetadataFieldMappers=" + Arrays.toString(deleteTombstoneMetadataFieldMappers) +
", noopTombstoneMetadataFieldMappers=" + Arrays.toString(noopTombstoneMetadataFieldMappers) +
'}';
}
} }