Skip to content

Commit

Permalink
[xdata] Fix NPE in FillableForm
Browse files Browse the repository at this point in the history
Calling write() in FillableForm's constructor causes a NPE because
write() makes use of requiredFields which has not been set at this
time. Furthermore, write() makes use of missingRequiredFields, which
is also populated in that loop. Therefore, we have to delay the
invocation of write() until requiredFields got set.

Thanks to Dan Caseley for reporting this.

Reported-by: Dan Caseley <[email protected]>
  • Loading branch information
Flowdalic committed Jan 10, 2024
1 parent 282d63d commit 643d85c
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* Copyright 2020 Florian Schmaus
* Copyright 2020-2024 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,20 +54,25 @@ public FillableForm(DataForm dataForm) {
}

Set<String> requiredFields = new HashSet<>();
List<FormField> requiredFieldsWithDefaultValue = new ArrayList<>();
for (FormField formField : dataForm.getFields()) {
if (formField.isRequired()) {
String fieldName = formField.getFieldName();
requiredFields.add(fieldName);

if (formField.hasValueSet()) {
// This is a form field with a default value.
write(formField);
requiredFieldsWithDefaultValue.add(formField);
} else {
missingRequiredFields.add(fieldName);
}
}
}
this.requiredFields = Collections.unmodifiableSet(requiredFields);

for (FormField field : requiredFieldsWithDefaultValue) {
write(field);
}
}

protected void writeListMulti(String fieldName, List<? extends CharSequence> values) {
Expand Down

0 comments on commit 643d85c

Please sign in to comment.