From 502873b1447a44d1ef34d377eb0dd73c54ee266d Mon Sep 17 00:00:00 2001 From: Przemyslaw Gomulka Date: Wed, 27 Nov 2019 12:29:18 +0100 Subject: [PATCH] [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 --- .../ingest/common/DateFormat.java | 5 ----- .../search/180_locale_dependent_mapping.yml | 6 +++--- .../common/time/DateFormatter.java | 19 ++++++++++--------- .../index/mapper/DocumentMapper.java | 17 +++++++++++++++++ 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java index c4b96bb06b0..e5902e7ccc0 100644 --- a/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java +++ b/modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java @@ -82,11 +82,6 @@ enum DateFormat { @Override Function 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); DateFormatter dateFormatter = DateFormatter.forPattern(format) diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search/180_locale_dependent_mapping.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search/180_locale_dependent_mapping.yml index 0bd139a070b..ba059e10fb6 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search/180_locale_dependent_mapping.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search/180_locale_dependent_mapping.yml @@ -1,8 +1,8 @@ --- "Test Index and Search locale dependent mappings / dates": - skip: - version: "all" - reason: "Awaits fix: https://github.com/elastic/elasticsearch/issues/39981(Previously: JDK9 only supports this with a special sysproperty added in 6.2.0.)" + version: " - 6.1.99" + reason: JDK9 only supports this with a special sysproperty added in 6.2.0 - do: indices.create: index: test_index @@ -13,7 +13,7 @@ properties: date_field: type: date - format: "E, d MMM yyyy HH:mm:ss Z" + format: "8E, d MMM uuuu HH:mm:ss Z" locale: "de" - do: bulk: diff --git a/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java b/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java index bacb12e680c..991dffea720 100644 --- a/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java +++ b/server/src/main/java/org/elasticsearch/common/time/DateFormatter.java @@ -129,27 +129,28 @@ public interface DateFormatter { DateMathParser toDateMathParser(); static DateFormatter forPattern(String input) { + if (Strings.hasLength(input) == false) { throw new IllegalArgumentException("No date pattern provided"); } // support the 6.x BWC compatible way of parsing java 8 dates - if (input.startsWith("8")) { - input = input.substring(1); - } - - List patterns = splitCombinedPatterns(input); + String format = strip8Prefix(input); + List patterns = splitCombinedPatterns(format); List formatters = patterns.stream() .map(DateFormatters::forPattern) .collect(Collectors.toList()); - if (formatters.size() == 1) { - return formatters.get(0); - } - return JavaDateFormatter.combined(input, formatters); } + static String strip8Prefix(String input) { + if (input.startsWith("8")) { + return input.substring(1); + } + return input; + } + static List splitCombinedPatterns(String input) { List patterns = new ArrayList<>(); for (String pattern : Strings.delimitedListToStringArray(input, "||")) { diff --git a/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java index 8de282787cc..467e31638bc 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java @@ -339,4 +339,21 @@ public class DocumentMapper implements ToXContentFragment { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { 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) + + '}'; + } }