Skip to content

[Backport 9.0] Making variant tag more lenient #939

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

Merged
merged 1 commit into from
Feb 17, 2025
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 @@ -243,7 +243,23 @@ public static Map.Entry<String, JsonParser> lookAheadFieldValue(
} else {
// Unbuffered path: parse the object into a JsonObject, then extract the value and parse it again
JsonObject object = parser.getObject();
String result = object.getString(name, null);

String result = null;
JsonValue value = object.get(name);
// Handle enums and booleans promoted to enums
if (value != null) {
if (value.getValueType() == JsonValue.ValueType.STRING) {
result = ((JsonString) value).getString();
} else if (value.getValueType() == JsonValue.ValueType.TRUE) {
result = "true";
} else if (value.getValueType() == JsonValue.ValueType.FALSE) {
result = "false";
}
}

if (result == null) {
result = defaultValue;
}

if (result == null) {
result = defaultValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,27 @@ public Map.Entry<String, JsonParser> lookAheadFieldValue(String name, String def
if (fieldName.equals(name)) {
// Found
tb.copyCurrentEvent(parser);
expectNextEvent(JsonToken.VALUE_STRING);

String result = null;
switch (parser.nextToken()) {
case VALUE_STRING:
result = parser.getText();
break;
// Handle booleans promoted to enums
case VALUE_TRUE:
result = "true";
break;
case VALUE_FALSE:
result = "false";
break;
default:
expectEvent(JsonToken.VALUE_STRING);
}

tb.copyCurrentEvent(parser);

return new AbstractMap.SimpleImmutableEntry<>(
parser.getText(),
result,
new JacksonJsonpParser(
JsonParserSequence.createFlattened(false, tb.asParser(), parser),
mapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.ilm.ExplainLifecycleResponse;
import co.elastic.clients.elasticsearch.ilm.explain_lifecycle.LifecycleExplainManaged;
import co.elastic.clients.elasticsearch.ilm.explain_lifecycle.LifecycleExplainUnmanaged;
import co.elastic.clients.elasticsearch.indices.GetMappingResponse;
import co.elastic.clients.json.JsonData;
import co.elastic.clients.testkit.ModelTestCase;
Expand Down Expand Up @@ -281,4 +284,68 @@ public void testContainerWithOptionalVariants() {
assertEquals(2.0, fsq2.functionScore().functions().get(0).linear().untyped().placement().decay(), 0.001);
}
}

@Test
public void testBooleanVariantTag() {

String jsonT = "{\n" +
" \"indices\": {\n" +
" \"test\": {\n" +
" \"index\": \"test\",\n" +
" \"managed\": true,\n" +
" \"policy\": \"my_policy\",\n" +
" \"index_creation_date_millis\": 1736785235558,\n" +
" \"time_since_index_creation\": \"27.75d\",\n" +
" \"lifecycle_date_millis\": 1736785235558,\n" +
" \"age\": \"27.75d\",\n" +
" \"phase\": \"warm\",\n" +
" \"phase_time_millis\": 1739183166898,\n" +
" \"action\": \"migrate\",\n" +
" \"action_time_millis\": 1739183166898,\n" +
" \"step\": \"check-migration\",\n" +
" \"step_time_millis\": 1739183166898,\n" +
" \"step_info\": {\n" +
" \"message\": \"Waiting for all shard copies to be active\",\n" +
" \"shards_left_to_allocate\": -1,\n" +
" \"all_shards_active\": false,\n" +
" \"number_of_replicas\": 1\n" +
" },\n" +
" \"phase_execution\": {\n" +
" \"policy\": \"my_policy\",\n" +
" \"phase_definition\": {\n" +
" \"min_age\": \"10d\",\n" +
" \"actions\": {\n" +
" \"forcemerge\": {\n" +
" \"max_num_segments\": 1\n" +
" }\n" +
" }\n" +
" },\n" +
" \"version\": 1,\n" +
" \"modified_date_in_millis\": 1739183005443\n" +
" }\n" +
" }\n" +
" }\n" +
"}";

ExplainLifecycleResponse respT = fromJson(jsonT,ExplainLifecycleResponse.class);

// if managed is "true" then the variant class must be Managed
assertTrue(respT.indices().get("test").isTrue());
assertTrue(respT.indices().get("test")._get().getClass().equals(LifecycleExplainManaged.class));

String jsonF = "{\n" +
" \"indices\": {\n" +
" \"test\": {\n" +
" \"index\": \"test\",\n" +
" \"managed\": false\n" +
" }\n" +
" }\n" +
"}";

ExplainLifecycleResponse respF = fromJson(jsonF,ExplainLifecycleResponse.class);

// if managed is "false" then the variant class must be Unmanaged
assertTrue(respF.indices().get("test").isFalse());
assertTrue(respF.indices().get("test")._get().getClass().equals(LifecycleExplainUnmanaged.class));
}
}