1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-10-18 12:15:58 +02:00

[xdata] Fix NPE in FillableForm

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 <dan@caseley.me.uk>
This commit is contained in:
Florian Schmaus 2024-01-10 10:41:22 +01:00
parent 282d63da36
commit 643d85c556

View file

@ -1,6 +1,6 @@
/** /**
* *
* Copyright 2020 Florian Schmaus * Copyright 2020-2024 Florian Schmaus
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -54,6 +54,7 @@ public class FillableForm extends FilledForm {
} }
Set<String> requiredFields = new HashSet<>(); Set<String> requiredFields = new HashSet<>();
List<FormField> requiredFieldsWithDefaultValue = new ArrayList<>();
for (FormField formField : dataForm.getFields()) { for (FormField formField : dataForm.getFields()) {
if (formField.isRequired()) { if (formField.isRequired()) {
String fieldName = formField.getFieldName(); String fieldName = formField.getFieldName();
@ -61,13 +62,17 @@ public class FillableForm extends FilledForm {
if (formField.hasValueSet()) { if (formField.hasValueSet()) {
// This is a form field with a default value. // This is a form field with a default value.
write(formField); requiredFieldsWithDefaultValue.add(formField);
} else { } else {
missingRequiredFields.add(fieldName); missingRequiredFields.add(fieldName);
} }
} }
} }
this.requiredFields = Collections.unmodifiableSet(requiredFields); this.requiredFields = Collections.unmodifiableSet(requiredFields);
for (FormField field : requiredFieldsWithDefaultValue) {
write(field);
}
} }
protected void writeListMulti(String fieldName, List<? extends CharSequence> values) { protected void writeListMulti(String fieldName, List<? extends CharSequence> values) {