feat(metadata): 实体加载按 pageId 把引用字段解析为 {_id,_label} 对象 + 保存拍回裸 id
EntityController.load 加可选 ?pageId=:读 view_page(pageId) 字段,把 uitype=SelectDialog/MultiSelectDialog(带 dialogid)字段值解析成 {_id,_label} 对象(multi→对象数组),label 由各字段 dialogid 经 PickerController.resolveValuesToOptions 同源解析(口径=老 DialogDisplayUtil);type=3 树 dialog(转 tree-select 吃裸id)按 isTreeDialog 跳过不转对象防 undefined;_id 去尾补白。pageId 空→行为不变(回归)。PickerController.resolveValuesToOptions 提包级可见供复用(端点行为不变)。FlowInputCoercer.flattenPickerObjects:写流入参里 {_id,...} 形态值拍回裸 _id(去尾补白)——amis picker「载入后不动直接保存」会把整个对象进 payload,后端统一扁平守住提交值=id(零前端改动)。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -89,6 +89,11 @@ public class FlowInputCoercer {
|
|||||||
if (flowId == null || flowId.isBlank() || map == null || map.isEmpty()) {
|
if (flowId == null || flowId.isBlank() || map == null || map.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// 引用 picker 对象值扁平化:master-detail/编辑表单经 form initApi 把引用字段载为 {_id,_label}
|
||||||
|
// 对象(picker 自带 label 即显名)。amis picker 仅在用户「重新选值」时按 valueField=_id 提取裸 id;
|
||||||
|
// 「载入后不动直接保存」时整个对象原样进 payload → 引擎收到 Map 而非裸 id 必坏。故写流前统一把
|
||||||
|
// {_id,...} 形态对象就地降为其 _id 裸值(与 valueField=_id 提交语义一致),与表名解析无关先行。
|
||||||
|
flattenPickerObjects(map);
|
||||||
// 先从缓存取 tableName;缓存未命中则查 contentxml
|
// 先从缓存取 tableName;缓存未命中则查 contentxml
|
||||||
String tableName = flowTableCache.get(flowId, this::resolveTableNameByFlowId);
|
String tableName = flowTableCache.get(flowId, this::resolveTableNameByFlowId);
|
||||||
if (tableName.isEmpty()) {
|
if (tableName.isEmpty()) {
|
||||||
@@ -114,6 +119,8 @@ public class FlowInputCoercer {
|
|||||||
if (contentXml == null || contentXml.isBlank() || map == null || map.isEmpty()) {
|
if (contentXml == null || contentXml.isBlank() || map == null || map.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// 引用 picker {_id,_label} 对象 → 裸 _id(见 coerceByFlowId 注释;与表名解析无关先行)
|
||||||
|
flattenPickerObjects(map);
|
||||||
String tableName = extractTableName(contentXml);
|
String tableName = extractTableName(contentXml);
|
||||||
if (tableName == null) {
|
if (tableName == null) {
|
||||||
return; // 无固定 tableName(如跨表流),不做 coerce
|
return; // 无固定 tableName(如跨表流),不做 coerce
|
||||||
@@ -125,6 +132,26 @@ public class FlowInputCoercer {
|
|||||||
applyCoerce(map, colTypes);
|
applyCoerce(map, colTypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 引用 picker 对象值就地扁平化:把 {@code {_id,_label}} 形态的 Map 值降为其 {@code _id} 裸值
|
||||||
|
* (与 amis picker {@code valueField=_id} 提交语义一致)。仅处理「含 {@code _id} 键的 Map」,
|
||||||
|
* 其它结构化值(数组/普通 Map)不动;{@code _id} 为 String 时去尾补白(CHAR 主键对齐)。
|
||||||
|
*
|
||||||
|
* <p>背景:master-detail 右编辑表单经 {@code form initApi} 把引用字段载为对象(picker 自带 label
|
||||||
|
* 即显名,spike 实证只此可靠显名);「载入后不改直接保存」时 amis 不触发 valueField 提取 → 整个对象
|
||||||
|
* 进 payload。本扁平化是后端唯一收口点(零前端改动),保证引擎收到的引用列恒为裸 id。fail-safe:
|
||||||
|
* 任何异常都不抛、不阻断写。
|
||||||
|
*/
|
||||||
|
static void flattenPickerObjects(Map<String, Object> map) {
|
||||||
|
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
||||||
|
Object val = entry.getValue();
|
||||||
|
if (val instanceof Map<?, ?> obj && obj.containsKey("_id")) {
|
||||||
|
Object id = obj.get("_id");
|
||||||
|
entry.setValue(id instanceof String s ? s.stripTrailing() : id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** 遍历 map,按 colTypes 就地强制转换 String 值为目标 Java 类型。 */
|
/** 遍历 map,按 colTypes 就地强制转换 String 值为目标 Java 类型。 */
|
||||||
private void applyCoerce(Map<String, Object> map, Map<String, String> colTypes) {
|
private void applyCoerce(Map<String, Object> map, Map<String, String> colTypes) {
|
||||||
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
||||||
|
|||||||
@@ -8,6 +8,11 @@ import java.util.Map;
|
|||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.dom4j.Document;
|
||||||
|
import org.dom4j.DocumentHelper;
|
||||||
|
import org.dom4j.Element;
|
||||||
|
import org.springframework.dao.DataAccessException;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@@ -37,11 +42,17 @@ public class EntityController {
|
|||||||
|
|
||||||
private final POJOCRUDService pojoCRUDService;
|
private final POJOCRUDService pojoCRUDService;
|
||||||
private final IdentifierWhitelist identifierWhitelist;
|
private final IdentifierWhitelist identifierWhitelist;
|
||||||
|
private final JdbcTemplate jdbcTemplate;
|
||||||
|
private final PickerController pickerController;
|
||||||
|
|
||||||
public EntityController(POJOCRUDService pojoCRUDService,
|
public EntityController(POJOCRUDService pojoCRUDService,
|
||||||
IdentifierWhitelist identifierWhitelist) {
|
IdentifierWhitelist identifierWhitelist,
|
||||||
|
JdbcTemplate jdbcTemplate,
|
||||||
|
PickerController pickerController) {
|
||||||
this.pojoCRUDService = pojoCRUDService;
|
this.pojoCRUDService = pojoCRUDService;
|
||||||
this.identifierWhitelist = identifierWhitelist;
|
this.identifierWhitelist = identifierWhitelist;
|
||||||
|
this.jdbcTemplate = jdbcTemplate;
|
||||||
|
this.pickerController = pickerController;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -97,15 +108,35 @@ public class EntityController {
|
|||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 单条加载:GET /api/entity/{table}/{id} */
|
/**
|
||||||
|
* 单条加载:GET /api/entity/{table}/{id}[?pageId=]
|
||||||
|
*
|
||||||
|
* <p><b>{@code pageId} 引用字段对象解析(picker 加载即显名)</b>:amis picker(可编辑引用字段)
|
||||||
|
* 在「form initApi 动态载入已存值」时只显裸 id(不自动补拉 source 解析 label)。给定 {@code pageId}
|
||||||
|
* 时读其 {@code view_page} XML 收集 {@code SelectDialog/MultiSelectDialog}(带 {@code dialogid})字段,
|
||||||
|
* 把载入记录里这些字段的裸 id 值就地解析为 {@code {_id,_label}} 对象(多选→对象数组),自带 label,
|
||||||
|
* picker 经 valueField=_id 仍提交 _id 不变。{@code pageId} 空 → 行为不变(仅 CHAR trim)。
|
||||||
|
*/
|
||||||
@GetMapping("/{table}/{id}")
|
@GetMapping("/{table}/{id}")
|
||||||
public Map<String, Object> load(@PathVariable String table, @PathVariable String id) {
|
public Map<String, Object> load(@PathVariable String table, @PathVariable String id,
|
||||||
|
@RequestParam(required = false) String pageId) {
|
||||||
Properties ctx = UserSession.getAdmin();
|
Properties ctx = UserSession.getAdmin();
|
||||||
Object pojo = pojoCRUDService.loadPOJO(id, table.toUpperCase(), ctx, false);
|
String t = table.toUpperCase();
|
||||||
|
Object pojo = pojoCRUDService.loadPOJO(id, t, ctx, false);
|
||||||
|
Object data;
|
||||||
|
if (pojo instanceof GeneralPOJO p) {
|
||||||
|
Map<String, Object> row = toRow(p);
|
||||||
|
if (pageId != null && !pageId.isBlank()) {
|
||||||
|
resolveReferenceFields(row, pageId.trim());
|
||||||
|
}
|
||||||
|
data = row;
|
||||||
|
} else {
|
||||||
|
data = pojo;
|
||||||
|
}
|
||||||
Map<String, Object> resp = new HashMap<>();
|
Map<String, Object> resp = new HashMap<>();
|
||||||
resp.put("status", 0);
|
resp.put("status", 0);
|
||||||
resp.put("msg", "");
|
resp.put("msg", "");
|
||||||
resp.put("data", pojo instanceof GeneralPOJO p ? toRow(p) : pojo);
|
resp.put("data", data);
|
||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,4 +241,139 @@ public class EntityController {
|
|||||||
private static Object trimChar(Object v) {
|
private static Object trimChar(Object v) {
|
||||||
return v instanceof String s ? s.stripTrailing() : v;
|
return v instanceof String s ? s.stripTrailing() : v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** view_page 里的可编辑引用字段:列名(大写) + dialogid(picker 数据源) + 是否多选。 */
|
||||||
|
private record RefField(String name, String dialogid, boolean multi) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* dialogid(=view_template_id) 指向的模板是否 type=3(树形对话框,层级引用)。与转换器
|
||||||
|
* {@code TemplateToAmisConverter.isTreeDialog} 同口径:type=3 的 SelectDialog 被转换器改写为
|
||||||
|
* {@code tree-select}(吃裸 id 标量、自 source 拉树标签),而非 {@code picker}(吃 {_id,_label} 对象)。
|
||||||
|
* 故 tree-select 引用字段不可转对象(否则 amis 显「undefined」),本端点据此跳过。查询失败/非树 → false。
|
||||||
|
*/
|
||||||
|
private boolean isTreeDialog(String dialogId) {
|
||||||
|
if (dialogId == null || dialogId.isBlank()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Integer type = jdbcTemplate.queryForObject(
|
||||||
|
"SELECT type FROM view_template WHERE view_template_id = ?",
|
||||||
|
Integer.class, dialogId);
|
||||||
|
return type != null && type == 3;
|
||||||
|
} catch (DataAccessException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 把载入记录里的引用字段裸 id 就地替换为 {@code {_id,_label}} 对象(picker 自带 label 即显名):
|
||||||
|
* 读 {@code pageId} 的 view_page XML 收集 {@code SelectDialog/MultiSelectDialog}(带 {@code dialogid})
|
||||||
|
* 字段,对 row 里这些非空字段值经 {@link PickerController#resolveValuesToOptions}(口径同 /resolve)
|
||||||
|
* 解析为对象(多选→逗号拆分为对象数组),解析不到的回退 {@code {_id:value,_label:value}}(不丢值)。
|
||||||
|
* 引用字段对象的 {@code _id} 同样去尾补白对齐选项 value。XML 缺失/解析异常 → 原样返回(降级)。
|
||||||
|
*/
|
||||||
|
private void resolveReferenceFields(Map<String, Object> row, String pageId) {
|
||||||
|
List<RefField> refs = referenceFields(pageId);
|
||||||
|
if (refs.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (RefField rf : refs) {
|
||||||
|
Object raw = row.get(rf.name());
|
||||||
|
if (raw == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String v = String.valueOf(raw).trim();
|
||||||
|
if (v.isBlank()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (rf.multi()) {
|
||||||
|
List<Map<String, Object>> objs = new ArrayList<>();
|
||||||
|
for (String part : v.split(",")) {
|
||||||
|
String single = part.trim();
|
||||||
|
if (!single.isBlank()) {
|
||||||
|
objs.add(resolveRefValue(rf.dialogid(), single));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
row.put(rf.name(), objs);
|
||||||
|
} else {
|
||||||
|
row.put(rf.name(), resolveRefValue(rf.dialogid(), v));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 单个引用值 → {@code {_id,_label}} 对象(复用 PickerController 的 dialog→显示名口径)。
|
||||||
|
* 解析不到(dialog 异常/查无)→ 回退 {@code {_id:value,_label:value}}(不丢值)。{@code _id} 去尾补白。
|
||||||
|
*/
|
||||||
|
private Map<String, Object> resolveRefValue(String dialogid, String value) {
|
||||||
|
try {
|
||||||
|
List<Map<String, Object>> resolved = pickerController.resolveValuesToOptions(
|
||||||
|
dialogid, value, null, null, null, null, "_label", "_id");
|
||||||
|
if (!resolved.isEmpty()) {
|
||||||
|
Map<String, Object> opt = resolved.get(0);
|
||||||
|
Object idVal = opt.get("_id");
|
||||||
|
Map<String, Object> obj = new LinkedHashMap<>();
|
||||||
|
obj.put("_id", idVal instanceof String s ? s.stripTrailing() : idVal);
|
||||||
|
obj.put("_label", opt.get("_label"));
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
} catch (RuntimeException ex) {
|
||||||
|
// dialog 解析/查询异常 → 不丢值,回退裸 id(picker 至少显裸 id)
|
||||||
|
}
|
||||||
|
Map<String, Object> obj = new LinkedHashMap<>();
|
||||||
|
obj.put("_id", value);
|
||||||
|
obj.put("_label", value);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读 {@code pageId} 的 view_page XML,收集可编辑引用字段({@code uitype} ∈
|
||||||
|
* {@code {SelectDialog, MultiSelectDialog}} 且有 {@code dialogid})。XML 缺失/无 fields/异常 → 空列表。
|
||||||
|
*/
|
||||||
|
private List<RefField> referenceFields(String pageId) {
|
||||||
|
List<RefField> refs = new ArrayList<>();
|
||||||
|
String xml = queryViewPageXml(pageId);
|
||||||
|
if (xml == null || xml.isBlank()) {
|
||||||
|
return refs;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Document doc = DocumentHelper.parseText(xml.trim());
|
||||||
|
Element fields = doc.getRootElement().element("fields");
|
||||||
|
if (fields == null) {
|
||||||
|
return refs;
|
||||||
|
}
|
||||||
|
for (Element f : fields.elements("field")) {
|
||||||
|
String name = f.attributeValue("name");
|
||||||
|
String uitype = f.attributeValue("uitype");
|
||||||
|
String dialogid = f.attributeValue("dialogid");
|
||||||
|
if (name == null || name.isBlank() || dialogid == null || dialogid.isBlank()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
boolean multi = "MultiSelectDialog".equals(uitype);
|
||||||
|
if (!multi && !"SelectDialog".equals(uitype)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// type=3 树对话框 → 转换器渲为 tree-select(吃裸 id),不可转 {_id,_label} 对象,跳过。
|
||||||
|
if (isTreeDialog(dialogid)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
refs.add(new RefField(name.toUpperCase(), dialogid, multi));
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
return refs;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 取 view_page XML(PG xml 物理类型须 {@code xml::text} 取出);缺失/异常 → null。 */
|
||||||
|
private String queryViewPageXml(String pageId) {
|
||||||
|
try {
|
||||||
|
List<String> xmls = jdbcTemplate.queryForList(
|
||||||
|
"SELECT xml::text FROM view_page WHERE view_page_id = ? AND xml IS NOT NULL",
|
||||||
|
String.class, pageId);
|
||||||
|
return xmls.isEmpty() ? null : xmls.get(0);
|
||||||
|
} catch (DataAccessException ex) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -323,10 +323,10 @@ public class PickerController {
|
|||||||
* ③ 返回按入参顺序回填;④ 查不到的 value 兜底 {@code {label:value,value:value}}(不丢值、显裸 id);
|
* ③ 返回按入参顺序回填;④ 查不到的 value 兜底 {@code {label:value,value:value}}(不丢值、显裸 id);
|
||||||
* ⑤ 数据权限拒绝(PrivilegeException)→ 全部兜底,不 500(与 {@link #list} 友好降级一致)。
|
* ⑤ 数据权限拒绝(PrivilegeException)→ 全部兜底,不 500(与 {@link #list} 友好降级一致)。
|
||||||
*/
|
*/
|
||||||
private List<Map<String, Object>> resolveValuesToOptions(String entityOrRef, String value,
|
List<Map<String, Object>> resolveValuesToOptions(String entityOrRef, String value,
|
||||||
String asUser, String roles,
|
String asUser, String roles,
|
||||||
String asDepart, String asOrg,
|
String asDepart, String asOrg,
|
||||||
String labelKey, String valueKey) {
|
String labelKey, String valueKey) {
|
||||||
// 入参拆分 + trim(CHAR 主键去补白后比对)+ 保序去重(重复值只查一次,回填时按原序复用)
|
// 入参拆分 + trim(CHAR 主键去补白后比对)+ 保序去重(重复值只查一次,回填时按原序复用)
|
||||||
List<String> wanted = new ArrayList<>();
|
List<String> wanted = new ArrayList<>();
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
|
|||||||
@@ -336,4 +336,57 @@ class FlowInputCoercerTest {
|
|||||||
|
|
||||||
verify(jdbc, times(2)).queryForObject(anyString(), eq(String.class), eq(flowId));
|
verify(jdbc, times(2)).queryForObject(anyString(), eq(String.class), eq(flowId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ── flattenPickerObjects(master-detail 引用 picker {_id,_label} 对象 → 裸 _id)────────
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("flattenPickerObjects: {_id,_label} 对象 → 裸 _id(CHAR 主键去尾补白)")
|
||||||
|
void flattenPickerObjectToId() {
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("VIEW_TEMPLATEID", new HashMap<>(Map.of("_id", "a5996b8a", "_label", "文件阅读")));
|
||||||
|
map.put("MENUROOT", new HashMap<>(Map.of("_id", "MESSAGE ", "_label", "信息站"))); // 尾补白
|
||||||
|
map.put("NAME", "公文收发"); // 普通 String 不动
|
||||||
|
map.put("SEQ", 40); // 普通数值不动
|
||||||
|
|
||||||
|
FlowInputCoercer.flattenPickerObjects(map);
|
||||||
|
|
||||||
|
assertEquals("a5996b8a", map.get("VIEW_TEMPLATEID"), "对象降为裸 _id");
|
||||||
|
assertEquals("MESSAGE", map.get("MENUROOT"), "_id String 去尾补白");
|
||||||
|
assertEquals("公文收发", map.get("NAME"), "普通 String 原样");
|
||||||
|
assertEquals(40, map.get("SEQ"), "数值原样");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("flattenPickerObjects: 无 _id 键的 Map / 数组 不动(不误伤结构化输入)")
|
||||||
|
void flattenPickerObjectsLeavesNonRefStructuresAlone() {
|
||||||
|
Map<String, Object> nested = new HashMap<>(Map.of("foo", "bar")); // 无 _id
|
||||||
|
List<String> arr = List.of("a", "b");
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("CONFIG", nested);
|
||||||
|
map.put("TAGS", arr);
|
||||||
|
|
||||||
|
FlowInputCoercer.flattenPickerObjects(map);
|
||||||
|
|
||||||
|
assertSame(nested, map.get("CONFIG"), "无 _id 键的 Map 不动");
|
||||||
|
assertSame(arr, map.get("TAGS"), "数组不动");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("coerceByFlowId: 引用对象先扁平为 _id 再按列类型 coerce(端到端 master-detail 保存)")
|
||||||
|
void coerceByFlowIdFlattensRefObjectThenCoerces() {
|
||||||
|
String flowId = "SYS_MENU_ADD";
|
||||||
|
String xml = "<flow><input><field name=\"tableName\" initValue=\"SYS_MENU\"/></input></flow>";
|
||||||
|
when(jdbc.queryForObject(anyString(), eq(String.class), eq(flowId))).thenReturn(xml);
|
||||||
|
when(jdbc.queryForList(anyString(), eq("SYS_MENU")))
|
||||||
|
.thenReturn(List.of(Map.of("col", "SEQ", "ref", "INTEGER")));
|
||||||
|
|
||||||
|
Map<String, Object> map = new HashMap<>();
|
||||||
|
map.put("VIEW_TEMPLATEID", new HashMap<>(Map.of("_id", "a5996b8a", "_label", "文件阅读")));
|
||||||
|
map.put("SEQ", "40"); // String → coerce 为 Integer
|
||||||
|
|
||||||
|
coercer.coerceByFlowId(flowId, map);
|
||||||
|
|
||||||
|
assertEquals("a5996b8a", map.get("VIEW_TEMPLATEID"), "引用对象降为裸 _id(提交值是 id)");
|
||||||
|
assertEquals(40, map.get("SEQ"), "数值列仍按元数据 coerce 为 Integer");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,20 +3,26 @@ package com.eip4.platform.metadata.web;
|
|||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.Mockito.doThrow;
|
import static org.mockito.Mockito.doThrow;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
|
import com.dsasp.server.common.dataDic.bean.GeneralPOJO;
|
||||||
import com.dsasp.server.common.dataDic.service.POJOCRUDService;
|
import com.dsasp.server.common.dataDic.service.POJOCRUDService;
|
||||||
import com.eip4.platform.metadata.security.IdentifierWhitelist;
|
import com.eip4.platform.metadata.security.IdentifierWhitelist;
|
||||||
import com.eip4.platform.metadata.security.IdentifierWhitelistExceptionHandler;
|
import com.eip4.platform.metadata.security.IdentifierWhitelistExceptionHandler;
|
||||||
@@ -40,7 +46,8 @@ class EntityControllerWhitelistTest {
|
|||||||
POJOCRUDService engine = mock(POJOCRUDService.class);
|
POJOCRUDService engine = mock(POJOCRUDService.class);
|
||||||
doThrow(new IllegalIdentifierException("非法实体或元数据不存在: NOPE"))
|
doThrow(new IllegalIdentifierException("非法实体或元数据不存在: NOPE"))
|
||||||
.when(wl).requireLegalTable(eq("NOPE"));
|
.when(wl).requireLegalTable(eq("NOPE"));
|
||||||
EntityController c = new EntityController(engine, wl);
|
EntityController c = new EntityController(engine, wl,
|
||||||
|
mock(org.springframework.jdbc.core.JdbcTemplate.class), mock(PickerController.class));
|
||||||
|
|
||||||
assertThrows(IllegalIdentifierException.class,
|
assertThrows(IllegalIdentifierException.class,
|
||||||
() -> c.list("NOPE", 1, 10, null, null, null, null, null, null, new HashMap<>()));
|
() -> c.list("NOPE", 1, 10, null, null, null, null, null, null, new HashMap<>()));
|
||||||
@@ -52,7 +59,8 @@ class EntityControllerWhitelistTest {
|
|||||||
POJOCRUDService engine = mock(POJOCRUDService.class);
|
POJOCRUDService engine = mock(POJOCRUDService.class);
|
||||||
doThrow(new IllegalIdentifierException("查询条件含非法列标识符,已拒绝"))
|
doThrow(new IllegalIdentifierException("查询条件含非法列标识符,已拒绝"))
|
||||||
.when(wl).requireLegalColumn(eq(TABLE), any(), eq("查询条件"));
|
.when(wl).requireLegalColumn(eq(TABLE), any(), eq("查询条件"));
|
||||||
EntityController c = new EntityController(engine, wl);
|
EntityController c = new EntityController(engine, wl,
|
||||||
|
mock(org.springframework.jdbc.core.JdbcTemplate.class), mock(PickerController.class));
|
||||||
|
|
||||||
Map<String, String> params = new HashMap<>();
|
Map<String, String> params = new HashMap<>();
|
||||||
params.put("XX' OR 1=1--", "x");
|
params.put("XX' OR 1=1--", "x");
|
||||||
@@ -64,7 +72,8 @@ class EntityControllerWhitelistTest {
|
|||||||
void 非法排序方向拒绝() {
|
void 非法排序方向拒绝() {
|
||||||
IdentifierWhitelist wl = mock(IdentifierWhitelist.class);
|
IdentifierWhitelist wl = mock(IdentifierWhitelist.class);
|
||||||
POJOCRUDService engine = mock(POJOCRUDService.class);
|
POJOCRUDService engine = mock(POJOCRUDService.class);
|
||||||
EntityController c = new EntityController(engine, wl);
|
EntityController c = new EntityController(engine, wl,
|
||||||
|
mock(org.springframework.jdbc.core.JdbcTemplate.class), mock(PickerController.class));
|
||||||
|
|
||||||
// orderBy 列合法(白名单不抛),但 orderDir=drop 非 asc/desc → Controller 抛
|
// orderBy 列合法(白名单不抛),但 orderDir=drop 非 asc/desc → Controller 抛
|
||||||
IllegalIdentifierException ex = assertThrows(IllegalIdentifierException.class,
|
IllegalIdentifierException ex = assertThrows(IllegalIdentifierException.class,
|
||||||
@@ -81,7 +90,8 @@ class EntityControllerWhitelistTest {
|
|||||||
engineResult.put(POJOCRUDService.LIST_RESULT, java.util.List.of());
|
engineResult.put(POJOCRUDService.LIST_RESULT, java.util.List.of());
|
||||||
engineResult.put(POJOCRUDService.PAGE_COUNT, 0L);
|
engineResult.put(POJOCRUDService.PAGE_COUNT, 0L);
|
||||||
when(engine.listSelect(any(), eq(TABLE), any(Properties.class))).thenReturn(engineResult);
|
when(engine.listSelect(any(), eq(TABLE), any(Properties.class))).thenReturn(engineResult);
|
||||||
EntityController c = new EntityController(engine, wl);
|
EntityController c = new EntityController(engine, wl,
|
||||||
|
mock(org.springframework.jdbc.core.JdbcTemplate.class), mock(PickerController.class));
|
||||||
|
|
||||||
Map<String, Object> resp = c.list(TABLE, 1, 10, null, null, null, null, "ORDERNO", "asc", new HashMap<>());
|
Map<String, Object> resp = c.list(TABLE, 1, 10, null, null, null, null, "ORDERNO", "asc", new HashMap<>());
|
||||||
assertEquals(0, resp.get("status"));
|
assertEquals(0, resp.get("status"));
|
||||||
@@ -100,7 +110,8 @@ class EntityControllerWhitelistTest {
|
|||||||
IdentifierWhitelist wl = mock(IdentifierWhitelist.class);
|
IdentifierWhitelist wl = mock(IdentifierWhitelist.class);
|
||||||
POJOCRUDService engine = mock(POJOCRUDService.class);
|
POJOCRUDService engine = mock(POJOCRUDService.class);
|
||||||
when(engine.listSelect(any(), eq(TABLE), any(Properties.class))).thenReturn(new HashMap<>());
|
when(engine.listSelect(any(), eq(TABLE), any(Properties.class))).thenReturn(new HashMap<>());
|
||||||
EntityController c = new EntityController(engine, wl);
|
EntityController c = new EntityController(engine, wl,
|
||||||
|
mock(org.springframework.jdbc.core.JdbcTemplate.class), mock(PickerController.class));
|
||||||
|
|
||||||
// page/perPage/asUser/roles 是保留键(经方法签名绑定),allParams 里只放保留键不应触发列校验
|
// page/perPage/asUser/roles 是保留键(经方法签名绑定),allParams 里只放保留键不应触发列校验
|
||||||
Map<String, String> params = new HashMap<>();
|
Map<String, String> params = new HashMap<>();
|
||||||
@@ -123,4 +134,103 @@ class EntityControllerWhitelistTest {
|
|||||||
assertEquals(1, resp.getBody().get("status"));
|
assertEquals(1, resp.getBody().get("status"));
|
||||||
assertFalse(String.valueOf(resp.getBody().get("msg")).contains("'"));
|
assertFalse(String.valueOf(resp.getBody().get("msg")).contains("'"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 单字段 GeneralPOJO 桩:主键 + 一个引用列 MENUROOT=值。 */
|
||||||
|
private static GeneralPOJO refRow(String id, String menuRoot) {
|
||||||
|
GeneralPOJO p = mock(GeneralPOJO.class);
|
||||||
|
when(p.get_ID()).thenReturn(id);
|
||||||
|
when(p.get_ColumnCount()).thenReturn(1);
|
||||||
|
when(p.get_ColumnName(0)).thenReturn("MENUROOT");
|
||||||
|
when(p.get_Value(0)).thenReturn(menuRoot);
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
void load带pageId_SelectDialog字段返对象() {
|
||||||
|
IdentifierWhitelist wl = mock(IdentifierWhitelist.class);
|
||||||
|
POJOCRUDService engine = mock(POJOCRUDService.class);
|
||||||
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||||
|
PickerController picker = mock(PickerController.class);
|
||||||
|
|
||||||
|
// 先构造 row(其内部 stubbing)再 stub engine,避免「stubbing 内嵌 stubbing」破坏 Mockito 状态
|
||||||
|
GeneralPOJO row = refRow("n1", "MESSAGE");
|
||||||
|
when(engine.loadPOJO(eq("n1"), eq(TABLE), any(Properties.class), anyBoolean()))
|
||||||
|
.thenReturn(row);
|
||||||
|
// view_page XML:MENUROOT 是 SelectDialog + dialogid=D1
|
||||||
|
when(jdbc.queryForList(any(String.class), eq(String.class), eq("PAGE1")))
|
||||||
|
.thenReturn(List.of("<page><fields>"
|
||||||
|
+ "<field name='MENUROOT' uitype='SelectDialog' dialogid='D1'/>"
|
||||||
|
+ "</fields></page>"));
|
||||||
|
// dialog 解析 MESSAGE → 显示名「信息站」
|
||||||
|
Map<String, Object> opt = new HashMap<>();
|
||||||
|
opt.put("_id", "MESSAGE");
|
||||||
|
opt.put("_label", "信息站");
|
||||||
|
when(picker.resolveValuesToOptions(eq("D1"), eq("MESSAGE"),
|
||||||
|
any(), any(), any(), any(), eq("_label"), eq("_id")))
|
||||||
|
.thenReturn(List.of(opt));
|
||||||
|
|
||||||
|
EntityController c = new EntityController(engine, wl, jdbc, picker);
|
||||||
|
Map<String, Object> resp = c.load(TABLE, "n1", "PAGE1");
|
||||||
|
assertEquals(0, resp.get("status"));
|
||||||
|
Map<String, Object> data = (Map<String, Object>) resp.get("data");
|
||||||
|
Object menuRoot = data.get("MENUROOT");
|
||||||
|
assertInstanceOf(Map.class, menuRoot, "SelectDialog 引用字段应解析为 {_id,_label} 对象");
|
||||||
|
Map<String, Object> obj = (Map<String, Object>) menuRoot;
|
||||||
|
assertEquals("MESSAGE", obj.get("_id"), "提交值仍是裸 id(picker valueField=_id)");
|
||||||
|
assertEquals("信息站", obj.get("_label"), "自带 label 即显名");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
void load带pageId_树对话框字段不转对象() {
|
||||||
|
IdentifierWhitelist wl = mock(IdentifierWhitelist.class);
|
||||||
|
POJOCRUDService engine = mock(POJOCRUDService.class);
|
||||||
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||||
|
PickerController picker = mock(PickerController.class);
|
||||||
|
|
||||||
|
// 行:PARENTID 是层级引用(tree-select),值是裸 id
|
||||||
|
GeneralPOJO p = mock(GeneralPOJO.class);
|
||||||
|
when(p.get_ID()).thenReturn("n1");
|
||||||
|
when(p.get_ColumnCount()).thenReturn(1);
|
||||||
|
when(p.get_ColumnName(0)).thenReturn("PARENTID");
|
||||||
|
when(p.get_Value(0)).thenReturn("parent-1");
|
||||||
|
when(engine.loadPOJO(eq("n1"), eq(TABLE), any(Properties.class), anyBoolean())).thenReturn(p);
|
||||||
|
|
||||||
|
when(jdbc.queryForList(any(String.class), eq(String.class), eq("PAGE1")))
|
||||||
|
.thenReturn(List.of("<page><fields>"
|
||||||
|
+ "<field name='PARENTID' uitype='SelectDialog' dialogid='TREE1'/>"
|
||||||
|
+ "</fields></page>"));
|
||||||
|
// TREE1 是 type=3 树对话框 → isTreeDialog=true → 不转对象
|
||||||
|
when(jdbc.queryForObject(any(String.class), eq(Integer.class), eq("TREE1"))).thenReturn(3);
|
||||||
|
|
||||||
|
EntityController c = new EntityController(engine, wl, jdbc, picker);
|
||||||
|
Map<String, Object> resp = c.load(TABLE, "n1", "PAGE1");
|
||||||
|
Map<String, Object> data = (Map<String, Object>) resp.get("data");
|
||||||
|
assertEquals("parent-1", data.get("PARENTID"), "tree-select 引用字段保持裸 id(不转对象,避免 amis 显 undefined)");
|
||||||
|
// 树对话框不调 picker 解析
|
||||||
|
verify(picker, never()).resolveValuesToOptions(any(), any(), any(), any(), any(), any(), any(), any());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
void load无pageId_返原样不解析引用() {
|
||||||
|
IdentifierWhitelist wl = mock(IdentifierWhitelist.class);
|
||||||
|
POJOCRUDService engine = mock(POJOCRUDService.class);
|
||||||
|
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||||
|
PickerController picker = mock(PickerController.class);
|
||||||
|
|
||||||
|
GeneralPOJO row = refRow("n1", "MESSAGE");
|
||||||
|
when(engine.loadPOJO(eq("n1"), eq(TABLE), any(Properties.class), anyBoolean()))
|
||||||
|
.thenReturn(row);
|
||||||
|
|
||||||
|
EntityController c = new EntityController(engine, wl, jdbc, picker);
|
||||||
|
Map<String, Object> resp = c.load(TABLE, "n1", null);
|
||||||
|
assertEquals(0, resp.get("status"));
|
||||||
|
Map<String, Object> data = (Map<String, Object>) resp.get("data");
|
||||||
|
assertEquals("MESSAGE", data.get("MENUROOT"), "无 pageId 引用字段原样返回(CHAR trim 后裸值)");
|
||||||
|
// 无 pageId 不读 view_page、不解析 dialog
|
||||||
|
verify(jdbc, never()).queryForList(any(String.class), eq(String.class), any());
|
||||||
|
verify(picker, never()).resolveValuesToOptions(any(), any(), any(), any(), any(), any(), any(), any());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user