Skip to content

Commit

Permalink
Fix crash when using AVRO CharSequence encodings and nested objects (#…
Browse files Browse the repository at this point in the history
…164)

* Fix NativeDataObjectMapper
* Fix PythonDataObjectMapper
  • Loading branch information
jeroenvandisseldorp authored Nov 22, 2024
1 parent f18e3be commit 81346eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,7 +99,7 @@ public DataObject toDataObject(DataType expected, Object value) {
if (value instanceof List<?> val)
return nativeToDataList((List<Object>) val, expected instanceof ListType expectedList ? expectedList.valueType() : DataType.UNKNOWN);
if (value instanceof Map<?, ?> val)
return nativeToDataStruct((Map<String, Object>) 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<Object>) val);
throw new ExecutionException("Can not convert to DataObject: " + value.getClass().getSimpleName());
}
Expand Down
12 changes: 12 additions & 0 deletions ksml-data/src/main/java/io/axual/ksml/data/util/MapUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.axual.ksml.data.util;

import java.util.HashMap;
import java.util.Map;

public class MapUtil {
public static Map<String, Object> stringKeys(Map<?, ?> map) {
final var result = new HashMap<String, Object>();
map.forEach((key, value) -> result.put(key != null ? key.toString() : null, value));
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String, Object>) map, expected instanceof StructType structType ? structType.schema() : null);
return nativeToDataStruct(MapUtil.stringKeys(map), expected instanceof StructType structType ? structType.schema() : null);
}

@Override
Expand Down

0 comments on commit 81346eb

Please sign in to comment.