diff --git a/backend/src/main/java/io/metersphere/excel/domain/TestCaseExcelData.java b/backend/src/main/java/io/metersphere/excel/domain/TestCaseExcelData.java index c67ac5e8e1..d441e5deda 100644 --- a/backend/src/main/java/io/metersphere/excel/domain/TestCaseExcelData.java +++ b/backend/src/main/java/io/metersphere/excel/domain/TestCaseExcelData.java @@ -48,7 +48,7 @@ public class TestCaseExcelData { @ExcelIgnore private String priority; @ExcelIgnore - Map customData = new LinkedHashMap<>(); + Map customData = new LinkedHashMap<>(); @ExcelIgnore List mergeStepDesc; diff --git a/backend/src/main/java/io/metersphere/excel/listener/TestCaseNoModelDataListener.java b/backend/src/main/java/io/metersphere/excel/listener/TestCaseNoModelDataListener.java index 82b8341eca..a07f5079d1 100644 --- a/backend/src/main/java/io/metersphere/excel/listener/TestCaseNoModelDataListener.java +++ b/backend/src/main/java/io/metersphere/excel/listener/TestCaseNoModelDataListener.java @@ -388,29 +388,29 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener customData = data.getCustomData(); + Map customData = data.getCustomData(); for (String fieldName : customData.keySet()) { - String value = customData.get(fieldName); + Object value = customData.get(fieldName); CustomFieldDao customField = customFieldsMap.get(fieldName); if (customField == null) { continue; } AbstractCustomFieldValidator customFieldValidator = customFieldValidatorMap.get(customField.getType()); try { - customFieldValidator.validate(customField, value); + customFieldValidator.validate(customField, value.toString()); + if (customFieldValidator.isKVOption) { + // 这里如果填的是选项值,替换成选项ID,保存 + customData.put(fieldName, customFieldValidator.parse2Key(value.toString(), customField)); + } } catch (CustomFieldValidateException e) { stringBuilder.append(e.getMessage().concat(ERROR_MSG_SEPARATOR)); } - if (customFieldValidator.isKVOption) { - // 这里如果填的是选项值,替换成选项ID,保存 - customData.put(fieldName, customFieldValidator.parse2Key(value, customField)); - } if (StringUtils.equals(fieldName, TestCaseImportFiled.STATUS.getFiledLangMap().get(Locale.SIMPLIFIED_CHINESE))) { - data.setStatus(customData.get(fieldName)); + data.setStatus(customData.get(fieldName).toString()); } else if (StringUtils.equals(fieldName, TestCaseImportFiled.PRIORITY.getFiledLangMap().get(Locale.SIMPLIFIED_CHINESE))) { - data.setPriority(customData.get(fieldName)); + data.setPriority(customData.get(fieldName).toString()); } else if (StringUtils.equals(fieldName, TestCaseImportFiled.MAINTAINER.getFiledLangMap().get(Locale.SIMPLIFIED_CHINESE))) { - data.setMaintainer(customData.get(fieldName)); + data.setMaintainer(customData.get(fieldName).toString()); } } } @@ -545,10 +545,11 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener customData = data.getCustomData(); + Map customData = data.getCustomData(); List testCaseCustomFields = new ArrayList<>(); customData.forEach((k, v) -> { - if (StringUtils.isNotBlank(v)) { + if ((v instanceof List && CollectionUtils.isNotEmpty((List)v)) + || StringUtils.isNotBlank(v.toString())) { CustomFieldDao customFieldDao = customFieldsMap.get(k); if (customFieldDao != null) { CustomFieldResource customFieldResource = new CustomFieldResource(); diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java index 7ba8075e6c..e17ac4c887 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java @@ -1494,7 +1494,7 @@ public class TestCaseService { for (TestCaseExcelData model : data) { List fields = new ArrayList<>(); - Map customDataMaps = Optional.ofNullable(model.getCustomData()) + Map customDataMaps = Optional.ofNullable(model.getCustomData()) .orElse(new HashMap<>()); Map otherFieldMaps = Optional.ofNullable(model.getOtherFields()) .orElse(new HashMap<>()); @@ -1507,7 +1507,7 @@ public class TestCaseService { } } if (!isSystemField) { - String value = customDataMaps.get(head); + Object value = customDataMaps.get(head); if (value == null) { value = otherFieldMaps.get(head); } @@ -1646,7 +1646,7 @@ public class TestCaseService { Map customNameMap, TestCaseDTO t, TestCaseExcelData data, Set textFields) { try { List fields = customFieldTestCaseService.getByResourceId(t.getId()); - Map map = new HashMap<>(); + Map map = new HashMap<>(); for (int index = 0; index < fields.size(); index++) { CustomFieldResource field = fields.get(index); //进行key value对换 diff --git a/backend/src/main/java/io/metersphere/track/validate/AbstractCustomFieldValidator.java b/backend/src/main/java/io/metersphere/track/validate/AbstractCustomFieldValidator.java index 19e6702982..2bd98c720f 100644 --- a/backend/src/main/java/io/metersphere/track/validate/AbstractCustomFieldValidator.java +++ b/backend/src/main/java/io/metersphere/track/validate/AbstractCustomFieldValidator.java @@ -2,6 +2,7 @@ package io.metersphere.track.validate; import com.alibaba.fastjson.JSONArray; import io.metersphere.commons.exception.CustomFieldValidateException; +import io.metersphere.commons.utils.LogUtil; import io.metersphere.dto.CustomFieldDao; import io.metersphere.i18n.Translator; import org.apache.commons.lang3.StringUtils; @@ -19,6 +20,7 @@ public abstract class AbstractCustomFieldValidator { /** * 校验参数是否合法 + * * @param customField * @param value */ @@ -26,10 +28,11 @@ public abstract class AbstractCustomFieldValidator { /** * 将选项的值转化为对应的key + * * @param keyOrValue * @return */ - public String parse2Key(String keyOrValue, CustomFieldDao customField) { + public Object parse2Key(String keyOrValue, CustomFieldDao customField) { return keyOrValue; } @@ -39,12 +42,35 @@ public abstract class AbstractCustomFieldValidator { } } + protected void validateArrayRequired(CustomFieldDao customField, String value) throws CustomFieldValidateException { + if (customField.getRequired() && (StringUtils.isBlank(value) || StringUtils.equals(value, "[]"))) { + CustomFieldValidateException.throwException(String.format(Translator.get("custom_field_required_tip"), customField.getName())); + } + } + protected List parse2Array(String name, String value) throws CustomFieldValidateException { try { + // [a, b] => ["a","b"] + if (!StringUtils.equals(value, "[]")) { + value = value.replace("[", "[\"") + .replace("]", "\"]") + .replace(",", "\",\"") + .replace(",", "\",\"") + .replace(" ", ""); + } return JSONArray.parseArray(value, String.class); } catch (Exception e) { CustomFieldValidateException.throwException(String.format(Translator.get("custom_field_required_tip"), name)); } return new ArrayList<>(); } + + protected List parse2Array(String value) { + try { + return parse2Array(null, value); + } catch (CustomFieldValidateException e) { + LogUtil.error(e); + } + return new ArrayList<>(); + } } diff --git a/backend/src/main/java/io/metersphere/track/validate/CustomFieldMemberValidator.java b/backend/src/main/java/io/metersphere/track/validate/CustomFieldMemberValidator.java index d1e91201b6..3d1cef5bb5 100644 --- a/backend/src/main/java/io/metersphere/track/validate/CustomFieldMemberValidator.java +++ b/backend/src/main/java/io/metersphere/track/validate/CustomFieldMemberValidator.java @@ -40,7 +40,7 @@ public class CustomFieldMemberValidator extends AbstractCustomFieldValidator { } @Override - public String parse2Key(String keyOrValue, CustomFieldDao customField) { + public Object parse2Key(String keyOrValue, CustomFieldDao customField) { if (userNameMap.containsKey(keyOrValue)) { return userNameMap.get(keyOrValue); } diff --git a/backend/src/main/java/io/metersphere/track/validate/CustomFieldMultipleMemberValidator.java b/backend/src/main/java/io/metersphere/track/validate/CustomFieldMultipleMemberValidator.java index 9b06e34240..0cefe22cac 100644 --- a/backend/src/main/java/io/metersphere/track/validate/CustomFieldMultipleMemberValidator.java +++ b/backend/src/main/java/io/metersphere/track/validate/CustomFieldMultipleMemberValidator.java @@ -12,7 +12,7 @@ public class CustomFieldMultipleMemberValidator extends CustomFieldMemberValidat @Override public void validate(CustomFieldDao customField, String value) throws CustomFieldValidateException { - validateRequired(customField, value); + validateArrayRequired(customField, value); if (StringUtils.isBlank(value)) { return; } @@ -27,11 +27,11 @@ public class CustomFieldMultipleMemberValidator extends CustomFieldMemberValidat } @Override - public String parse2Key(String keyOrValuesStr, CustomFieldDao customField) { + public Object parse2Key(String keyOrValuesStr, CustomFieldDao customField) { if (StringUtils.isBlank(keyOrValuesStr)) { return ""; } - List keyOrValues = JSONArray.parseArray(keyOrValuesStr, String.class); + List keyOrValues = parse2Array(keyOrValuesStr); for (int i = 0; i < keyOrValues.size(); i++) { String item = keyOrValues.get(i); @@ -39,6 +39,6 @@ public class CustomFieldMultipleMemberValidator extends CustomFieldMemberValidat keyOrValues.set(i, userNameMap.get(item)); } } - return JSONArray.toJSONString(keyOrValues); + return keyOrValues; } } diff --git a/backend/src/main/java/io/metersphere/track/validate/CustomFieldMultipleSelectValidator.java b/backend/src/main/java/io/metersphere/track/validate/CustomFieldMultipleSelectValidator.java index 5a04340f94..6bf83534a2 100644 --- a/backend/src/main/java/io/metersphere/track/validate/CustomFieldMultipleSelectValidator.java +++ b/backend/src/main/java/io/metersphere/track/validate/CustomFieldMultipleSelectValidator.java @@ -14,7 +14,7 @@ public class CustomFieldMultipleSelectValidator extends CustomFieldSelectValidat @Override public void validate(CustomFieldDao customField, String value) throws CustomFieldValidateException { - validateRequired(customField, value); + validateArrayRequired(customField, value); if (StringUtils.isBlank(value)) { return; } @@ -29,11 +29,11 @@ public class CustomFieldMultipleSelectValidator extends CustomFieldSelectValidat } @Override - public String parse2Key(String keyOrValuesStr, CustomFieldDao customField) { + public Object parse2Key(String keyOrValuesStr, CustomFieldDao customField) { if (StringUtils.isBlank(keyOrValuesStr)) { return ""; } - List keyOrValues = JSONArray.parseArray(keyOrValuesStr, String.class); + List keyOrValues = parse2Array(keyOrValuesStr); Map nameMap = optionTextMapCache.get(customField.getId()); for (int i = 0; i < keyOrValues.size(); i++) { String item = keyOrValues.get(i); @@ -41,6 +41,6 @@ public class CustomFieldMultipleSelectValidator extends CustomFieldSelectValidat keyOrValues.set(i, nameMap.get(item)); } } - return JSONArray.toJSONString(keyOrValues); + return keyOrValues; } } diff --git a/backend/src/main/java/io/metersphere/track/validate/CustomFieldSelectValidator.java b/backend/src/main/java/io/metersphere/track/validate/CustomFieldSelectValidator.java index 826aa6fdfa..7be848b837 100644 --- a/backend/src/main/java/io/metersphere/track/validate/CustomFieldSelectValidator.java +++ b/backend/src/main/java/io/metersphere/track/validate/CustomFieldSelectValidator.java @@ -54,7 +54,7 @@ public class CustomFieldSelectValidator extends AbstractCustomFieldValidator { } @Override - public String parse2Key(String keyOrValuesStr, CustomFieldDao customField) { + public Object parse2Key(String keyOrValuesStr, CustomFieldDao customField) { Map textMap = optionTextMapCache.get(customField.getId()); if (MapUtils.isNotEmpty(textMap) && textMap.containsKey(keyOrValuesStr)) { return textMap.get(keyOrValuesStr); diff --git a/frontend/src/business/components/track/case/components/TestCaseList.vue b/frontend/src/business/components/track/case/components/TestCaseList.vue index c1581afc5f..630d8c58ee 100644 --- a/frontend/src/business/components/track/case/components/TestCaseList.vue +++ b/frontend/src/business/components/track/case/components/TestCaseList.vue @@ -718,10 +718,9 @@ export default { }, getCustomFieldFilter(field) { if (field.name === '用例状态') { - let option = []; - if (this.trashEnable) { - option.push({text: this.$t('test_track.plan.plan_status_trash'), value: 'Trash'}); - } else { + let option = null; + if (!this.trashEnable) { + option = []; field.options.forEach((item) => { option.push({ text: this.$t(item.text),