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:
parent
5bdfe7de0e
commit
9e1303905e
|
|
@ -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) {
|
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
|
// In case the formData has not been initialized before the fxn call, assign a new HashMap to the variable
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,9 @@ import com.appsmith.external.constants.ConditionalOperator;
|
||||||
import com.appsmith.external.models.Condition;
|
import com.appsmith.external.models.Condition;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
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 com.appsmith.external.helpers.PluginUtils.parseWhereClause;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
public class PluginUtilsTest {
|
public class PluginUtilsTest {
|
||||||
|
|
||||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
@ -174,6 +179,28 @@ public class PluginUtilsTest {
|
||||||
assertEquals(Map.of("k", "value"), data);
|
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
|
@Test
|
||||||
public void testSetDataValueSafelyInFormData_withNestedPath_createsInnermostDataKey() {
|
public void testSetDataValueSafelyInFormData_withNestedPath_createsInnermostDataKey() {
|
||||||
final Map<String, Object> dataMap = new HashMap<>();
|
final Map<String, Object> dataMap = new HashMap<>();
|
||||||
|
|
@ -182,4 +209,5 @@ public class PluginUtilsTest {
|
||||||
|
|
||||||
assertEquals(Map.of("key", Map.of("innerKey", Map.of("data", "value"))), dataMap);
|
assertEquals(Map.of("key", Map.of("innerKey", Map.of("data", "value"))), dataMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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.STRING_TYPE;
|
||||||
import static com.appsmith.external.helpers.PluginUtils.getDataValueSafelyFromFormData;
|
import static com.appsmith.external.helpers.PluginUtils.getDataValueSafelyFromFormData;
|
||||||
import static com.appsmith.external.helpers.PluginUtils.getTrimmedStringDataValueSafelyFromFormData;
|
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.parseWhereClause;
|
||||||
import static com.appsmith.external.helpers.PluginUtils.validDataConfigurationPresentInFormData;
|
import static com.appsmith.external.helpers.PluginUtils.validDataConfigurationPresentInFormData;
|
||||||
import static com.external.constants.FieldName.SHEET_NAME;
|
import static com.external.constants.FieldName.SHEET_NAME;
|
||||||
|
|
@ -107,13 +107,13 @@ public class MethodConfig {
|
||||||
switch (parameters.size()) {
|
switch (parameters.size()) {
|
||||||
case 3:
|
case 3:
|
||||||
case 2:
|
case 2:
|
||||||
this.tableHeaderIndex = (String) getValueSafelyFromFormData(parameters, TABLE_HEADER_INDEX);
|
this.tableHeaderIndex = getValueSafelyFromFormDataAsString(parameters, TABLE_HEADER_INDEX);
|
||||||
if (!StringUtils.hasLength(this.tableHeaderIndex)) {
|
if (!StringUtils.hasLength(this.tableHeaderIndex)) {
|
||||||
this.tableHeaderIndex = "1";
|
this.tableHeaderIndex = "1";
|
||||||
}
|
}
|
||||||
this.sheetName = (String) getValueSafelyFromFormData(parameters, SHEET_NAME);
|
this.sheetName = getValueSafelyFromFormDataAsString(parameters, SHEET_NAME);
|
||||||
case 1:
|
case 1:
|
||||||
this.spreadsheetUrl = (String) getValueSafelyFromFormData(parameters, SHEET_URL);
|
this.spreadsheetUrl = getValueSafelyFromFormDataAsString(parameters, SHEET_URL);
|
||||||
setSpreadsheetUrlFromSpreadsheetId();
|
setSpreadsheetUrlFromSpreadsheetId();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user