feat(flow+gateway): by-key 取最新版源 XML(修 process-select options=model 语义缺口)

live 验收发现:/api/process-select/options 在本库 143 项全部 source='model',
option.id=act_re_model id(非 procDefId)→ by-id source 端点永远 found:false;
option.key 才是真流程 key。补 by-key 姊妹端点,打开器改用 key 载入。

- flowable-service ProcessDiagramController:
  + GET /api/flow/process-definitions/by-key/{key}/source
    (processDefinitionKey(key).latestVersion().singleResult(),null→found:false)。
  by-id 与 by-key 逻辑抽共享私有 sourceResponse(procDefId, def),不复制粘贴;
  by-id 行为不变(仍以入参 procDefId 读部署 BPMN,既有 7 测试零修改)。
- 网关:FlowableClient.procDefSourceByKey(key)(uri 变量严格编码,禁拼接)
  + ProcessModelerController GET /api/workflow/process-definitions/by-key/{key}/source
    (同超管门,非超管 403)。by-id/by-key 段数不同无路由歧义。

测试(mvn -pl flowable-service,platform-flow-gateway,platform-auth -am test 全绿,既有零修改):
- ProcessDiagramControllerTest 7→9(+2:by-key 命中取最新版 v2 净化XML+idMap / key 不存在 found:false)
- FlowableClientTest 11→12(+1:纯中文 key「总包合同交底」uri 变量严格编码,断言 %E6%80%BB 且无裸中文)
- ProcessModelerControllerTest 4→6(+2:by-key 超管 200 透传 / 非超管 403 不触发下游)
- 三模块 Reactor 均 SUCCESS。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-02 10:59:04 +08:00
parent 88ab0b8f0e
commit 9cfef7e931
6 changed files with 153 additions and 4 deletions

View File

