From e94af49d6f73e79314964e797565e74c0e2d5c65 Mon Sep 17 00:00:00 2001 From: AgAngle <1323481023@qq.com> Date: Thu, 7 Mar 2024 18:01:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=A1=B9=E7=9B=AE=E8=AE=BE=E7=BD=AE):=20?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E8=AE=BE=E7=BD=AE=E7=9A=84=E5=A4=9A=E9=80=89?= =?UTF-8?q?=E6=A1=86=E9=BB=98=E8=AE=A4=E5=80=BC=E9=80=89=E9=A1=B9=E8=A2=AB?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=90=8E=EF=BC=8C=E5=88=9B=E5=BB=BA=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resolver/field/AbstractCustomFieldResolver.java | 2 +- .../service/BaseTemplateCustomFieldService.java | 11 ++++++++++- .../system/service/CreateTemplateResourceService.java | 5 +++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/resolver/field/AbstractCustomFieldResolver.java b/backend/services/system-setting/src/main/java/io/metersphere/system/resolver/field/AbstractCustomFieldResolver.java index 18bae124ec..e0e16d3438 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/resolver/field/AbstractCustomFieldResolver.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/resolver/field/AbstractCustomFieldResolver.java @@ -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) { diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTemplateCustomFieldService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTemplateCustomFieldService.java index 6ddbe23966..a9dfe09d3a 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTemplateCustomFieldService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTemplateCustomFieldService.java @@ -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 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()); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/CreateTemplateResourceService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/CreateTemplateResourceService.java index cc2e4d2d45..865c6d0544 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/CreateTemplateResourceService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/CreateTemplateResourceService.java @@ -110,6 +110,9 @@ public class CreateTemplateResourceService implements CreateProjectResourceServi Map customFieldMap = baseCustomFieldService.getByScopeIdAndScene(organizationId, scene.name()).stream() .collect(Collectors.toMap(CustomField::getId, Function.identity())); + // 忽略默认值校验,可能有多选框的选项被删除,造成不合法数据 + BaseTemplateCustomFieldService.validateDefaultValue.set(false); + orgTemplates.forEach((template) -> { List 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(); } /**