Merge pull request #502 from abyss638/accept_empty_form_label_backport_from_master

Accept empty string as form field label value (backport to 4.4)
master
Florian Schmaus 12 months ago committed by GitHub
commit f03f2c75f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/FormField.java
  2. 16
      smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/FormFieldTest.java
  3. 29
      smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/provider/DataFormProviderTest.java

@ -33,6 +33,7 @@ import org.jivesoftware.smack.util.CollectionUtil;
import org.jivesoftware.smack.util.EqualsUtil;
import org.jivesoftware.smack.util.HashCode;
import org.jivesoftware.smack.util.MultiMap;
import org.jivesoftware.smack.util.Objects;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.XmlStringBuilder;
@ -600,7 +601,7 @@ public abstract class FormField implements FullyQualifiedElement {
* @return a reference to this builder.
*/
public B setLabel(String label) {
this.label = StringUtils.requireNotNullNorEmpty(label, "label must not be null or empty");
this.label = Objects.requireNonNull(label, "label must not be null");
return getThis();
}

@ -17,6 +17,8 @@
package org.jivesoftware.smackx.xdata;
import static org.jivesoftware.smack.test.util.XmlAssertUtil.assertXmlSimilar;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import org.jxmpp.jid.JidTestUtil;
@ -35,4 +37,18 @@ class FormFieldTest {
assertXmlSimilar(expectedXml, xml);
}
@Test
public void testEmptyLabel() {
TextSingleFormField.Builder builder = FormField.textSingleBuilder("type");
builder.setLabel("");
TextSingleFormField formField = builder.build();
assertEquals("", formField.getLabel());
}
@Test
public void testThrowExceptionWhenNullLabel() {
TextSingleFormField.Builder builder = FormField.textSingleBuilder("type");
assertThrows(IllegalArgumentException.class, () -> builder.setLabel(null));
}
}

@ -114,4 +114,33 @@ public class DataFormProviderTest {
assertEquals(2, items.size());
}
@Test
public void testRetrieveFieldWithEmptyLabel() throws XmlPullParserException, IOException, SmackParsingException {
String form =
"<x xmlns='jabber:x:data' type='form'>" +
" <title>Advanced User Search</title>" +
" <instructions>The following fields are available for searching. Wildcard (*) characters are allowed as part of the query.</instructions>" +
" <field var='FORM_TYPE' label='' type='hidden'>" +
" <value>jabber:iq:search</value>" +
" </field>" +
" <field label='Search' var='search'>" +
" <required/>" +
" </field>" +
" <field label='Username' var='Username' type='boolean'>" +
" <value>true</value>" +
" </field>" +
" <field label='Name' var='Name' type='boolean'>" +
" <value>true</value>" +
" </field>" +
" <field label='Email' var='Email' type='boolean'>" +
" <value>true</value>" +
" </field>" +
"</x>";
XmlPullParser parser = PacketParserUtils.getParserFor(form);
DataForm dataForm = DataFormProvider.INSTANCE.parse(parser);
FormField usernameFormField = dataForm.getField("FORM_TYPE");
assertEquals(FormField.Type.hidden, usernameFormField.getType());
assertEquals("", usernameFormField.getLabel());
}
}

Loading…
Cancel
Save