From d862453d6822525fc7b14ec60a5496767dd7fa8e Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Tue, 5 Feb 2019 07:52:17 -0700 Subject: [PATCH] Support unknown fields in ingest pipeline map configuration (#38352) We already support unknown objects in the list of pipelines, this changes the `PipelineConfiguration` to support fields other than just `id` and `config`. Relates to #36938 --- .../action/ingest/GetPipelineResponse.java | 19 +++++++----- .../ingest/PipelineConfiguration.java | 8 ++++- .../ingest/PipelineConfigurationTests.java | 31 +++++++++++++++++-- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/ingest/GetPipelineResponse.java b/server/src/main/java/org/elasticsearch/action/ingest/GetPipelineResponse.java index 297a7f0efc1..fdbd2414313 100644 --- a/server/src/main/java/org/elasticsearch/action/ingest/GetPipelineResponse.java +++ b/server/src/main/java/org/elasticsearch/action/ingest/GetPipelineResponse.java @@ -20,6 +20,7 @@ package org.elasticsearch.action.ingest; import org.elasticsearch.action.ActionResponse; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -109,13 +110,12 @@ public class GetPipelineResponse extends ActionResponse implements StatusToXCont while(parser.nextToken().equals(Token.FIELD_NAME)) { String pipelineId = parser.currentName(); parser.nextToken(); - XContentBuilder contentBuilder = XContentBuilder.builder(parser.contentType().xContent()); - contentBuilder.generator().copyCurrentStructure(parser); - PipelineConfiguration pipeline = - new PipelineConfiguration( - pipelineId, BytesReference.bytes(contentBuilder), contentBuilder.contentType() - ); - pipelines.add(pipeline); + try (XContentBuilder contentBuilder = XContentBuilder.builder(parser.contentType().xContent())) { + contentBuilder.generator().copyCurrentStructure(parser); + PipelineConfiguration pipeline = + new PipelineConfiguration(pipelineId, BytesReference.bytes(contentBuilder), contentBuilder.contentType()); + pipelines.add(pipeline); + } } ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.currentToken(), parser::getTokenLocation); return new GetPipelineResponse(pipelines); @@ -148,6 +148,11 @@ public class GetPipelineResponse extends ActionResponse implements StatusToXCont } } + @Override + public String toString() { + return Strings.toString(this); + } + @Override public int hashCode() { int result = 1; diff --git a/server/src/main/java/org/elasticsearch/ingest/PipelineConfiguration.java b/server/src/main/java/org/elasticsearch/ingest/PipelineConfiguration.java index 6778f3d1eaa..81ef55ecf40 100644 --- a/server/src/main/java/org/elasticsearch/ingest/PipelineConfiguration.java +++ b/server/src/main/java/org/elasticsearch/ingest/PipelineConfiguration.java @@ -22,6 +22,7 @@ package org.elasticsearch.ingest; import org.elasticsearch.cluster.AbstractDiffable; import org.elasticsearch.cluster.Diff; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -41,7 +42,7 @@ import java.util.Objects; */ public final class PipelineConfiguration extends AbstractDiffable implements ToXContentObject { - private static final ObjectParser PARSER = new ObjectParser<>("pipeline_config", Builder::new); + private static final ObjectParser PARSER = new ObjectParser<>("pipeline_config", true, Builder::new); static { PARSER.declareString(Builder::setId, new ParseField("id")); PARSER.declareField((parser, builder, aVoid) -> { @@ -123,6 +124,11 @@ public final class PipelineConfiguration extends AbstractDiffable { public void testSerialization() throws IOException { PipelineConfiguration configuration = new PipelineConfiguration("1", @@ -68,4 +69,30 @@ public class PipelineConfigurationTests extends ESTestCase { assertEquals("{}", XContentHelper.convertToJson(parsed.getConfig(), false, parsed.getXContentType())); assertEquals("1", parsed.getId()); } + + @Override + protected PipelineConfiguration createTestInstance() { + BytesArray config; + if (randomBoolean()) { + config = new BytesArray("{}".getBytes(StandardCharsets.UTF_8)); + } else { + config = new BytesArray("{\"foo\": \"bar\"}".getBytes(StandardCharsets.UTF_8)); + } + return new PipelineConfiguration(randomAlphaOfLength(4), config, XContentType.JSON); + } + + @Override + protected PipelineConfiguration doParseInstance(XContentParser parser) throws IOException { + return PipelineConfiguration.getParser().parse(parser, null); + } + + @Override + protected boolean supportsUnknownFields() { + return true; + } + + @Override + protected Predicate getRandomFieldsExcludeFilter() { + return field -> field.equals("config"); + } }