diff --git a/platform-metadata/src/main/java/com/eip4/platform/metadata/flow/FlowInputCoercer.java b/platform-metadata/src/main/java/com/eip4/platform/metadata/flow/FlowInputCoercer.java index d2992fa..6b4b546 100644 --- a/platform-metadata/src/main/java/com/eip4/platform/metadata/flow/FlowInputCoercer.java +++ b/platform-metadata/src/main/java/com/eip4/platform/metadata/flow/FlowInputCoercer.java @@ -164,6 +164,17 @@ public class FlowInputCoercer { if (refId == null) { continue; // 元数据无此列,不动 } + // 空串 → null(写路径通用修复,列类型感知,见 emptyToNull):amis 表单里未选的 + // select/picker/tree-select、未赋默认值的隐藏框提交 "" 空串而非 null。FK/PK/数字/日期列 + // 吃空串 → 保存失败(FK 「功能引用的对象被删除」、PK "" 致 isSave 误判走 update、数字列 + // (Integer)"" cast 崩)。对齐老语义(老前端未选字段直接省略键 = null,引擎 INSERT 跳过该列 + // 走 DB 默认/可空、PK null 则自动生成 UUID)。先判空串再 coerce:空串决定 null 后无需再 parse。 + if (((String) val).trim().isEmpty()) { + if (emptyToNull(refId, colUpper)) { + entry.setValue(null); + } + continue; // 空串列处理完毕(不论是否 nullify,都无需走类型 coerce) + } Object coerced = coerceValue((String) val, refId, colUpper); if (coerced != val) { // 引用变了才更新 entry.setValue(coerced); @@ -171,6 +182,49 @@ public class FlowInputCoercer { } } + /** + * 已知非 id 文本 DisplayType(自由文本列),其空串不转 null(保留老语义:老引擎把空文本 + * 存为 {@code ''},且部分 NOT NULL 文本列以空串为合法默认)。其余一律 ""→null。 + */ + private static final java.util.Set TEXT_DISPLAY_TYPES = java.util.Set.of("NVARCHAR", "CODE"); + + /** + * 列类型感知的「空串 → null」判定:决定某列的 {@code ""} 是否应转 null。 + * + *

转 null(true)——本次修复核心: + *

+ * + *

保留空串(false):自由文本列({@code NVARCHAR/CODE} 且列名非 {@code _ID} 结尾)—— + * 对齐老引擎语义(空文本存 {@code ''}),不破坏以空串为合法默认的 NOT NULL 文本列。 + * + * @param refId referenceid(DisplayType 字符串或 36 位实体 UUID,已大写) + * @param colUpper 列名(大写) + */ + static boolean emptyToNull(String refId, String colUpper) { + // id 列(PK/FK,名以 _ID 结尾):恒转 null(本问题核心:空 PK/FK 必坏) + if (colUpper.endsWith("_ID")) { + return true; + } + // FK/引用列:referenceid 是 36 位实体 UUID(非已知 DisplayType 关键字)→ 转 null + if (refId.length() == 36) { + return true; + } + // 自由文本列(NVARCHAR/CODE):保留空串(对齐老语义,护 NOT NULL 合法空文本) + if (TEXT_DISPLAY_TYPES.contains(refId)) { + return false; + } + // 其余已知类型(数字/小数/布尔/日期/CHAR-id/LIST/BLOB 等非文本)→ 转 null + return true; + } + /** flowId → 流 {@code } 声明的必填 *_ID 入参名缓存({@code ""} = 无/歧义)。 */ private final Cache requiredIdInputCache = Caffeine.newBuilder() .maximumSize(5_000) diff --git a/platform-metadata/src/test/java/com/eip4/platform/metadata/flow/FlowInputCoercerTest.java b/platform-metadata/src/test/java/com/eip4/platform/metadata/flow/FlowInputCoercerTest.java index bf323fa..74d487d 100644 --- a/platform-metadata/src/test/java/com/eip4/platform/metadata/flow/FlowInputCoercerTest.java +++ b/platform-metadata/src/test/java/com/eip4/platform/metadata/flow/FlowInputCoercerTest.java @@ -389,4 +389,112 @@ class FlowInputCoercerTest { assertEquals("a5996b8a", map.get("VIEW_TEMPLATEID"), "引用对象降为裸 _id(提交值是 id)"); assertEquals(40, map.get("SEQ"), "数值列仍按元数据 coerce 为 Integer"); } + + // ── emptyToNull(列类型感知的 空串→null 规则)────────────────────────────────── + + @Test + @DisplayName("emptyToNull: id 列(名以 _ID 结尾,PK/FK)恒转 null") + void emptyToNullIdColumns() { + // PK(CHAR) + org.junit.jupiter.api.Assertions.assertTrue( + FlowInputCoercer.emptyToNull("CHAR", "SYS_MENU_ID"), "CHAR PK _ID → null"); + // FK(refId 是 36 位 UUID 且名以 _ID 结尾) + org.junit.jupiter.api.Assertions.assertTrue( + FlowInputCoercer.emptyToNull("0236DBDC-435C-4F6A-B0E8-8FD6D215EC45", "USER_ID"), "FK _ID → null"); + } + + @Test + @DisplayName("emptyToNull: FK/引用列(refId=36 位实体 UUID)转 null(即便列名不以 _ID 结尾,如 MENUROOT)") + void emptyToNullReferenceColumns() { + org.junit.jupiter.api.Assertions.assertTrue( + FlowInputCoercer.emptyToNull("C931BA3B-4CAC-44AB-9836-B120E9FDCF36", "MENUROOT"), + "MENUROOT FK(36 位 refId)→ null"); + org.junit.jupiter.api.Assertions.assertTrue( + FlowInputCoercer.emptyToNull("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", "VIEW_TEMPLATEID"), + "VIEW_TEMPLATEID FK(36 字符 refId)→ null"); + } + + @Test + @DisplayName("emptyToNull: 非文本类型列(数字/小数/布尔/日期)转 null") + void emptyToNullNonTextTypes() { + org.junit.jupiter.api.Assertions.assertTrue(FlowInputCoercer.emptyToNull("INTEGER", "SEQ")); + org.junit.jupiter.api.Assertions.assertTrue(FlowInputCoercer.emptyToNull("ENUMERATE", "IMPORTANT")); + org.junit.jupiter.api.Assertions.assertTrue(FlowInputCoercer.emptyToNull("STATUS", "STATUS")); + org.junit.jupiter.api.Assertions.assertTrue(FlowInputCoercer.emptyToNull("DECIMAL", "AMOUNT")); + org.junit.jupiter.api.Assertions.assertTrue(FlowInputCoercer.emptyToNull("MONEY", "PRICE")); + org.junit.jupiter.api.Assertions.assertTrue(FlowInputCoercer.emptyToNull("BOOLEAN", "ISACTIVE")); + org.junit.jupiter.api.Assertions.assertTrue(FlowInputCoercer.emptyToNull("DATE", "PUBLISHDATE")); + org.junit.jupiter.api.Assertions.assertTrue(FlowInputCoercer.emptyToNull("TIME", "CREATETIME")); + } + + @Test + @DisplayName("emptyToNull: 自由文本列(NVARCHAR/CODE,名非 _ID)保留空串 → false(护 NOT NULL 合法空文本)") + void emptyToNullTextColumnsPreserved() { + org.junit.jupiter.api.Assertions.assertFalse( + FlowInputCoercer.emptyToNull("NVARCHAR", "TITLE"), "NVARCHAR 自由文本保留空串"); + org.junit.jupiter.api.Assertions.assertFalse( + FlowInputCoercer.emptyToNull("CODE", "DOCCODE"), "CODE 保留空串"); + org.junit.jupiter.api.Assertions.assertFalse( + FlowInputCoercer.emptyToNull("NVARCHAR", "DEPARTID"), "NVARCHAR DEPARTID(老库存裸 id 文本)保留空串"); + } + + @Test + @DisplayName("coerce: 端到端 空串→null(FK/PK/数字 转 null,自由文本保留,合法值不动)") + void coerceEmptyToNullEndToEnd() { + String xml = ""; + when(jdbc.queryForList(anyString(), eq("SYS_MENU"))) + .thenReturn(List.of( + Map.of("col", "SYS_MENU_ID", "ref", "CHAR"), // PK + Map.of("col", "MENUROOT", "ref", "C931BA3B-4CAC-44AB-9836-B120E9FDCF36"), // FK 36 位 + Map.of("col", "VIEW_TEMPLATEID", "ref", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"), // FK 36 字符 + Map.of("col", "SEQ", "ref", "INTEGER"), // 数字 + Map.of("col", "NAME", "ref", "NVARCHAR") // 自由文本 + )); + + Map map = new HashMap<>(); + map.put("SYS_MENU_ID", ""); // PK 空串 → null(引擎自动生成 UUID) + map.put("MENUROOT", ""); // FK 空串 → null(INSERT 跳过,避免 FK 违反) + map.put("VIEW_TEMPLATEID", ""); // FK 空串 → null + map.put("SEQ", ""); // 数字空串 → null(避免 (Integer)"" 崩) + map.put("NAME", "测试空串"); // 合法文本 → 不动 + + coercer.coerce(xml, map); + + assertNull(map.get("SYS_MENU_ID"), "PK 空串 → null"); + assertNull(map.get("MENUROOT"), "FK 空串 → null"); + assertNull(map.get("VIEW_TEMPLATEID"), "FK 空串 → null"); + assertNull(map.get("SEQ"), "数字空串 → null"); + assertEquals("测试空串", map.get("NAME"), "合法文本不被误伤"); + } + + @Test + @DisplayName("coerce: 数字列合法非空值仍正常 coerce 为 Integer(空串修复不破坏合法值)") + void coerceNonEmptyNumericStillCoerces() { + String xml = ""; + when(jdbc.queryForList(anyString(), eq("SYS_MENU"))) + .thenReturn(List.of(Map.of("col", "SEQ", "ref", "INTEGER"))); + + Map map = new HashMap<>(); + map.put("SEQ", "99"); + coercer.coerce(xml, map); + assertEquals(Integer.valueOf(99), map.get("SEQ"), "非空数字仍 coerce 为 Integer"); + } + + @Test + @DisplayName("coerceByFlowId: 引用 picker 对象 _id 为空串 → 扁平后转 null(与 flattenPickerObjects 协同)") + void coerceByFlowIdFlattenedEmptyRefToNull() { + String flowId = "SYS_MENU_ADD"; + String xml = ""; + when(jdbc.queryForObject(anyString(), eq(String.class), eq(flowId))).thenReturn(xml); + when(jdbc.queryForList(anyString(), eq("SYS_MENU"))) + .thenReturn(List.of(Map.of("col", "MENUROOT", "ref", "C931BA3B-4CAC-44AB-9836-B120E9FDCF36"))); + + Map map = new HashMap<>(); + // picker 载入未改、其 _id 为空串(未选)→ flatten 为 "",再按 FK 列类型 → null + map.put("MENUROOT", new HashMap<>(Map.of("_id", "", "_label", ""))); + + coercer.coerceByFlowId(flowId, map); + + assertNull(map.get("MENUROOT"), "空 _id 引用对象 → null(避免 FK 违反)"); + } }