diff --git a/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/helpers/PluginUtils.java b/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/helpers/PluginUtils.java index 81159b627c..237a71bd84 100644 --- a/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/helpers/PluginUtils.java +++ b/app/server/appsmith-interfaces/src/main/java/com/appsmith/external/helpers/PluginUtils.java @@ -224,6 +224,10 @@ public class PluginUtils { } + public static String getValueSafelyFromFormDataAsString(Map formData, String field) { + return String.valueOf(getValueSafelyFromFormData(formData, field)); + } + public static void setDataValueSafelyInFormData(Map formData, String field, Object value) { // In case the formData has not been initialized before the fxn call, assign a new HashMap to the variable diff --git a/app/server/appsmith-interfaces/src/test/java/com/appsmith/external/helpers/PluginUtilsTest.java b/app/server/appsmith-interfaces/src/test/java/com/appsmith/external/helpers/PluginUtilsTest.java index 140ec86ec6..5d9f8b6f47 100644 --- a/app/server/appsmith-interfaces/src/test/java/com/appsmith/external/helpers/PluginUtilsTest.java +++ b/app/server/appsmith-interfaces/src/test/java/com/appsmith/external/helpers/PluginUtilsTest.java @@ -4,6 +4,9 @@ import com.appsmith.external.constants.ConditionalOperator; import com.appsmith.external.models.Condition; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; + +import lombok.extern.slf4j.Slf4j; + import org.junit.jupiter.api.Test; import java.io.IOException; @@ -16,8 +19,10 @@ import static com.appsmith.external.helpers.PluginUtils.STRING_TYPE; import static com.appsmith.external.helpers.PluginUtils.parseWhereClause; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +@Slf4j public class PluginUtilsTest { private final ObjectMapper objectMapper = new ObjectMapper(); @@ -173,6 +178,28 @@ public class PluginUtilsTest { assertEquals(Map.of("k", "value"), data); } + + + @Test + public void testGetValueSafelyInFormData_IncorrectParsingByCaller() { + final Map dataMap = Map.of("k", 1); + + final Object data = PluginUtils.getValueSafelyFromFormData(dataMap, "k"); + + assertThrows(ClassCastException.class, () -> { + String result = (String) data; + }); + } + + @Test + public void testGetValueSafelyInFormDataAsString() { + final Map dataMap = Map.of("k", 1); + + final Object data = PluginUtils.getValueSafelyFromFormDataAsString(dataMap, "k"); + + String result = (String) data; + assertTrue(result instanceof String); + } @Test public void testSetDataValueSafelyInFormData_withNestedPath_createsInnermostDataKey() { @@ -182,4 +209,5 @@ public class PluginUtilsTest { assertEquals(Map.of("key", Map.of("innerKey", Map.of("data", "value"))), dataMap); } + } diff --git a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/MethodConfig.java b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/MethodConfig.java index 36f13e178a..71083c3f03 100644 --- a/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/MethodConfig.java +++ b/app/server/appsmith-plugins/googleSheetsPlugin/src/main/java/com/external/config/MethodConfig.java @@ -22,7 +22,7 @@ import java.util.regex.Pattern; import static com.appsmith.external.helpers.PluginUtils.STRING_TYPE; import static com.appsmith.external.helpers.PluginUtils.getDataValueSafelyFromFormData; import static com.appsmith.external.helpers.PluginUtils.getTrimmedStringDataValueSafelyFromFormData; -import static com.appsmith.external.helpers.PluginUtils.getValueSafelyFromFormData; +import static com.appsmith.external.helpers.PluginUtils.getValueSafelyFromFormDataAsString; import static com.appsmith.external.helpers.PluginUtils.parseWhereClause; import static com.appsmith.external.helpers.PluginUtils.validDataConfigurationPresentInFormData; import static com.external.constants.FieldName.SHEET_NAME; @@ -107,13 +107,13 @@ public class MethodConfig { switch (parameters.size()) { case 3: case 2: - this.tableHeaderIndex = (String) getValueSafelyFromFormData(parameters, TABLE_HEADER_INDEX); + this.tableHeaderIndex = getValueSafelyFromFormDataAsString(parameters, TABLE_HEADER_INDEX); if (!StringUtils.hasLength(this.tableHeaderIndex)) { this.tableHeaderIndex = "1"; } - this.sheetName = (String) getValueSafelyFromFormData(parameters, SHEET_NAME); + this.sheetName = getValueSafelyFromFormDataAsString(parameters, SHEET_NAME); case 1: - this.spreadsheetUrl = (String) getValueSafelyFromFormData(parameters, SHEET_URL); + this.spreadsheetUrl = getValueSafelyFromFormDataAsString(parameters, SHEET_URL); setSpreadsheetUrlFromSpreadsheetId(); } }