Skip to content

Commit 1cc1885

Browse files
authored
Do not prefix already prefixed format with 8 (#48139)
It happens when a locale is specified on a mapping that a new JavaDateFormatter is created with a new mapping and already prefixed with 8 format is prefixed again. This should change avoids this behavior. relates #47983
1 parent 269e9ba commit 1cc1885

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

server/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ check.dependsOn(testScriptDocValuesMissingV7Behaviour)
150150
unitTest {
151151
// these are tested explicitly in separate test tasks
152152
exclude '**/*ScriptDocValuesMissingV7BehaviourTests.class'
153+
jvmArg '-Djava.locale.providers=COMPAT'
154+
153155
}
154156

155157
forbiddenPatterns {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class JavaDateFormatter implements DateFormatter {
7676
throw new IllegalArgumentException("formatters must have the same locale");
7777
}
7878
this.printer = printer;
79-
this.format = "8" + format;
79+
this.format = format.startsWith("8") ? format : "8" + format;
8080

8181
if (parsers.length == 0) {
8282
this.parsers = Collections.singletonList(printer);

server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.elasticsearch.test.ESIntegTestCase;
4343
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
4444
import org.elasticsearch.test.ESIntegTestCase.Scope;
45+
import org.hamcrest.Matchers;
4546

4647
import java.util.HashMap;
4748
import java.util.List;
@@ -56,8 +57,6 @@
5657
import static org.hamcrest.Matchers.allOf;
5758
import static org.hamcrest.Matchers.equalTo;
5859
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
59-
import static org.hamcrest.Matchers.hasSize;
60-
import static org.hamcrest.Matchers.is;
6160
import static org.hamcrest.Matchers.lessThanOrEqualTo;
6261
import static org.hamcrest.core.IsNull.notNullValue;
6362

@@ -385,21 +384,38 @@ public void testIndexNameInResponse() {
385384
}
386385

387386
public void testCreateIndexWithJava8Date() throws Exception {
388-
String jodaIncompatibleFormat = "8yyyy-MM-dd HH:mm:ssXX";
389-
XContentBuilder builder = jsonBuilder().startObject().startObject("properties")
390-
.startObject("time")
391-
.field("type", "date")
392-
.field("format", jodaIncompatibleFormat)
393-
.endObject().endObject().endObject();
394-
395-
CreateIndexRequestBuilder requestBuilder = client().admin().indices().prepareCreate("test");
396-
assertAcked(requestBuilder.addMapping("doc", builder).get());
387+
BiFunction<XContentBuilder, String, List<Object>> createIndex = (xContentBuilder, propertyToBeReturned) -> {
388+
String indexName = "test" + xContentBuilder.hashCode();
389+
CreateIndexRequestBuilder requestBuilder = client().admin().indices().prepareCreate(indexName);
390+
assertAcked(requestBuilder.addMapping("doc", xContentBuilder).get());
391+
392+
GetMappingsResponse response = client().admin().indices().prepareGetMappings(indexName).get();
393+
List<Object> formats = XContentMapValues.extractRawValues(propertyToBeReturned, response
394+
.getMappings()
395+
.get(indexName)
396+
.get("doc")
397+
.getSourceAsMap());
398+
return formats;
399+
};
397400

398-
GetMappingsResponse response = client().admin().indices().prepareGetMappings("test").get();
399-
List<Object> formats =
400-
XContentMapValues.extractRawValues("properties.time.format", response.getMappings().get("test").get("doc").getSourceAsMap());
401-
assertThat(formats, hasSize(1));
402-
assertThat(formats.get(0), is(jodaIncompatibleFormat));
401+
XContentBuilder builder = jsonBuilder().startObject().startObject("properties")
402+
.startObject("time")
403+
.field("type", "date")
404+
.field("format", "8yyyy-MM-dd-MM-dd HH:mm:ssXX")
405+
.endObject()
406+
.endObject().endObject();
407+
408+
assertThat(createIndex.apply(builder, "properties.time.format"), Matchers.contains("8yyyy-MM-dd-MM-dd HH:mm:ssXX"));
409+
410+
411+
builder = jsonBuilder().startObject().startObject("properties")
412+
.startObject("time")
413+
.field("type", "date")
414+
.field("format", "8yyyy-MM-dd-MM-dd HH:mm:ssXX")
415+
.field("locale", "de")
416+
.endObject()
417+
.endObject().endObject();
418+
assertThat(createIndex.apply(builder, "properties.time.locale"), Matchers.contains("de"));
403419
}
404420

405421
}

server/src/test/java/org/elasticsearch/index/mapper/DateFieldMapperTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,18 @@ public void testFloatEpochFormat() throws IOException {
270270
assertEquals(epochMillis, pointField.numericValue().longValue());
271271
}
272272

273+
public void testChangeLocaleWith8Prefix() throws IOException {
274+
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
275+
.startObject("properties").startObject("field").field("type", "date")
276+
.field("format", "8E, d MMM uuuu HH:mm:ss Z")
277+
.field("locale", "de")
278+
.endObject().endObject().endObject().endObject());
279+
280+
DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
281+
282+
assertEquals(mapping, mapper.mappingSource().toString());
283+
}
284+
273285
public void testChangeLocale() throws IOException {
274286
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
275287
.startObject("properties").startObject("field").field("type", "date").field("locale", "fr").endObject().endObject()

0 commit comments

Comments
 (0)