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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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 0000000000000000000000000000000000000000..69bdf63e24a10e949cb3ad40765ab53b4fe5c03e --- /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 0000000000000000000000000000000000000000..7acf15c16555a329b0eb2aaba5d8e99745762905 --- /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 0000000000000000000000000000000000000000..e54ef8cae72e6999a9e36c032c1eb6f066c42409 --- /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 fd60f5844ecb319afed78525437b186e854008b1..6c80ea61ef0e1f946090469a4fa50ec4c7fe5f11 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) {