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


##########
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:
   Resolved, the `chooseDataType` call needed to use the `fieldValue`, not the 
`value` (of the Record passed to the method)



-- 
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