feat(flow): 按 procDefId 取净化源 XML 端点(含 idMap,D7 载入侧)

新增 GET /api/flow/process-definitions/{procDefId}/source:
createProcessDefinitionQuery 取 key/version + readProcessModel 取 BPMN
+ BpmnIdSanitizer.sanitize 净化中文 id → {found,bpmnXml,idMap,procDefKey,version}。
定义不存在 / XML 为空或读取异常 → {found:false}(HTTP 200,WARN log,防御性同 diagram 端点)。
idMap 透出原→净化映射,供发布时逆向还原原 key(D7)。

TDD:ProcessDiagramControllerTest +3 用例(净化XML+idMap+key版本 / 定义不存在
found:false / 读取异常 found:false),既有 4 用例零修改仍绿。
类 7/7、模块全量 71/71 绿(JDK21)。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-02 10:27:46 +08:00
parent e361d3a9ad
commit 3927146f08
2 changed files with 90 additions and 0 deletions

View File

@@ -11,6 +11,7 @@ import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -109,6 +110,38 @@ public class ProcessDiagramController {
return ResponseEntity.ok(resp);
}
@GetMapping("/process-definitions/{procDefId}/source")
@Operation(summary = "按流程定义 id 取净化源 XML",
description = "返回 {found, bpmnXml(净化后), idMap(原id→净化id), procDefKey, version},供 bpmn-js Modeler 在线编辑载入。"
+ "流程定义不存在 / BPMN 读取失败或为空 → found=false(HTTP 200,前端降级)。"
+ "中文 process id 经 BpmnIdSanitizer 可逆净化,idMap 供发布时逆向还原原 key(D7)。")
public ResponseEntity<Map<String, Object>> source(@PathVariable String procDefId) {
Map<String, Object> resp = new LinkedHashMap<>();
resp.put("procDefId", procDefId);
ProcessDefinition def = repositoryService.createProcessDefinitionQuery()
.processDefinitionId(procDefId).singleResult();
if (def == null) {
resp.put("found", false);
return ResponseEntity.ok(resp);
}
String bpmnXml = readProcessModel(procDefId);
if (bpmnXml == null || bpmnXml.isBlank()) {
log.warn("[流程源] BPMN 为空或读取失败 procDefId={}", procDefId);
resp.put("found", false);
return ResponseEntity.ok(resp);
}
BpmnIdSanitizer.Result r = BpmnIdSanitizer.sanitize(bpmnXml);
resp.put("found", true);
resp.put("bpmnXml", r.xml());
resp.put("idMap", r.idMap());
resp.put("procDefKey", def.getKey());
resp.put("version", def.getVersion());
return ResponseEntity.ok(resp);
}
/**
* 把 BPMN 里非法(非 ASCII NCName)的 id 及其全部引用一致重写为安全 ASCII id,使 bpmn-js 能解析渲染,
* 并同步重映射 active/finished 节点 id。净化算法委托 {@link BpmnIdSanitizer}(含可逆映射与 hash 碰撞消解)。

View File

@@ -17,6 +17,8 @@ import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricActivityInstanceQuery;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.history.HistoricProcessInstanceQuery;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.repository.ProcessDefinitionQuery;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.engine.runtime.ProcessInstanceQuery;
import org.junit.jupiter.api.BeforeEach;
@@ -39,6 +41,7 @@ class ProcessDiagramControllerTest {
@Mock ProcessInstanceQuery processInstanceQuery;
@Mock HistoricProcessInstanceQuery historicProcessInstanceQuery;
@Mock HistoricActivityInstanceQuery historicActivityInstanceQuery;
@Mock ProcessDefinitionQuery processDefinitionQuery;
MockMvc mvc;
@@ -150,4 +153,58 @@ class ProcessDiagramControllerTest {
org.junit.jupiter.api.Assertions.assertEquals(safe, finished.get(0));
org.junit.jupiter.api.Assertions.assertEquals("sid-A", active.get(0));
}
@Test
void source_按procDefId返净化XML与idMap及key版本() throws Exception {
// 存量流程 process id 为中文(如「总包合同交底」),moddle 载入前须净化;端点透出原→净化映射供发布时逆向。
ProcessDefinition def = org.mockito.Mockito.mock(ProcessDefinition.class);
when(def.getKey()).thenReturn("总包合同交底");
when(def.getVersion()).thenReturn(3);
when(repositoryService.createProcessDefinitionQuery()).thenReturn(processDefinitionQuery);
when(processDefinitionQuery.processDefinitionId("def-1")).thenReturn(processDefinitionQuery);
when(processDefinitionQuery.singleResult()).thenReturn(def);
String xml = "<definitions>"
+ "<process id=\"总包合同交底\" name=\"总包合同交底\">"
+ "<userTask id=\"sid-A\"/>"
+ "<sequenceFlow id=\"f1\" sourceRef=\"总包合同交底\" targetRef=\"sid-A\"/>"
+ "</process></definitions>";
when(repositoryService.getProcessModel("def-1"))
.thenReturn(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
mvc.perform(get("/api/flow/process-definitions/def-1/source"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.found").value(true))
.andExpect(jsonPath("$.procDefKey").value("总包合同交底"))
.andExpect(jsonPath("$.version").value(3))
// idMap 非空且含中文原 key;净化后 XML 的 id 属性不再含中文(name 展示位不受影响)
.andExpect(jsonPath("$.idMap").isNotEmpty())
.andExpect(jsonPath("$.idMap['总包合同交底']").isNotEmpty())
.andExpect(jsonPath("$.bpmnXml",
org.hamcrest.Matchers.not(org.hamcrest.Matchers.containsString("id=\"总包合同交底\""))));
}
@Test
void source_定义不存在返found_false() throws Exception {
when(repositoryService.createProcessDefinitionQuery()).thenReturn(processDefinitionQuery);
when(processDefinitionQuery.processDefinitionId("nope")).thenReturn(processDefinitionQuery);
when(processDefinitionQuery.singleResult()).thenReturn(null);
mvc.perform(get("/api/flow/process-definitions/nope/source"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.found").value(false));
}
@Test
void source_读取XML异常返found_false不抛500() throws Exception {
ProcessDefinition def = org.mockito.Mockito.mock(ProcessDefinition.class);
when(repositoryService.createProcessDefinitionQuery()).thenReturn(processDefinitionQuery);
when(processDefinitionQuery.processDefinitionId("def-x")).thenReturn(processDefinitionQuery);
when(processDefinitionQuery.singleResult()).thenReturn(def);
when(repositoryService.getProcessModel("def-x")).thenThrow(new RuntimeException("boom"));
mvc.perform(get("/api/flow/process-definitions/def-x/source"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.found").value(false));
}
}