fix(项目设置): 模板设置的多选框默认值选项被删除后,创建项目报错

This commit is contained in:
AgAngle 2024-03-07 18:01:58 +08:00 committed by Craftsman
parent 858f9bd16d
commit e94af49d6f
3 changed files with 16 additions and 2 deletions

View File

@ -23,7 +23,7 @@ public abstract class AbstractCustomFieldResolver {
abstract public void validate(CustomFieldDao customField, Object value);
protected void throwValidateException(String name) {
throw new MSException(FIELD_VALIDATE_ERROR, Translator.get(FIELD_VALIDATE_ERROR.getMessage(), name));
throw new MSException(FIELD_VALIDATE_ERROR, Translator.getWithArgs(FIELD_VALIDATE_ERROR.getMessage(), name));
}
protected void validateRequired(CustomFieldDao customField, Object value) {

View File

@ -12,6 +12,7 @@ import io.metersphere.system.resolver.field.CustomFieldResolverFactory;
import io.metersphere.system.uid.IDGenerator;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -33,6 +34,11 @@ public class BaseTemplateCustomFieldService {
@Resource
private BaseCustomFieldService baseCustomFieldService;
/**
* 是否校验默认值
*/
public static final ThreadLocal<Boolean> validateDefaultValue = new ThreadLocal<>();
public void deleteByTemplateId(String templateId) {
TemplateCustomFieldExample example = new TemplateCustomFieldExample();
example.createCriteria()
@ -102,7 +108,10 @@ public class BaseTemplateCustomFieldService {
AbstractCustomFieldResolver customFieldResolver = CustomFieldResolverFactory.getResolver(customField.getType());
CustomFieldDao customFieldDao = BeanUtils.copyBean(new CustomFieldDao(), customField);
customFieldDao.setRequired(false);
customFieldResolver.validate(customFieldDao, field.getDefaultValue());
if (BooleanUtils.isNotFalse(validateDefaultValue.get())) {
// 创建项目时不校验默认值
customFieldResolver.validate(customFieldDao, field.getDefaultValue());
}
return customFieldResolver.parse2String(field.getDefaultValue());
}

View File

@ -110,6 +110,9 @@ public class CreateTemplateResourceService implements CreateProjectResourceServi
Map<String, CustomField> customFieldMap = baseCustomFieldService.getByScopeIdAndScene(organizationId, scene.name()).stream()
.collect(Collectors.toMap(CustomField::getId, Function.identity()));
// 忽略默认值校验可能有多选框的选项被删除造成不合法数据
BaseTemplateCustomFieldService.validateDefaultValue.set(false);
orgTemplates.forEach((template) -> {
List<TemplateCustomField> templateCustomFields = templateCustomFieldMap.get(template.getId());
templateCustomFields = templateCustomFields == null ? List.of() : templateCustomFields;
@ -124,6 +127,7 @@ public class CreateTemplateResourceService implements CreateProjectResourceServi
templateCustomFieldRequest.setDefaultValue(customFieldResolver.parse2Value(templateCustomField.getDefaultValue()));
}
} catch (Exception e) {
BaseTemplateCustomFieldService.validateDefaultValue.remove();
LogUtils.error(e);
templateCustomFieldRequest.setDefaultValue(null);
}
@ -132,6 +136,7 @@ public class CreateTemplateResourceService implements CreateProjectResourceServi
.toList();
addRefProjectTemplate(projectId, template, templateCustomFieldRequests, null);
});
BaseTemplateCustomFieldService.validateDefaultValue.remove();
}
/**