Improve the field value parsing for choice widgets to handle null values

The specification states that the field value is `null` if no item is
selected and we didn't handle this case properly. Even though this did
not break the rendering because we always convert the value to an array
and the `includes` check in the display layer would simply not match,
the field value would be `[null]` which is not expected and strange from
an API perspective.

This commit fixes that by ensuring that we return an empty array in
case the field value is `null`. The API therefore still always gives an
array for the field value, but now the code is more specific so that the
value is either an empty array or an array of strings.
This commit is contained in:
Tim van der Meij 2020-08-18 23:04:56 +02:00
parent 3ca037af78
commit 12c20772ac
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762
2 changed files with 9 additions and 6 deletions

View File

@ -1610,11 +1610,14 @@ class ChoiceWidgetAnnotation extends WidgetAnnotation {
}
}
// Determine the field value. In this case, it may be a string or an
// array of strings. For convenience in the display layer, convert the
// string to an array of one string as well.
if (!Array.isArray(this.data.fieldValue)) {
// The field value can be `null` if no item is selected, a string if one
// item is selected or an array of strings if multiple items are selected.
// For consistency in the API and convenience in the display layer, we
// always make the field value an array with zero, one or multiple items.
if (isString(this.data.fieldValue)) {
this.data.fieldValue = [this.data.fieldValue];
} else if (!this.data.fieldValue) {
this.data.fieldValue = [];
}
// Process field flags for the display layer.

View File

@ -2479,8 +2479,8 @@ describe("annotation", function () {
});
it("should convert the field value to an array", function (done) {
const inputs = ["Foo", ["Foo", "Bar"]];
const outputs = [["Foo"], ["Foo", "Bar"]];
const inputs = [null, "Foo", ["Foo", "Bar"]];
const outputs = [[], ["Foo"], ["Foo", "Bar"]];
let promise = Promise.resolve();
for (let i = 0, ii = inputs.length; i < ii; i++) {