From a59d93bbf381b679b5500b32f7865cc376f79988 Mon Sep 17 00:00:00 2001 From: jeroenvandisseldorp Date: Fri, 22 Nov 2024 12:49:34 +0100 Subject: [PATCH] Fix crash when using AVRO CharSequence encodings and nested objects * Fix NativeDataObjectMapper * Fix PythonDataObjectMapper --- .../ksml/data/mapper/NativeDataObjectMapper.java | 3 ++- .../main/java/io/axual/ksml/data/util/MapUtil.java | 12 ++++++++++++ .../io/axual/ksml/python/PythonDataObjectMapper.java | 3 ++- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 ksml-data/src/main/java/io/axual/ksml/data/util/MapUtil.java diff --git a/ksml-data/src/main/java/io/axual/ksml/data/mapper/NativeDataObjectMapper.java b/ksml-data/src/main/java/io/axual/ksml/data/mapper/NativeDataObjectMapper.java index f98ee43e..128e2c75 100644 --- a/ksml-data/src/main/java/io/axual/ksml/data/mapper/NativeDataObjectMapper.java +++ b/ksml-data/src/main/java/io/axual/ksml/data/mapper/NativeDataObjectMapper.java @@ -27,6 +27,7 @@ import io.axual.ksml.data.schema.SchemaLibrary; import io.axual.ksml.data.schema.StructSchema; import io.axual.ksml.data.type.*; +import io.axual.ksml.data.util.MapUtil; import io.axual.ksml.data.value.Tuple; import java.util.ArrayList; @@ -98,7 +99,7 @@ public DataObject toDataObject(DataType expected, Object value) { if (value instanceof List val) return nativeToDataList((List) val, expected instanceof ListType expectedList ? expectedList.valueType() : DataType.UNKNOWN); if (value instanceof Map val) - return nativeToDataStruct((Map) val, expected instanceof StructType expectedStruct ? expectedStruct.schema() : null); + return nativeToDataStruct(MapUtil.stringKeys(val), expected instanceof StructType expectedStruct ? expectedStruct.schema() : null); if (value instanceof Tuple val) return toDataTuple((Tuple) val); throw new ExecutionException("Can not convert to DataObject: " + value.getClass().getSimpleName()); } diff --git a/ksml-data/src/main/java/io/axual/ksml/data/util/MapUtil.java b/ksml-data/src/main/java/io/axual/ksml/data/util/MapUtil.java new file mode 100644 index 00000000..a1411ae8 --- /dev/null +++ b/ksml-data/src/main/java/io/axual/ksml/data/util/MapUtil.java @@ -0,0 +1,12 @@ +package io.axual.ksml.data.util; + +import java.util.HashMap; +import java.util.Map; + +public class MapUtil { + public static Map stringKeys(Map map) { + final var result = new HashMap(); + map.forEach((key, value) -> result.put(key != null ? key.toString() : null, value)); + return result; + } +} \ No newline at end of file diff --git a/ksml/src/main/java/io/axual/ksml/python/PythonDataObjectMapper.java b/ksml/src/main/java/io/axual/ksml/python/PythonDataObjectMapper.java index 6fd5c830..3be184f9 100644 --- a/ksml/src/main/java/io/axual/ksml/python/PythonDataObjectMapper.java +++ b/ksml/src/main/java/io/axual/ksml/python/PythonDataObjectMapper.java @@ -25,6 +25,7 @@ import io.axual.ksml.data.mapper.NativeDataObjectMapper; import io.axual.ksml.data.object.*; import io.axual.ksml.data.type.*; +import io.axual.ksml.data.util.MapUtil; import io.axual.ksml.util.ExecutionUtil; import org.graalvm.polyglot.Value; @@ -134,7 +135,7 @@ private Object arrayToNative(DataType expected, Value object) { private DataObject mapToNative(DataType expected, Value object) { Map map = ExecutionUtil.tryThis(() -> object.as(Map.class)); if (map == null) return null; - return nativeToDataStruct((Map) map, expected instanceof StructType structType ? structType.schema() : null); + return nativeToDataStruct(MapUtil.stringKeys(map), expected instanceof StructType structType ? structType.schema() : null); } @Override