From bf7944fa9a65d3261fdeb471049fe23e2822c1b0 Mon Sep 17 00:00:00 2001 From: dialYun <1552333077@qq.com> Date: Sat, 27 Jul 2024 02:45:45 +0000 Subject: [PATCH] =?UTF-8?q?meta=E5=B1=82=E5=AD=97=E6=AE=B5=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E5=80=BC=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dialYun <1552333077@qq.com> --- .../testSet/autotest.yaml | 0 .../testSet/input/request.json5 | 36 +++++++++++++ .../testSet/output/response.json5 | 15 ++++++ .../service/TestXMetaPropDefaultValue.java | 46 ++++++++++++++++ .../nop/biz/crud/ObjMetaBasedValidator.java | 52 +++++++++++++++++-- 5 files changed, 145 insertions(+), 4 deletions(-) create mode 100644 nop-auth/nop-auth-service/cases/io/nop/auth/service/TestXMetaPropDefaultValue/testSet/autotest.yaml create mode 100644 nop-auth/nop-auth-service/cases/io/nop/auth/service/TestXMetaPropDefaultValue/testSet/input/request.json5 create mode 100644 nop-auth/nop-auth-service/cases/io/nop/auth/service/TestXMetaPropDefaultValue/testSet/output/response.json5 create mode 100644 nop-auth/nop-auth-service/src/test/java/io/nop/auth/service/TestXMetaPropDefaultValue.java diff --git a/nop-auth/nop-auth-service/cases/io/nop/auth/service/TestXMetaPropDefaultValue/testSet/autotest.yaml b/nop-auth/nop-auth-service/cases/io/nop/auth/service/TestXMetaPropDefaultValue/testSet/autotest.yaml new file mode 100644 index 000000000..e69de29bb diff --git a/nop-auth/nop-auth-service/cases/io/nop/auth/service/TestXMetaPropDefaultValue/testSet/input/request.json5 b/nop-auth/nop-auth-service/cases/io/nop/auth/service/TestXMetaPropDefaultValue/testSet/input/request.json5 new file mode 100644 index 000000000..69bdf63e2 --- /dev/null +++ b/nop-auth/nop-auth-service/cases/io/nop/auth/service/TestXMetaPropDefaultValue/testSet/input/request.json5 @@ -0,0 +1,36 @@ +{ + selection: { + fields: { + userName: null, + phone: null, + dept: { + fields: { + deptName: null, + email: null, + } + }, + position: { + fields: { + name: null, + remark: null, + } + } + } + }, + data: { + data: { + userName: "auto_test1", + password: "auto_test1", + nickName: "autoTestNick", + gender: "1", + userType: "1", + 'dept': { + deptType: "一", + deptName: "科技部", + }, + 'position': { + name: "测试职位", + } + } + } +} \ No newline at end of file diff --git a/nop-auth/nop-auth-service/cases/io/nop/auth/service/TestXMetaPropDefaultValue/testSet/output/response.json5 b/nop-auth/nop-auth-service/cases/io/nop/auth/service/TestXMetaPropDefaultValue/testSet/output/response.json5 new file mode 100644 index 000000000..7acf15c16 --- /dev/null +++ b/nop-auth/nop-auth-service/cases/io/nop/auth/service/TestXMetaPropDefaultValue/testSet/output/response.json5 @@ -0,0 +1,15 @@ +{ + data: { + userName: "auto_test1", + phone: "132", + 'dept': { + deptName: "科技部", + email: "123", + }, + 'position': { + name: "测试职位", + remark: "AAAAAA", + } + }, + "status": 0 +} \ No newline at end of file diff --git a/nop-auth/nop-auth-service/src/test/java/io/nop/auth/service/TestXMetaPropDefaultValue.java b/nop-auth/nop-auth-service/src/test/java/io/nop/auth/service/TestXMetaPropDefaultValue.java new file mode 100644 index 000000000..e54ef8cae --- /dev/null +++ b/nop-auth/nop-auth-service/src/test/java/io/nop/auth/service/TestXMetaPropDefaultValue.java @@ -0,0 +1,46 @@ +package io.nop.auth.service; + + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import io.nop.api.core.annotations.autotest.EnableSnapshot; +import io.nop.api.core.annotations.autotest.NopTestConfig; +import io.nop.api.core.beans.ApiRequest; +import io.nop.api.core.util.FutureHelper; +import io.nop.auth.service.audit.AuditServiceImpl; +import io.nop.autotest.junit.JunitAutoTestCase; +import io.nop.graphql.core.IGraphQLExecutionContext; +import io.nop.graphql.core.ast.GraphQLOperationType; +import io.nop.graphql.core.engine.IGraphQLEngine; +import jakarta.inject.Inject; +import org.junit.jupiter.api.Test; + +@Disabled +@NopTestConfig(initDatabaseSchema = true, localDb = true, disableSnapshot = false) +public class TestXMetaPropDefaultValue extends JunitAutoTestCase { + + @Inject + IGraphQLEngine graphQLEngine; + + @Inject + AuditServiceImpl auditService; + + + @EnableSnapshot + @Test + public void testSet() { + + ApiRequest request = input("request.json5", ApiRequest.class); + IGraphQLExecutionContext context = graphQLEngine.newRpcContext(GraphQLOperationType.mutation, "NopAuthUser__save", + request); + Object result = FutureHelper.syncGet(graphQLEngine.executeRpcAsync(context)); + output("response.json5", result); + assertTrue(FutureHelper.waitUntil(() -> auditService.isAllProcessed(), 1000)); + + try { + Thread.sleep(200); + } catch (InterruptedException e) { + Thread.interrupted(); + } + } +} diff --git a/nop-biz/src/main/java/io/nop/biz/crud/ObjMetaBasedValidator.java b/nop-biz/src/main/java/io/nop/biz/crud/ObjMetaBasedValidator.java index fd60f5844..6c80ea61e 100644 --- a/nop-biz/src/main/java/io/nop/biz/crud/ObjMetaBasedValidator.java +++ b/nop-biz/src/main/java/io/nop/biz/crud/ObjMetaBasedValidator.java @@ -32,6 +32,7 @@ import io.nop.core.dict.DictProvider; import io.nop.core.lang.eval.IEvalAction; import io.nop.core.lang.eval.IEvalScope; import io.nop.core.lang.json.JsonTool; +import io.nop.core.lang.json.delta.JsonMerger; import io.nop.dao.DaoConstants; import io.nop.graphql.core.GraphQLConstants; import io.nop.orm.OrmConstants; @@ -106,10 +107,54 @@ public class ObjMetaBasedValidator { scope.setLocalValue(null, BizConstants.VAR_ROOT, data); Map map = _validate(bizObjName, objMeta.getRootSchema(), null, data, selection, filter, false, scope); + Map defaultValueMap = convertPropDefaultValue(null, objMeta.getRootSchema(), map); + map = new JsonMerger().mergeMap(map, defaultValueMap); appendEqCondition(map); return map; } + + private Map convertPropDefaultValue(String bizObjName, ISchema schema, Map data) { + + Map ret = new LinkedHashMap<>(); + for (Map.Entry entry : data.entrySet()) { + String name = entry.getKey(); + Object value = entry.getValue(); + IObjPropMeta propMeta = schema.getProp(name); + + if (value instanceof Collection) { + ISchema propSchema = getPropSchema(propMeta, true, bizObjectManager, bizObjName); + List list = CollectionHelper.toList(value); + for (Object item : list) { + Map itemMap = (Map) item; + ret.put(name, getPropDefaultValue(propSchema, itemMap)); + convertPropDefaultValue(propSchema.getBizObjName(), propSchema, itemMap); + } + } else if (value instanceof Map) { + ISchema propSchema = getPropSchema(propMeta, false, bizObjectManager, bizObjName); + Map itemMap = (Map) value; + ret.put(name, getPropDefaultValue(propSchema, itemMap)); + convertPropDefaultValue(propSchema.getBizObjName(), propSchema, itemMap); + } else if(bizObjName == null && ret.isEmpty()) { + Map map = this.getPropDefaultValue(schema, data); + ret.putAll(map); + } + } + return ret; + } + + public Map getPropDefaultValue(ISchema propSchema, Map itemMap) { + Map valueMap = new LinkedHashMap<>(); + propSchema.getProps().forEach(prop -> { + if (itemMap.containsKey(prop.getName()) + || StringHelper.isEmptyObject(prop.getDefaultValue())) { + return; + } + valueMap.put(prop.getName(), prop.getDefaultValue()); + }); + return valueMap; + } + // 将objMeta的filter中定义的eq条件作为固定的属性值设置到对象中 private void appendEqCondition(Map map) { ITreeBean filter = objMeta.getFilter(); @@ -390,10 +435,9 @@ public class ObjMetaBasedValidator { StdDataType type = propMeta.getStdDataType(); if (type == null) return value; - return type.convert(value, errCode -> { - return new NopException(ERR_BIZ_PROP_TYPE_CONVERT_FAIL).param(ARG_BIZ_OBJ_NAME, bizObjName) - .param(ARG_PROP_NAME, propMeta.getName()); - }); + return type.convert(value, errCode -> + new NopException(ERR_BIZ_PROP_TYPE_CONVERT_FAIL).param(ARG_BIZ_OBJ_NAME, bizObjName) + .param(ARG_PROP_NAME, propMeta.getName())); } public Map validateForSave(Map data, FieldSelectionBean selection) { -- Gitee