From 33f59fd7edb89cddcbf9f4f691f462906106d967 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 2 Mar 2021 21:40:52 +0100 Subject: [PATCH] [xdata] Ensure that hidden FROM_TYPE field is first --- .../smackx/xdata/packet/DataForm.java | 27 +++++++++++++++++++ .../smackx/xdata/packet/DataFormTest.java | 10 +++++++ 2 files changed, 37 insertions(+) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java index 37a37741a..9ab354d57 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java @@ -100,6 +100,9 @@ public final class DataForm implements ExtensionElement { instructions = CollectionUtil.cloneAndSeal(builder.instructions); reportedData = builder.reportedData; items = CollectionUtil.cloneAndSeal(builder.items); + + builder.orderFields(); + fields = CollectionUtil.cloneAndSeal(builder.fields); fieldsMap = CollectionUtil.cloneAndSeal(builder.fieldsMap); extensionElements = CollectionUtil.cloneAndSeal(builder.extensionElements); @@ -382,6 +385,30 @@ public final class DataForm implements ExtensionElement { extensionElements = CollectionUtil.newListWith(dataForm.getExtensionElements()); } + private void orderFields() { + Iterator it = fields.iterator(); + if (!it.hasNext()) { + return; + } + + FormField hiddenFormTypeField = it.next().asHiddenFormTypeFieldIfPossible(); + if (hiddenFormTypeField != null) { + // The hidden FROM_TYPE field is already in first position, nothing to do. + return; + } + + while (it.hasNext()) { + hiddenFormTypeField = it.next().asHiddenFormTypeFieldIfPossible(); + if (hiddenFormTypeField != null) { + // Remove the hidden FORM_TYPE field that is not on first position. + it.remove(); + // And insert it again at first position. + fields.add(0, hiddenFormTypeField); + break; + } + } + } + /** * Deprecated do not use. * diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/packet/DataFormTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/packet/DataFormTest.java index 726be55d6..3f9e6feb3 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/packet/DataFormTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/packet/DataFormTest.java @@ -153,4 +153,14 @@ public class DataFormTest extends SmackTestSuite { DataForm df = pr.parse(PacketParserUtils.getParserFor(formWithFixedField)); assertEquals(Type.fixed, df.getFields().get(0).getType()); } + + @Test + public void testReorderHiddenFormTypeFieldAtFirstPosition() { + DataForm dataForm = DataForm.builder() + .addField(FormField.textSingleBuilder("foo1").setValue("bar").build()) + .addField(FormField.textSingleBuilder("foo2").setValue("baz").build()) + .setFormType("my-form-type") + .build(); + assertNotNull(dataForm.getFields().get(0).asHiddenFormTypeFieldIfPossible()); + } }