From 49228aa5dcbae5c4216310117daba586a63e6778 Mon Sep 17 00:00:00 2001 From: Mark Payne Date: Thu, 7 Jun 2018 15:44:22 -0400 Subject: [PATCH] NIFI-5281: If value is not valid according to the schema's CHOICE field, JSON Writer should write null value instead of throwing NullPointerException Signed-off-by: Pierre Villard This closes #2772. --- .../java/org/apache/nifi/json/WriteJsonResult.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/json/WriteJsonResult.java b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/json/WriteJsonResult.java index c596ae2187..d113f8d916 100755 --- a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/json/WriteJsonResult.java +++ b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/json/WriteJsonResult.java @@ -55,7 +55,6 @@ public class WriteJsonResult extends AbstractRecordSetWriter implements RecordSe private final RecordSchema recordSchema; private final JsonFactory factory = new JsonFactory(); private final JsonGenerator generator; - private final OutputStream out; private final NullSuppression nullSuppression; private final OutputGrouping outputGrouping; private final Supplier LAZY_DATE_FORMAT; @@ -69,7 +68,6 @@ public class WriteJsonResult extends AbstractRecordSetWriter implements RecordSe this.logger = logger; this.recordSchema = recordSchema; this.schemaAccess = schemaAccess; - this.out = out; this.nullSuppression = nullSuppression; this.outputGrouping = outputGrouping; @@ -270,14 +268,19 @@ public class WriteJsonResult extends AbstractRecordSetWriter implements RecordSe } @SuppressWarnings("unchecked") - private void writeValue(final JsonGenerator generator, final Object value, final String fieldName, final DataType dataType) - throws JsonGenerationException, IOException { + private void writeValue(final JsonGenerator generator, final Object value, final String fieldName, final DataType dataType) throws JsonGenerationException, IOException { if (value == null) { generator.writeNull(); return; } final DataType chosenDataType = dataType.getFieldType() == RecordFieldType.CHOICE ? DataTypeUtils.chooseDataType(value, (ChoiceDataType) dataType) : dataType; + if (chosenDataType == null) { + logger.debug("Could not find a suitable field type in the CHOICE for field {} and value {}; will use null value", new Object[] {fieldName, value}); + generator.writeNull(); + return; + } + final Object coercedValue = DataTypeUtils.convertType(value, chosenDataType, LAZY_DATE_FORMAT, LAZY_TIME_FORMAT, LAZY_TIMESTAMP_FORMAT, fieldName); if (coercedValue == null) { generator.writeNull();