Skip to content

Commit 502873b

Browse files
authored
[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
1 parent 0a73ba0 commit 502873b

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/DateFormat.java

-5
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,6 @@ private long parseMillis(String date) {
8282

8383
@Override
8484
Function<String, ZonedDateTime> getFunction(String format, ZoneId zoneId, Locale locale) {
85-
// support the 6.x BWC compatible way of parsing java 8 dates
86-
if (format.startsWith("8")) {
87-
format = format.substring(1);
88-
}
89-
9085
boolean isUtc = ZoneOffset.UTC.equals(zoneId);
9186

9287
DateFormatter dateFormatter = DateFormatter.forPattern(format)

rest-api-spec/src/main/resources/rest-api-spec/test/search/180_locale_dependent_mapping.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
---
22
"Test Index and Search locale dependent mappings / dates":
33
- skip:
4-
version: "all"
5-
reason: "Awaits fix: https://github.com/elastic/elasticsearch/issues/39981(Previously: JDK9 only supports this with a special sysproperty added in 6.2.0.)"
4+
version: " - 6.1.99"
5+
reason: JDK9 only supports this with a special sysproperty added in 6.2.0
66
- do:
77
indices.create:
88
index: test_index
@@ -13,7 +13,7 @@
1313
properties:
1414
date_field:
1515
type: date
16-
format: "E, d MMM yyyy HH:mm:ss Z"
16+
format: "8E, d MMM uuuu HH:mm:ss Z"
1717
locale: "de"
1818
- do:
1919
bulk:

server/src/main/java/org/elasticsearch/common/time/DateFormatter.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,28 @@ default String formatJoda(DateTime dateTime) {
129129
DateMathParser toDateMathParser();
130130

131131
static DateFormatter forPattern(String input) {
132+
132133
if (Strings.hasLength(input) == false) {
133134
throw new IllegalArgumentException("No date pattern provided");
134135
}
135136

136137
// support the 6.x BWC compatible way of parsing java 8 dates
137-
if (input.startsWith("8")) {
138-
input = input.substring(1);
139-
}
140-
141-
List<String> patterns = splitCombinedPatterns(input);
138+
String format = strip8Prefix(input);
139+
List<String> patterns = splitCombinedPatterns(format);
142140
List<DateFormatter> formatters = patterns.stream()
143141
.map(DateFormatters::forPattern)
144142
.collect(Collectors.toList());
145143

146-
if (formatters.size() == 1) {
147-
return formatters.get(0);
148-
}
149-
150144
return JavaDateFormatter.combined(input, formatters);
151145
}
152146

147+
static String strip8Prefix(String input) {
148+
if (input.startsWith("8")) {
149+
return input.substring(1);
150+
}
151+
return input;
152+
}
153+
153154
static List<String> splitCombinedPatterns(String input) {
154155
List<String> patterns = new ArrayList<>();
155156
for (String pattern : Strings.delimitedListToStringArray(input, "||")) {

server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java

+17
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,21 @@ public DocumentMapper updateFieldType(Map<String, MappedFieldType> fullNameToFie
339339
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
340340
return mapping.toXContent(builder, params);
341341
}
342+
343+
@Override
344+
public String toString() {
345+
return "DocumentMapper{" +
346+
"mapperService=" + mapperService +
347+
", type='" + type + '\'' +
348+
", typeText=" + typeText +
349+
", mappingSource=" + mappingSource +
350+
", mapping=" + mapping +
351+
", documentParser=" + documentParser +
352+
", fieldMappers=" + fieldMappers +
353+
", objectMappers=" + objectMappers +
354+
", hasNestedObjects=" + hasNestedObjects +
355+
", deleteTombstoneMetadataFieldMappers=" + Arrays.toString(deleteTombstoneMetadataFieldMappers) +
356+
", noopTombstoneMetadataFieldMappers=" + Arrays.toString(noopTombstoneMetadataFieldMappers) +
357+
'}';
358+
}
342359
}

0 commit comments

Comments
 (0)