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 <pierre.villard.fr@gmail.com>

This closes #2772.
This commit is contained in:
Mark Payne 2018-06-07 15:44:22 -04:00 committed by Pierre Villard
parent 0e09b98b02
commit 49228aa5dc
1 changed files with 7 additions and 4 deletions

View File

@ -55,7 +55,6 @@ public class WriteJsonResult extends AbstractRecordSetWriter implements RecordSe
private final RecordSchema recordSchema; private final RecordSchema recordSchema;
private final JsonFactory factory = new JsonFactory(); private final JsonFactory factory = new JsonFactory();
private final JsonGenerator generator; private final JsonGenerator generator;
private final OutputStream out;
private final NullSuppression nullSuppression; private final NullSuppression nullSuppression;
private final OutputGrouping outputGrouping; private final OutputGrouping outputGrouping;
private final Supplier<DateFormat> LAZY_DATE_FORMAT; private final Supplier<DateFormat> LAZY_DATE_FORMAT;
@ -69,7 +68,6 @@ public class WriteJsonResult extends AbstractRecordSetWriter implements RecordSe
this.logger = logger; this.logger = logger;
this.recordSchema = recordSchema; this.recordSchema = recordSchema;
this.schemaAccess = schemaAccess; this.schemaAccess = schemaAccess;
this.out = out;
this.nullSuppression = nullSuppression; this.nullSuppression = nullSuppression;
this.outputGrouping = outputGrouping; this.outputGrouping = outputGrouping;
@ -270,14 +268,19 @@ public class WriteJsonResult extends AbstractRecordSetWriter implements RecordSe
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void writeValue(final JsonGenerator generator, final Object value, final String fieldName, final DataType dataType) private void writeValue(final JsonGenerator generator, final Object value, final String fieldName, final DataType dataType) throws JsonGenerationException, IOException {
throws JsonGenerationException, IOException {
if (value == null) { if (value == null) {
generator.writeNull(); generator.writeNull();
return; return;
} }
final DataType chosenDataType = dataType.getFieldType() == RecordFieldType.CHOICE ? DataTypeUtils.chooseDataType(value, (ChoiceDataType) dataType) : dataType; 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); final Object coercedValue = DataTypeUtils.convertType(value, chosenDataType, LAZY_DATE_FORMAT, LAZY_TIME_FORMAT, LAZY_TIMESTAMP_FORMAT, fieldName);
if (coercedValue == null) { if (coercedValue == null) {
generator.writeNull(); generator.writeNull();