fix: ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String (#17707)

* get a string casted value safely from form data

* letting warning be and removing unwanted line
This commit is contained in:
amogh2019 2022-10-25 18:26:35 +05:30 committed by GitHub
parent 5bdfe7de0e
commit 9e1303905e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 4 deletions

View File

@ -224,6 +224,10 @@ public class PluginUtils {
}
public static String getValueSafelyFromFormDataAsString(Map<String, Object> formData, String field) {
return String.valueOf(getValueSafelyFromFormData(formData, field));
}
public static void setDataValueSafelyInFormData(Map<String, Object> formData, String field, Object value) {
// In case the formData has not been initialized before the fxn call, assign a new HashMap to the variable

View File

@ -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<String, Object> 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<String, Object> 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);
}
}

View File

@ -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();
}
}