From d4d2a7a6d8a519cc51175320251c222ad5e27fa3 Mon Sep 17 00:00:00 2001 From: Frank Matheron Date: Tue, 23 Feb 2021 14:25:31 +0100 Subject: [PATCH] [xdata] Do not require FORM_TYPE field in FilledForm The FillableForm API is the most convenient way of filling out a form. Currently only forms with a FORM_TYPE can be filled, due to a restriction in FillableForm. This makes filling out untyped forms very cumbersome. This commit removes the required FORM_TYPE when using FilledForm (and therefor FillableForm). Some subclasses of FilledForm already check that the correct FORM_TYPE is set using FilledForm#ensureFormType(). --- .../smackx/xdata/form/FillableForm.java | 15 ++++++++++----- .../smackx/xdata/form/FilledForm.java | 5 ----- 2 files changed, 10 insertions(+), 10 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 b407864cc..7e90c7711 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 @@ -256,11 +256,16 @@ public class FillableForm extends FilledForm { if (!missingRequiredFields.isEmpty()) { throw new IllegalStateException("Not all required fields filled. Missing: " + missingRequiredFields); } - DataForm dataFormToSend = DataForm.builder() - .addField(formTypeFormField) - .addFields(filledFields.values()) - .build(); - return dataFormToSend; + DataForm.Builder builder = DataForm.builder(); + + // the submit form has the same FORM_TYPE as the form. + if (formTypeFormField != null) { + builder.addField(formTypeFormField); + } + + builder.addFields(filledFields.values()); + + return builder.build(); } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FilledForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FilledForm.java index 7624eab80..f81bb0c0e 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FilledForm.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/form/FilledForm.java @@ -17,7 +17,6 @@ package org.jivesoftware.smackx.xdata.form; import org.jivesoftware.smack.util.Objects; -import org.jivesoftware.smack.util.StringUtils; import org.jivesoftware.smackx.xdata.FormField; import org.jivesoftware.smackx.xdata.TextSingleFormField; @@ -32,10 +31,6 @@ public abstract class FilledForm implements FormReader { public FilledForm(DataForm dataForm) { this.dataForm = Objects.requireNonNull(dataForm); - String formType = dataForm.getFormType(); - if (StringUtils.isNullOrEmpty(formType)) { - throw new IllegalArgumentException("The provided data form has no hidden FROM_TYPE field."); - } if (dataForm.getType() == Type.cancel) { throw new IllegalArgumentException("Forms of type 'cancel' are not filled nor fillable"); }