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

Support unknown fields in ingest pipeline map configuration #38429

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -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;
Expand Down Expand Up @@ -109,13 +110,12 @@ public static GetPipelineResponse fromXContent(XContentParser parser) throws IOE
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);
Expand Down Expand Up @@ -148,6 +148,11 @@ public boolean equals(Object other) {
}
}

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

@Override
public int hashCode() {
int result = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand All @@ -42,7 +43,7 @@
*/
public final class PipelineConfiguration extends AbstractDiffable<PipelineConfiguration> implements ToXContentObject {

private static final ObjectParser<Builder, Void> PARSER = new ObjectParser<>("pipeline_config", Builder::new);
private static final ObjectParser<Builder, Void> PARSER = new ObjectParser<>("pipeline_config", true, Builder::new);
static {
PARSER.declareString(Builder::setId, new ParseField("id"));
PARSER.declareField((parser, builder, aVoid) -> {
Expand Down Expand Up @@ -130,6 +131,11 @@ public static Diff<PipelineConfiguration> readDiffFrom(StreamInput in) throws IO
return readDiffFrom(PipelineConfiguration::readFrom, in);
}

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

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.AbstractXContentTestCase;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.function.Predicate;

public class PipelineConfigurationTests extends ESTestCase {
public class PipelineConfigurationTests extends AbstractXContentTestCase<PipelineConfiguration> {

public void testSerialization() throws IOException {
PipelineConfiguration configuration = new PipelineConfiguration("1",
Expand Down Expand Up @@ -68,4 +69,30 @@ public void testParser() throws IOException {
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<String> getRandomFieldsExcludeFilter() {
return field -> field.equals("config");
}
}