ChrisSamo632 commented on code in PR #7746:
URL: https://github.com/apache/nifi/pull/7746#discussion_r1354608522


##########
nifi-commons/nifi-record/src/main/java/org/apache/nifi/serialization/record/util/DataTypeUtils.java:
##########
@@ -854,79 +855,82 @@ public static Map<String, Object> toMap(final Object 
value, final String fieldNa
      */
     @SuppressWarnings({"unchecked", "rawtypes"})
     public static Object convertRecordFieldtoObject(final Object value, final 
DataType dataType) {
-
         if (value == null) {
             return null;
         }
 
+        DataType chosenDataType;
+        if (dataType instanceof ChoiceDataType) {
+            final DataType chosen = chooseDataType(value, (ChoiceDataType) 
dataType);
+            chosenDataType = chosen != null ? chosen : dataType;
+        } else {
+            chosenDataType = dataType;
+        }
+
         if (value instanceof Record) {
-            Record record = (Record) value;
-            RecordSchema recordSchema = record.getSchema();
+            final Record record = (Record) value;
+            final RecordSchema recordSchema = record.getSchema();
             if (recordSchema == null) {
                 throw new IllegalTypeConversionException("Cannot convert value 
of type Record to Map because Record does not have an associated Schema");
             }
 
-            final Map<String, Object> recordMap = new LinkedHashMap<>();
-            for (RecordField field : recordSchema.getFields()) {
-                final DataType fieldDataType = field.getDataType();
+            final Map<String, Object> recordMap = new 
LinkedHashMap<>(record.getRawFieldNames().size(), 1);
+            for (final RecordField field : recordSchema.getFields()) {
+                if (field.getDataType() instanceof ChoiceDataType) {
+                    final DataType chosen = chooseDataType(value, 
(ChoiceDataType) field.getDataType());
+                    chosenDataType = chosen != null ? chosen : 
field.getDataType();
+                } else {
+                    chosenDataType = field.getDataType();
+                }
                 final String fieldName = field.getFieldName();
-                Object fieldValue = record.getValue(fieldName);
+                final Object fieldValue = record.getValue(fieldName);
 
                 if (fieldValue == null) {
                     recordMap.put(fieldName, null);
-                } else if (isScalarValue(fieldDataType, fieldValue)) {
+                } else if (isScalarValue(chosenDataType, fieldValue)) {
                     recordMap.put(fieldName, fieldValue);
-                } else if (fieldDataType instanceof RecordDataType) {
+                } else if (chosenDataType instanceof RecordDataType) {
                     Record nestedRecord = (Record) fieldValue;

Review Comment:
   Recent example from the [NiFi Dev Mailing 
List](https://lists.apache.org/thread/kcnsxvbbdfwfhj0tdsyn53x8ljhgdt1v) 
containing some failing data and associated Jolt Transform Spec.
   
   Currently fails on this branch with:
   ```
   java.lang.ClassCastException: class java.lang.String cannot be cast to class 
org.apache.nifi.serialization.record.Record (java.lang.String is in module 
java.base of loader 'bootstrap'; org.apache.nifi.serialization.record.Record is 
in unnamed module of loader org.apache.nifi.nar.NarClassLoader @4b3ad7ca)
        at 
org.apache.nifi.serialization.record.util.DataTypeUtils.convertRecordFieldtoObject(DataTypeUtils.java:893)
        at 
org.apache.nifi.processors.jolt.record.JoltTransformRecord.transform(JoltTransformRecord.java:425)
   ```
   
   Fails on `main`/`support/nifi-1.x` with:
   
   ```
   Cannot convert value [MapRecord[{A: 192,B: 32,C: 13,D: 19}]] of
   CHOICE[STRING,RECORD] to Map for field dimensions because type is not
   supported.
   ```
   
   So this change for NIFI-8135 has at least had an impact, albeit not quite 
resolved the problem



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to