Skip to content

Commit

Permalink
[Tests] Fix alias names in PutIndexTemplateRequestTests (#30960)
Browse files Browse the repository at this point in the history
The randomized alias names could contain unicode controll charactes that don't
survive an xContent rendering and parsing roundtrip when using the YAML xContent
type. This fix filters the randomized unicode string for control characters to
avoid this particular problem.

Closes #30911
  • Loading branch information
Christoph Büscher committed Jun 1, 2018
1 parent 443e47d commit 61316f0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.ToXContent.Params;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
Expand All @@ -42,7 +43,7 @@
/**
* Represents an alias, to be associated with an index
*/
public class Alias implements Streamable, ToXContentObject {
public class Alias implements Streamable, ToXContentFragment {

private static final ParseField FILTER = new ParseField("filter");
private static final ParseField ROUTING = new ParseField("routing");
Expand Down Expand Up @@ -248,6 +249,11 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

@Override
public String toString() {
return Strings.toString(this);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ protected PutIndexTemplateRequest createTestInstance() {
request.patterns(Arrays.asList(generateRandomStringArray(20, 100, false, false)));
int numAlias = between(0, 5);
for (int i = 0; i < numAlias; i++) {
Alias alias = new Alias(randomRealisticUnicodeOfLengthBetween(1, 10));
// some ASCII or Latin-1 control characters, especially newline, can lead to
// problems with yaml parsers, that's why we filter them here (see #30911)
Alias alias = new Alias(randomRealisticUnicodeOfLengthBetween(1, 10).replaceAll("\\p{Cc}", ""));
if (randomBoolean()) {
alias.indexRouting(randomRealisticUnicodeOfLengthBetween(1, 10));
}
Expand Down

0 comments on commit 61316f0

Please sign in to comment.