Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AliasMetaData#fromXContent parsing #30866

Merged
merged 2 commits into from
May 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.ToXContentFragment;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;
import java.util.Collections;
Expand All @@ -42,7 +44,7 @@

import static java.util.Collections.emptySet;

public class AliasMetaData extends AbstractDiffable<AliasMetaData> {
public class AliasMetaData extends AbstractDiffable<AliasMetaData> implements ToXContentFragment {

private final String alias;

Expand Down Expand Up @@ -199,6 +201,17 @@ public static Diff<AliasMetaData> readDiffFrom(StreamInput in) throws IOExceptio
return readDiffFrom(AliasMetaData::new, in);
}

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

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
AliasMetaData.Builder.toXContent(this, builder, params);
return builder;
}

public static class Builder {

private final String alias;
Expand Down Expand Up @@ -314,6 +327,8 @@ public static AliasMetaData fromXContent(XContentParser parser) throws IOExcepti
if ("filter".equals(currentFieldName)) {
Map<String, Object> filter = parser.mapOrdered();
builder.filter(filter);
} else {
parser.skipChildren();
}
} else if (token == XContentParser.Token.VALUE_EMBEDDED_OBJECT) {
if ("filter".equals(currentFieldName)) {
Expand All @@ -327,6 +342,8 @@ public static AliasMetaData fromXContent(XContentParser parser) throws IOExcepti
} else if ("search_routing".equals(currentFieldName) || "searchRouting".equals(currentFieldName)) {
builder.searchRouting(parser.text());
}
} else if (token == XContentParser.Token.START_ARRAY) {
parser.skipChildren();
}
}
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@

package org.elasticsearch.cluster.metadata;

import org.elasticsearch.cluster.metadata.AliasMetaData.Builder;
import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.common.xcontent.XContent;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.test.AbstractXContentTestCase;

import java.io.IOException;
import java.util.function.Predicate;

import static org.hamcrest.Matchers.equalTo;

public class AliasMetaDataTests extends ESTestCase {
public class AliasMetaDataTests extends AbstractXContentTestCase<AliasMetaData> {

public void testSerialization() throws IOException {
final AliasMetaData before =
Expand All @@ -52,4 +53,49 @@ public void testSerialization() throws IOException {

assertThat(after, equalTo(before));
}

@Override
protected AliasMetaData createTestInstance() {
return createTestItem();
}

@Override
protected Predicate<String> getRandomFieldsExcludeFilter() {
return p -> p.equals("") // do not add elements at the top-level as any element at this level is parsed as a new alias
|| p.contains(".filter"); // do not insert random data into AliasMetaData#filter
}

@Override
protected AliasMetaData doParseInstance(XContentParser parser) throws IOException {
if (parser.nextToken() == XContentParser.Token.START_OBJECT) {
parser.nextToken();
}
assertEquals(XContentParser.Token.FIELD_NAME, parser.currentToken());
AliasMetaData aliasMetaData = AliasMetaData.Builder.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
return aliasMetaData;
}

@Override
protected boolean supportsUnknownFields() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a comment that clarifies this is needed as this class is used by the high-level REST client. I am quite worried that this class is internal (like many others though) yet its parsing may be modified without taking the client into account.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javanna I can add a comment later today or the commit msg is enough?
Sorry for getting back to you that late.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no worries at all, it was not that important. and I was not willing to wait for another test run after adding the comment addition so I merged :)

return true;
}

private static AliasMetaData createTestItem() {
Builder builder = AliasMetaData.builder(randomAlphaOfLengthBetween(3, 10));
if (randomBoolean()) {
builder.routing(randomAlphaOfLengthBetween(3, 10));
}
if (randomBoolean()) {
builder.searchRouting(randomAlphaOfLengthBetween(3, 10));
}
if (randomBoolean()) {
builder.indexRouting(randomAlphaOfLengthBetween(3, 10));
}
if (randomBoolean()) {
builder.filter("{\"term\":{\"year\":2016}}");
}
return builder.build();
}

}