From d05649bfaeb1060f8fa70e7a43880fd075c844e6 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Mon, 24 Aug 2020 18:52:05 +0200 Subject: [PATCH] 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 --- .../org/elasticsearch/client/enrich/PutPolicyRequest.java | 7 +++++-- .../elasticsearch/client/enrich/PutPolicyRequestTests.java | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/PutPolicyRequest.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/PutPolicyRequest.java index 0eb902dfe46..8ffc039517a 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/PutPolicyRequest.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/enrich/PutPolicyRequest.java @@ -78,7 +78,10 @@ public final class PutPolicyRequest implements Validatable, ToXContentObject { 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; } @@ -106,7 +109,7 @@ public final class PutPolicyRequest implements Validatable, ToXContentObject { { builder.field(NamedPolicy.INDICES_FIELD.getPreferredName(), indices); 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.ENRICH_FIELDS_FIELD.getPreferredName(), enrichFields); diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/PutPolicyRequestTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/PutPolicyRequestTests.java index a0138949490..7edb50309a8 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/PutPolicyRequestTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/enrich/PutPolicyRequestTests.java @@ -94,7 +94,7 @@ public class PutPolicyRequestTests extends AbstractRequestTestCase