From 4d790aa7db42a44623b809091c9f6d00cb5d853a Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sun, 1 Sep 2024 21:52:25 +0200 Subject: [PATCH] [xdata] Only require list-multi and list-single fields to have a value Only list-multi and list-single fields require at least one value when submitting a form. In other cases, for example XEP-0045's muc#roomconfig_roomadmins, which is of type jid-multi, is used without any values to reset the room's admins list. Fixes SMACK-946. --- .../smackx/xdata/form/FillableForm.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FillableForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FillableForm.java index 332c7fc3ff..04cee26545 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FillableForm.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FillableForm.java @@ -30,6 +30,8 @@ import org.jivesoftware.smackx.xdata.AbstractSingleStringValueFormField; import org.jivesoftware.smackx.xdata.FormField; import org.jivesoftware.smackx.xdata.FormFieldChildElement; +import org.jivesoftware.smackx.xdata.ListMultiFormField; +import org.jivesoftware.smackx.xdata.ListSingleFormField; import org.jivesoftware.smackx.xdata.packet.DataForm; import org.jxmpp.jid.Jid; @@ -226,11 +228,20 @@ public final void write(FormField filledFormField) { if (filledFormField.getType() == FormField.Type.fixed) { throw new IllegalArgumentException(); } - if (!filledFormField.hasValueSet()) { - throw new IllegalArgumentException(); - } String fieldName = filledFormField.getFieldName(); + + boolean isListField = filledFormField instanceof ListMultiFormField + || filledFormField instanceof ListSingleFormField; + // Only list-* fields require a value to be set. Other fields types can be empty. For example MUC's + // muc#roomconfig_roomadmins, which is of type jid-multi, is submitted without values to reset the room's admin + // list. + if (isListField && !filledFormField.hasValueSet()) { + throw new IllegalArgumentException("Tried to write form field " + fieldName + " of type " + + filledFormField.getType() + + " without any values set. However, according to XEP-0045 ยง 3.3 fields of type list-multi or list-single must have one item set."); + } + if (!getDataForm().hasField(fieldName)) { throw new IllegalArgumentException(); }