Skip to content

Commit 4d8dcba

Browse files
authored
Use ordered maps for PipelineConfiguration xcontent deserialization (#123403) (#123415)
1 parent 83e8015 commit 4d8dcba

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

docs/changelog/123403.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 123403
2+
summary: Use ordered maps for `PipelineConfiguration` xcontent deserialization
3+
area: Ingest Node
4+
type: bug
5+
issues: []

server/src/main/java/org/elasticsearch/ingest/PipelineConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public final class PipelineConfiguration implements SimpleDiffable<PipelineConfi
4646
static {
4747
PARSER.declareString(Builder::setId, new ParseField("id"));
4848
PARSER.declareField(
49-
(parser, builder, aVoid) -> builder.setConfig(parser.map()),
49+
(parser, builder, aVoid) -> builder.setConfig(parser.mapOrdered()),
5050
new ParseField("config"),
5151
ObjectParser.ValueType.OBJECT
5252
);

server/src/test/java/org/elasticsearch/ingest/PipelineConfigurationTests.java

+37
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
import java.nio.charset.StandardCharsets;
2929
import java.util.HashMap;
3030
import java.util.Map;
31+
import java.util.Set;
3132
import java.util.function.Predicate;
3233

3334
import static org.hamcrest.Matchers.anEmptyMap;
35+
import static org.hamcrest.Matchers.contains;
3436
import static org.hamcrest.Matchers.equalTo;
3537
import static org.hamcrest.Matchers.not;
3638
import static org.hamcrest.Matchers.sameInstance;
@@ -143,6 +145,41 @@ public void testGetVersion() {
143145
}
144146
}
145147

148+
@SuppressWarnings("unchecked")
149+
public void testMapKeyOrderingRoundTrip() throws IOException {
150+
// make up two random keys
151+
String key1 = randomAlphaOfLength(10);
152+
String key2 = randomValueOtherThan(key1, () -> randomAlphaOfLength(10));
153+
// stick them as mappings onto themselves in the _meta of a pipeline configuration
154+
// this happens to use the _meta as a convenient map to test that the ordering of the key sets is the same
155+
String configJson = Strings.format("""
156+
{"description": "blah", "_meta" : {"foo": "bar", "%s": "%s", "%s": "%s"}}""", key1, key1, key2, key2);
157+
PipelineConfiguration configuration = new PipelineConfiguration(
158+
"1",
159+
new BytesArray(configJson.getBytes(StandardCharsets.UTF_8)),
160+
XContentType.JSON
161+
);
162+
163+
// serialize it to bytes
164+
XContentType xContentType = randomFrom(XContentType.values());
165+
final BytesReference bytes;
166+
try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
167+
configuration.toXContent(builder, ToXContent.EMPTY_PARAMS);
168+
bytes = BytesReference.bytes(builder);
169+
}
170+
171+
// deserialize it back
172+
ContextParser<Void, PipelineConfiguration> parser = PipelineConfiguration.getParser();
173+
XContentParser xContentParser = xContentType.xContent()
174+
.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput());
175+
PipelineConfiguration parsed = parser.parse(xContentParser, null);
176+
177+
// make sure the _meta key sets are in the same order
178+
Set<String> keys1 = ((Map<String, Object>) configuration.getConfig().get("_meta")).keySet();
179+
Set<String> keys2 = ((Map<String, Object>) parsed.getConfig().get("_meta")).keySet();
180+
assertThat(keys1, contains(keys2.toArray(new String[0])));
181+
}
182+
146183
@Override
147184
protected PipelineConfiguration createTestInstance() {
148185
BytesArray config;

0 commit comments

Comments
 (0)