@@ -112,15 +112,37 @@ public class ProcessDiagramController {
@GetMapping("/process-definitions/{procDefId}/source") @GetMapping("/process-definitions/{procDefId}/source")
@Operation(summary = "按流程定义 id 取净化源 XML", @Operation(summary = "按流程定义 id 取净化源 XML",
description = "返回 {found, bpmnXml(净化后), idMap(原id→净化id), procDefKey, version},供 bpmn-js Modeler 在线编辑载入。" description = "返回 {procDefId, found, bpmnXml(净化后), idMap(原id→净化id), procDefKey, version},供 bpmn-js Modeler 在线编辑载入。"
+ "流程定义不存在 / BPMN 读取失败或为空 → found=false(HTTP 200,前端降级)。" + "流程定义不存在 / BPMN 读取失败或为空 → found=false(HTTP 200,前端降级)。"
+ "中文 process id 经 BpmnIdSanitizer 可逆净化,idMap 供发布时逆向还原原 key(D7)。") + "中文 process id 经 BpmnIdSanitizer 可逆净化,idMap 供发布时逆向还原原 key(D7)。")
public ResponseEntity<Map<String, Object>> source(@PathVariable String procDefId) { public ResponseEntity<Map<String, Object>> source(@PathVariable String procDefId) {
Map<String, Object> resp = new LinkedHashMap<>();
resp.put("procDefId", procDefId);
ProcessDefinition def = repositoryService.createProcessDefinitionQuery() ProcessDefinition def = repositoryService.createProcessDefinitionQuery()
.processDefinitionId(procDefId).singleResult(); .processDefinitionId(procDefId).singleResult();
return sourceResponse(procDefId, def);
}
@GetMapping("/process-definitions/by-key/{key}/source")
@Operation(summary = "按流程 key 取最新版净化源 XML",
description = "返回与 by-id 端点完全相同的形状 {procDefId, found, bpmnXml, idMap, procDefKey, version},"
+ "供 Modeler 打开器按真流程 key 载入(process-select 的 source='model' 项 id 是 act_re_model id 而非 procDefId,"
+ "by-id 永远 found:false → 改用本端点)。processDefinitionKey(key).latestVersion() 取最新版,key 不存在 found:false。")
public ResponseEntity<Map<String, Object>> sourceByKey(@PathVariable String key) {
ProcessDefinition def = repositoryService.createProcessDefinitionQuery()
.processDefinitionKey(key).latestVersion().singleResult();
String procDefId = def == null ? null : def.getId();
return sourceResponse(procDefId, def);
}
/**
* 按已解析的流程定义构建源响应(by-id / by-key 共用,逻辑单一不复制):净化 XML + 原→净化 idMap + key/version。
* {@code def==null}(不存在)或 BPMN 空/读失败 → {@code found:false}(HTTP 200,前端降级)。
* {@code procDefId} 是读取部署 BPMN 资源用的定义 id(by-id=入参、by-key=解析出的最新版 id),命中时随响应回显。
*/
private ResponseEntity<Map<String, Object>> sourceResponse(String procDefId, ProcessDefinition def) {
Map<String, Object> resp = new LinkedHashMap<>();
if (procDefId != null) {
resp.put("procDefId", procDefId);
}
if (def == null) { if (def == null) {
resp.put("found", false); resp.put("found", false);
return ResponseEntity.ok(resp); return ResponseEntity.ok(resp);

View File

@@ -198,6 +198,50 @@ class ProcessDiagramControllerTest {
.andExpect(jsonPath("$.found").value(false)); .andExpect(jsonPath("$.found").value(false));
} }
@Test
void sourceByKey_按key命中返最新版净化XML与idMap及版本() throws Exception {
// process-select options 的 source='model' 项 id 是 act_re_model id(非 procDefId),by-id 永远 found:false;
// 打开器改用 option.key(真流程 key)走本端点。latestVersion() 在两版本(v1/v2)里取 v2。
ProcessDefinition v2 = org.mockito.Mockito.mock(ProcessDefinition.class);
when(v2.getId()).thenReturn("总包合同交底:2:aa22");
when(v2.getKey()).thenReturn("总包合同交底");
when(v2.getVersion()).thenReturn(2);
when(repositoryService.createProcessDefinitionQuery()).thenReturn(processDefinitionQuery);
when(processDefinitionQuery.processDefinitionKey("总包合同交底")).thenReturn(processDefinitionQuery);
when(processDefinitionQuery.latestVersion()).thenReturn(processDefinitionQuery);
when(processDefinitionQuery.singleResult()).thenReturn(v2);
String xml = "<definitions>"
+ "<process id=\"总包合同交底\" name=\"总包合同交底\">"
+ "<userTask id=\"sid-A\"/>"
+ "<sequenceFlow id=\"f1\" sourceRef=\"总包合同交底\" targetRef=\"sid-A\"/>"
+ "</process></definitions>";
when(repositoryService.getProcessModel("总包合同交底:2:aa22"))
.thenReturn(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
mvc.perform(get("/api/flow/process-definitions/by-key/{key}/source", "总包合同交底"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.found").value(true))
.andExpect(jsonPath("$.procDefId").value("总包合同交底:2:aa22"))
.andExpect(jsonPath("$.procDefKey").value("总包合同交底"))
.andExpect(jsonPath("$.version").value(2))
.andExpect(jsonPath("$.idMap['总包合同交底']").isNotEmpty())
.andExpect(jsonPath("$.bpmnXml",
org.hamcrest.Matchers.not(org.hamcrest.Matchers.containsString("id=\"总包合同交底\""))));
}
@Test
void sourceByKey_key不存在返found_false() throws Exception {
when(repositoryService.createProcessDefinitionQuery()).thenReturn(processDefinitionQuery);
when(processDefinitionQuery.processDefinitionKey("无此流程")).thenReturn(processDefinitionQuery);
when(processDefinitionQuery.latestVersion()).thenReturn(processDefinitionQuery);
when(processDefinitionQuery.singleResult()).thenReturn(null);
mvc.perform(get("/api/flow/process-definitions/by-key/{key}/source", "无此流程"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.found").value(false));
}
@Test @Test
void source_读取XML异常返found_false不抛500() throws Exception { void source_读取XML异常返found_false不抛500() throws Exception {
ProcessDefinition def = org.mockito.Mockito.mock(ProcessDefinition.class); ProcessDefinition def = org.mockito.Mockito.mock(ProcessDefinition.class);

View File

@@ -244,6 +244,22 @@ public class FlowableClient {
.body(Map.class)); .body(Map.class));
} }
/**
* 按流程 key 取最新版净化源 XML(供 Modeler 打开器按真流程 key 载入)。对应 flowable-service
* {@code GET /api/flow/process-definitions/by-key/{key}/source},返回体与 {@link #procDefSource} 同形状。
*
* <p>process-select 的 {@code source='model'} 选项 id 是 act_re_model id 而非 procDefId,
* by-id 端点永远 {@code found:false} → 打开器改用真流程 key 走本路。key 同样作为 uri 变量严格编码
* (纯中文 key percent 编码),<b>绝不字符串拼 URL</b>。
*/
@SuppressWarnings("unchecked")
public Map<String, Object> procDefSourceByKey(String key) {
return execute("流程源(按key)", () -> restClient.get()
.uri("/api/flow/process-definitions/by-key/{key}/source", key)
.retrieve()
.body(Map.class));
}
/** 按发起人查询历史流程实例。对应 flowable-service {@code GET /api/flow/process/started}。 */ /** 按发起人查询历史流程实例。对应 flowable-service {@code GET /api/flow/process/started}。 */
public List<ProcessInstanceDto> started(String startedBy, int firstResult, int maxResults) { public List<ProcessInstanceDto> started(String startedBy, int firstResult, int maxResults) {
return execute("发起流程列表", () -> restClient.get() return execute("发起流程列表", () -> restClient.get()

View File

@@ -51,6 +51,20 @@ public class ProcessModelerController {
return ResponseEntity.ok(flowableClient.procDefSource(procDefId)); return ResponseEntity.ok(flowableClient.procDefSource(procDefId));
} }
@Operation(summary = "按流程 key 取最新版净化源 XML(超管)",
description = "超管门后转发 flowable-service /api/flow/process-definitions/by-key/{key}/source,"
+ "返回与 by-id 端点同形状。打开器对 process-select 的 source='model' 选项改用真流程 key 走本路"
+ "(该选项 id 是 act_re_model id 非 procDefId,by-id 永远 found:false)。非超管 403。"
+ "中文 key 由 FlowableClient 以 uri 变量严格编码转发。")
@GetMapping("/process-definitions/by-key/{key}/source")
public ResponseEntity<Map<String, Object>> sourceByKey(@AuthenticationPrincipal AuthPrincipal principal,
@PathVariable String key) {
if (!EngineContextBinder.isSystemAdmin(principal)) {
return forbidden();
}
return ResponseEntity.ok(flowableClient.procDefSourceByKey(key));
}
/** 超管门未过:403 + {status:1,msg} 风格对齐既有网关端点。 */ /** 超管门未过:403 + {status:1,msg} 风格对齐既有网关端点。 */
private static ResponseEntity<Map<String, Object>> forbidden() { private static ResponseEntity<Map<String, Object>> forbidden() {
Map<String, Object> body = new LinkedHashMap<>(); Map<String, Object> body = new LinkedHashMap<>();

View File

@@ -235,6 +235,33 @@ class FlowableClientTest {
server.verify(); server.verify();
} }
/**
* by-key 端点:process-select options 的 source='model' 项 id 非 procDefId,打开器改用真流程 key 走此路。
* 纯中文 key「总包合同交底」作为 uri 变量严格编码转发——percent 编码,绝无裸中文。
*/
@Test
void 取流程源按key_纯中文key作为uri变量严格编码不裸传() {
FlowGatewayProperties p = props();
RestClient.Builder builder = RestClient.builder().baseUrl(p.getBaseUrl());
MockRestServiceServer server = MockRestServiceServer.bindTo(builder).build();
String key = "总包合同交底";
server.expect(ExpectedCount.once(), requestTo(allOf(
startsWith("http://localhost:8089/api/flow/process-definitions/by-key/"),
endsWith("/source"),
containsString("%E6%80%BB"), // 「总」的 UTF-8 percent 编码,证明已编码
not(containsString("总包合同交底"))))) // 中文未裸传
.andExpect(method(HttpMethod.GET))
.andRespond(withSuccess("{\"found\":true,\"procDefKey\":\"总包合同交底\",\"version\":2}",
MediaType.APPLICATION_JSON));
FlowableClient client = new FlowableClient(builder.build(), p);
Map<String, Object> resp = client.procDefSourceByKey(key);
assertThat(resp).containsEntry("procDefKey", "总包合同交底");
assertThat(resp).containsEntry("found", true);
server.verify();
}
@Test @Test
void 取流程源_下游4xx透传FlowRemote且不重试() { void 取流程源_下游4xx透传FlowRemote且不重试() {
FlowGatewayProperties p = props(); FlowGatewayProperties p = props();

View File

@@ -82,4 +82,30 @@ class ProcessModelerControllerTest {
.isInstanceOf(FlowServiceUnavailableException.class); .isInstanceOf(FlowServiceUnavailableException.class);
verify(flowableClient).procDefSource(CN_PROCDEF); verify(flowableClient).procDefSource(CN_PROCDEF);
} }
// ---- by-key:process-select options=model 的语义缺口修复,打开器用真流程 key ----
private static final String CN_KEY = "总包合同交底";
@Test
void byKey_超管_透传下游源XML返回200() {
Map<String, Object> downstream = Map.of("found", true, "procDefId", "总包合同交底:2:aa22",
"procDefKey", "总包合同交底", "bpmnXml", "<definitions/>", "idMap", Map.of(), "version", 2);
when(flowableClient.procDefSourceByKey(CN_KEY)).thenReturn(downstream);
ResponseEntity<Map<String, Object>> resp = controller.sourceByKey(admin(), CN_KEY);
assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(resp.getBody()).isEqualTo(downstream);
verify(flowableClient).procDefSourceByKey(CN_KEY);
}
@Test
void byKey_非超管_403且不触发下游调用() {
ResponseEntity<Map<String, Object>> resp = controller.sourceByKey(normalUser(), CN_KEY);
assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
assertThat(resp.getBody()).containsEntry("status", 1).containsEntry("msg", "仅系统管理员可用");
verify(flowableClient, never()).procDefSourceByKey(anyString());
}
} }