Skip to content

Commit 7a06678

Browse files
Making variant tag more lenient (#936) (#938)
* lifecycle response fix * checkstyle * update test * cleaner code Co-authored-by: Laura Trotta <[email protected]>
1 parent d01eff6 commit 7a06678

File tree

3 files changed

+102
-3
lines changed

3 files changed

+102
-3
lines changed

java-client/src/main/java/co/elastic/clients/json/JsonpUtils.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,23 @@ public static Map.Entry<String, JsonParser> lookAheadFieldValue(
243243
} else {
244244
// Unbuffered path: parse the object into a JsonObject, then extract the value and parse it again
245245
JsonObject object = parser.getObject();
246-
String result = object.getString(name, null);
246+
247+
String result = null;
248+
JsonValue value = object.get(name);
249+
// Handle enums and booleans promoted to enums
250+
if (value != null) {
251+
if (value.getValueType() == JsonValue.ValueType.STRING) {
252+
result = ((JsonString) value).getString();
253+
} else if (value.getValueType() == JsonValue.ValueType.TRUE) {
254+
result = "true";
255+
} else if (value.getValueType() == JsonValue.ValueType.FALSE) {
256+
result = "false";
257+
}
258+
}
259+
260+
if (result == null) {
261+
result = defaultValue;
262+
}
247263

248264
if (result == null) {
249265
result = defaultValue;

java-client/src/main/java/co/elastic/clients/json/jackson/JacksonJsonpParser.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,27 @@ public Map.Entry<String, JsonParser> lookAheadFieldValue(String name, String def
336336
if (fieldName.equals(name)) {
337337
// Found
338338
tb.copyCurrentEvent(parser);
339-
expectNextEvent(JsonToken.VALUE_STRING);
339+
340+
String result = null;
341+
switch (parser.nextToken()) {
342+
case VALUE_STRING:
343+
result = parser.getText();
344+
break;
345+
// Handle booleans promoted to enums
346+
case VALUE_TRUE:
347+
result = "true";
348+
break;
349+
case VALUE_FALSE:
350+
result = "false";
351+
break;
352+
default:
353+
expectEvent(JsonToken.VALUE_STRING);
354+
}
355+
340356
tb.copyCurrentEvent(parser);
341357

342358
return new AbstractMap.SimpleImmutableEntry<>(
343-
parser.getText(),
359+
result,
344360
new JacksonJsonpParser(
345361
JsonParserSequence.createFlattened(false, tb.asParser(), parser),
346362
mapper

java-client/src/test/java/co/elastic/clients/elasticsearch/model/VariantsTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
2727
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
2828
import co.elastic.clients.elasticsearch.core.SearchRequest;
29+
import co.elastic.clients.elasticsearch.ilm.ExplainLifecycleResponse;
30+
import co.elastic.clients.elasticsearch.ilm.explain_lifecycle.LifecycleExplainManaged;
31+
import co.elastic.clients.elasticsearch.ilm.explain_lifecycle.LifecycleExplainUnmanaged;
2932
import co.elastic.clients.elasticsearch.indices.GetMappingResponse;
3033
import co.elastic.clients.json.JsonData;
3134
import co.elastic.clients.testkit.ModelTestCase;
@@ -281,4 +284,68 @@ public void testContainerWithOptionalVariants() {
281284
assertEquals(2.0, fsq2.functionScore().functions().get(0).linear().untyped().placement().decay(), 0.001);
282285
}
283286
}
287+
288+
@Test
289+
public void testBooleanVariantTag() {
290+
291+
String jsonT = "{\n" +
292+
" \"indices\": {\n" +
293+
" \"test\": {\n" +
294+
" \"index\": \"test\",\n" +
295+
" \"managed\": true,\n" +
296+
" \"policy\": \"my_policy\",\n" +
297+
" \"index_creation_date_millis\": 1736785235558,\n" +
298+
" \"time_since_index_creation\": \"27.75d\",\n" +
299+
" \"lifecycle_date_millis\": 1736785235558,\n" +
300+
" \"age\": \"27.75d\",\n" +
301+
" \"phase\": \"warm\",\n" +
302+
" \"phase_time_millis\": 1739183166898,\n" +
303+
" \"action\": \"migrate\",\n" +
304+
" \"action_time_millis\": 1739183166898,\n" +
305+
" \"step\": \"check-migration\",\n" +
306+
" \"step_time_millis\": 1739183166898,\n" +
307+
" \"step_info\": {\n" +
308+
" \"message\": \"Waiting for all shard copies to be active\",\n" +
309+
" \"shards_left_to_allocate\": -1,\n" +
310+
" \"all_shards_active\": false,\n" +
311+
" \"number_of_replicas\": 1\n" +
312+
" },\n" +
313+
" \"phase_execution\": {\n" +
314+
" \"policy\": \"my_policy\",\n" +
315+
" \"phase_definition\": {\n" +
316+
" \"min_age\": \"10d\",\n" +
317+
" \"actions\": {\n" +
318+
" \"forcemerge\": {\n" +
319+
" \"max_num_segments\": 1\n" +
320+
" }\n" +
321+
" }\n" +
322+
" },\n" +
323+
" \"version\": 1,\n" +
324+
" \"modified_date_in_millis\": 1739183005443\n" +
325+
" }\n" +
326+
" }\n" +
327+
" }\n" +
328+
"}";
329+
330+
ExplainLifecycleResponse respT = fromJson(jsonT,ExplainLifecycleResponse.class);
331+
332+
// if managed is "true" then the variant class must be Managed
333+
assertTrue(respT.indices().get("test").isTrue());
334+
assertTrue(respT.indices().get("test")._get().getClass().equals(LifecycleExplainManaged.class));
335+
336+
String jsonF = "{\n" +
337+
" \"indices\": {\n" +
338+
" \"test\": {\n" +
339+
" \"index\": \"test\",\n" +
340+
" \"managed\": false\n" +
341+
" }\n" +
342+
" }\n" +
343+
"}";
344+
345+
ExplainLifecycleResponse respF = fromJson(jsonF,ExplainLifecycleResponse.class);
346+
347+
// if managed is "false" then the variant class must be Unmanaged
348+
assertTrue(respF.indices().get("test").isFalse());
349+
assertTrue(respF.indices().get("test")._get().getClass().equals(LifecycleExplainUnmanaged.class));
350+
}
284351
}

0 commit comments

Comments
 (0)