Skip to content
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
5 changes: 5 additions & 0 deletions docs/changelog/123403.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 123403
summary: Use ordered maps for `PipelineConfiguration` xcontent deserialization
area: Ingest Node
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public final class PipelineConfiguration implements SimpleDiffable<PipelineConfi
static {
PARSER.declareString(Builder::setId, new ParseField("id"));
PARSER.declareField(
(parser, builder, aVoid) -> builder.setConfig(parser.map()),
(parser, builder, aVoid) -> builder.setConfig(parser.mapOrdered()),
new ParseField("config"),
ObjectParser.ValueType.OBJECT
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.sameInstance;
Expand Down Expand Up @@ -143,6 +145,41 @@ public void testGetVersion() {
}
}

@SuppressWarnings("unchecked")
public void testMapKeyOrderingRoundTrip() throws IOException {
// make up two random keys
String key1 = randomAlphaOfLength(10);
String key2 = randomValueOtherThan(key1, () -> randomAlphaOfLength(10));
// stick them as mappings onto themselves in the _meta of a pipeline configuration
// this happens to use the _meta as a convenient map to test that the ordering of the key sets is the same
String configJson = Strings.format("""
{"description": "blah", "_meta" : {"foo": "bar", "%s": "%s", "%s": "%s"}}""", key1, key1, key2, key2);
PipelineConfiguration configuration = new PipelineConfiguration(
"1",
new BytesArray(configJson.getBytes(StandardCharsets.UTF_8)),
XContentType.JSON
);

// serialize it to bytes
XContentType xContentType = randomFrom(XContentType.values());
final BytesReference bytes;
try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
configuration.toXContent(builder, ToXContent.EMPTY_PARAMS);
bytes = BytesReference.bytes(builder);
}

// deserialize it back
ContextParser<Void, PipelineConfiguration> parser = PipelineConfiguration.getParser();
XContentParser xContentParser = xContentType.xContent()
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput());
PipelineConfiguration parsed = parser.parse(xContentParser, null);

// make sure the _meta key sets are in the same order
Set<String> keys1 = ((Map<String, Object>) configuration.getConfig().get("_meta")).keySet();
Set<String> keys2 = ((Map<String, Object>) parsed.getConfig().get("_meta")).keySet();
assertThat(keys1, contains(keys2.toArray(new String[0])));
}

@Override
protected PipelineConfiguration createTestInstance() {
BytesArray config;
Expand Down