Fix PutPolicyRequestTests.testFromXContent (#61485) (#61494)

We only ever support `JSON` for the query source format in practice.
The reason this test worked before is a bug in xcontent parsing that parses
empty maps out of streams of the wrong format.

Closes #61483
This commit is contained in:
Armin Braun 2020-08-24 18:52:05 +02:00 committed by GitHub
parent bb4d97073c
commit d05649bfae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 3 deletions

View File

@ -78,7 +78,10 @@ public final class PutPolicyRequest implements Validatable, ToXContentObject {
return query; return query;
} }
public void setQuery(BytesReference query) { // package private for testing only
void setQuery(BytesReference query) {
assert query == null || XContentHelper.xContentType(query) == XContentType.JSON :
"Only accepts JSON encoded query but received [" + Strings.toString(query) + "]";
this.query = query; this.query = query;
} }
@ -106,7 +109,7 @@ public final class PutPolicyRequest implements Validatable, ToXContentObject {
{ {
builder.field(NamedPolicy.INDICES_FIELD.getPreferredName(), indices); builder.field(NamedPolicy.INDICES_FIELD.getPreferredName(), indices);
if (query != null) { if (query != null) {
builder.field(NamedPolicy.QUERY_FIELD.getPreferredName(), asMap(query, builder.contentType())); builder.field(NamedPolicy.QUERY_FIELD.getPreferredName(), asMap(query, XContentType.JSON));
} }
builder.field(NamedPolicy.MATCH_FIELD_FIELD.getPreferredName(), matchField); builder.field(NamedPolicy.MATCH_FIELD_FIELD.getPreferredName(), matchField);
builder.field(NamedPolicy.ENRICH_FIELDS_FIELD.getPreferredName(), enrichFields); builder.field(NamedPolicy.ENRICH_FIELDS_FIELD.getPreferredName(), enrichFields);

View File

@ -94,7 +94,7 @@ public class PutPolicyRequestTests extends AbstractRequestTestCase<PutPolicyRequ
assertThat(clientTestInstance.getIndices(), equalTo(serverInstance.getPolicy().getIndices())); assertThat(clientTestInstance.getIndices(), equalTo(serverInstance.getPolicy().getIndices()));
if (clientTestInstance.getQuery() != null) { if (clientTestInstance.getQuery() != null) {
XContentType type = serverInstance.getPolicy().getQuery().getContentType(); XContentType type = serverInstance.getPolicy().getQuery().getContentType();
assertThat(PutPolicyRequest.asMap(clientTestInstance.getQuery(), type), assertThat(PutPolicyRequest.asMap(clientTestInstance.getQuery(), XContentType.JSON),
equalTo(PutPolicyRequest.asMap(serverInstance.getPolicy().getQuery().getQuery(), type))); equalTo(PutPolicyRequest.asMap(serverInstance.getPolicy().getQuery().getQuery(), type)));
} else { } else {
assertThat(serverInstance.getPolicy().getQuery(), nullValue()); assertThat(serverInstance.getPolicy().getQuery(), nullValue());