fix(workflow): T4 critical lint bugs — potentialOwner array + namespaced assignee (would block valid JNPF workflows)

C1 critical: bpmn-moddle Activity.resources 是 isMany: true flat ResourceRole 数组,
   不存在 resources.potentialOwner 嵌套访问. 修复前对所有 <bpmn:potentialOwner/>
   元素 (BPMN 标准 candidate wrapper pattern) 误判为 no candidate 而阻断.
   修复: filter by $type === 'bpmn:PotentialOwner'.

C2 critical: camunda/flowable extension 属性 (assignee / candidateUsers) 落到 $attrs
   并保留命名空间前缀 ($attrs['camunda:assignee']). 修复前只查 ut.assignee +
   $attrs.assignee, 错失命名空间属性. JNPF/Camunda XML 风格 userTask 全部误判.
   修复: 查 ut.assignee + $attrs.assignee + $attrs['camunda:assignee'] +
   $attrs['flowable:assignee'].

I1: 加 2 回归测试 (Test 5/6) — potentialOwner wrapper 误判 + camunda:assignee 误判
I2: 更新 _semantic_lint.ts:84-93 注释, 反映正确的访问 pattern (filter by $type)
M1: 去掉冗余 (ut as any).resources cast — ut 已 any (filter 上下文)
M3: 加 2 稳定性测试 — 空 XML + 单 End+Gateway 短路验证

注: 同步修正原 Test 3 (userTask 无 assignee 且无 potentialOwner), 因为之前的 XML
含 <bpmn:potentialOwner/>, C1 修复后会视为有候选 → 改为真正的 'no candidate' case.

Test 4 (Test 4 = assign=${deptManager} 阻断) 不受影响, 因为 $vars 用 ut.assignee 直接读,
不会触 $attrs 命名空间路径.

tsc --noEmit 干净; vitest 504 passed (500 旧 + 4 new); bpmn-bs-regression 6/6 绿.

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
sswandClaude committed 2026-07-28 17:25:13 +08:00
1 parent d3f29a49db
commit 6341768f9f
2 files changed
+100 -13

No files matched your search

+22 -8
View File
@@ -12,6 +12,12 @@
* 3. userTask assignee 解析 (空字符串 / ${var} 无法解析)
*
* 设计原则: 在 Save(type=1) 之前阻断, 不让坏 BPMN 进入后端 Flowable 部署阶段.
*
* bpmn-moddle 解析注意:
* - Activity.resources 是 isMany: true 的 flat ResourceRole 数组 (bpmn-moddle schema).
* 不存在 resources.potentialOwner 这种嵌套; 需 filter by $type === 'bpmn:PotentialOwner'.
* - camunda/flowable extension 属性 (assignee / candidateUsers / candidateGroups) 不在
* moddle schema 内, 会落到 $attrs 并保留命名空间前缀 ($attrs['camunda:assignee']).
*/
import { BpmnModdle } from 'bpmn-moddle'
@@ -83,15 +89,23 @@ export async function semanticLint(xml: string, ctx: ResolverCtx): Promise<Seman
// 3. userTask assignee / potentialOwner 解析
// 注: bpmn-moddle 不识别 camunda/flowable extension 属性 (assignee / candidateUsers),
// 这些属性会落到 ut.$attrs 而不是 ut.assignee. 需要两个地方都查.
// 这些属性会落到 ut.$attrs 并保留命名空间前缀 ($attrs['camunda:assignee']).
// 需要查多个命名空间才能覆盖 JNPF/Camunda/Flowable 三种 XML 风格.
// 注: Activity.resources 是 flat ResourceRole 数组 (bpmn-moddle schema isMany=true),
// 不存在 resources.potentialOwner 嵌套; 需 filter by $type === 'bpmn:PotentialOwner'.
for (const ut of userTasks) {
const assignee = String(ut.assignee || ut.$attrs?.assignee || '').trim()
// bpmn-moddle 解析 userTask 时, potentialOwner 在 ut.resources?.potentialOwner
// (如果 XML 用 camunda/ns 命名空间, 也可能在 ut.$attrs, 这里按 bpmn-moddle 标准处理)
const resources = (ut as any).resources
const po: any[] = resources?.potentialOwner || []
const $attrs = ut.$attrs || {}
const assignee = String(
ut.assignee ||
$attrs.assignee ||
$attrs['camunda:assignee'] ||
$attrs['flowable:assignee'] ||
'',
).trim()
// 3a. 缺 assignee 且缺 candidate → 阻断
const po = (ut.resources || []).filter((r: any) => r.$type === 'bpmn:PotentialOwner')
// 3a. 缺 assignee 且缺 potentialOwner → 阻断
if (!assignee && po.length === 0) {
errors.push(`userTask ${ut.id}: missing assignee and no candidate`)
diagnostics.push({ nodeId: ut.id, message: 'no assignee or candidate' })
@@ -119,4 +133,4 @@ export async function semanticLint(xml: string, ctx: ResolverCtx): Promise<Seman
function hasConvergingGateway(flowEls: any[], ends: any[]): boolean {
if (ends.length <= 1) return true
return flowEls.some((e: any) => String(e.$type || '').includes('Gateway'))
}
}
+78 -5
View File
@@ -1,12 +1,20 @@
/**
* workflow semantic lint tests (Task 4 — webapi hardening)
*
* 4 场景:
* 原始 4 场景 (T4):
* 1. 多结束节点且无合并 → 阻断
* 2. 无 start → 阻断
* 3. userTask assignee 空字符串 → 阻断
* 3. userTask 无 assignee 且无 potentialOwner → 阻断
* 4. 显式 ${deptManager} 无法解析 → 阻断并 diagnostics
*
* T4 critical fix 回归 (C1/C2):
* 5. userTask assignee="" + <bpmn:potentialOwner/> → ok=true (C1: potentialOwner array)
* 6. userTask camunda:assignee="alice" → ok=true (C2: namespaced assignee)
*
* M3 稳定性测试:
* 7. 空 XML → errors 含 'flowXml is empty'
* 8. 单 End + Gateway → ok=true (短路验证)
*
* 与 BPMN layout lint 分离: layout 自动修复不掩盖语义错误.
*/
import { describe, expect, it } from 'vitest'
@@ -40,12 +48,15 @@ describe('workflow semantic lint (Task 4)', () => {
expect(r.ok).toBe(false)
})
it('userTask assignee 空字符串 → ok=false', async () => {
it('userTask 无 assignee 且无 potentialOwner → ok=false', async () => {
// 测试 "真的没候选人" 的情况: 既没有 assignee 属性, 也没有 potentialOwner 元素.
// (T4 critical fix 之前, 这个 XML 含 <bpmn:potentialOwner/>, 但用错误的 resources?.potentialOwner 访问
// 一直拿到空数组, 误判为 "no candidate" 而阻断; 修复后 potentialOwner 元素被识别, 此测试改成真无候选人.)
const xml = `<?xml version="1.0"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL">
<bpmn:process id="p1">
<bpmn:startEvent id="s1"/>
<bpmn:userTask id="t1"><bpmn:potentialOwner id="po1"/></bpmn:userTask>
<bpmn:userTask id="t1"/>
<bpmn:endEvent id="e1"/>
</bpmn:process>
</bpmn:definitions>`
@@ -67,4 +78,66 @@ describe('workflow semantic lint (Task 4)', () => {
expect(r.ok).toBe(false)
expect(r.diagnostics.some(d => /unresolved/.test(d.message))).toBe(true)
})
})
// ─── T4 critical fix 回归 (C1: potentialOwner array 访问模式) ─────────────
it('C1 回归: userTask 无 assignee + <bpmn:potentialOwner/> → ok=true', async () => {
// BPMN 标准 pattern: <bpmn:potentialOwner> 元素表达 "存在候选人 wrapper"
// (具体用户在 <bpmn:resourceAssignmentExpression> 里, 这里只看 potentialOwner 元素是否存在)
// 修复前: resources?.potentialOwner → undefined → 错判为 "no candidate"
// 修复后: resources.filter($type === 'bpmn:PotentialOwner') → 1 → 视为有候选
const xml = `<?xml version="1.0"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL">
<bpmn:process id="p1">
<bpmn:startEvent id="s1"/>
<bpmn:userTask id="t1"><bpmn:potentialOwner id="po1"/></bpmn:userTask>
<bpmn:endEvent id="e1"/>
</bpmn:process>
</bpmn:definitions>`
const r = await semanticLint(xml, { candidateResolver: () => [] })
expect(r.ok).toBe(true)
expect(r.errors).toEqual([])
})
// ─── T4 critical fix 回归 (C2: namespaced assignee) ─────────────────────
it('C2 回归: userTask camunda:assignee="alice" → ok=true', async () => {
// JNPF/Camunda XML 风格: userTask 用命名空间前缀属性 camunda:assignee
// 修复前: 只查 ut.assignee + $attrs.assignee → 错失命名空间属性
// 修复后: 查 ut.assignee + $attrs.assignee + $attrs['camunda:assignee'] + $attrs['flowable:assignee']
const xml = `<?xml version="1.0"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:camunda="http://camunda.org/schema/1.0/bpmn">
<bpmn:process id="p1">
<bpmn:startEvent id="s1"/>
<bpmn:userTask id="t1" camunda:assignee="alice"/>
<bpmn:endEvent id="e1"/>
</bpmn:process>
</bpmn:definitions>`
const r = await semanticLint(xml, { candidateResolver: () => [] })
expect(r.ok).toBe(true)
expect(r.errors).toEqual([])
})
// ─── M3 稳定性测试 ─────────────────────────────────────────────────────
it('M3 稳定性: 空 XML → errors 含 "flowXml is empty"', async () => {
const r = await semanticLint('', { candidateResolver: () => [] })
expect(r.ok).toBe(false)
expect(r.errors).toContain('flowXml is empty')
})
it('M3 稳定性: 单 End + Gateway → ok=true (短路验证)', async () => {
// ends.length === 1 时 hasConvergingGateway 短路返回 true, 不会触发 multiple-end
const xml = `<?xml version="1.0"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL">
<bpmn:process id="p1">
<bpmn:startEvent id="s1"/>
<bpmn:userTask id="t1"/>
<bpmn:exclusiveGateway id="g1"/>
<bpmn:userTask id="t2"/>
<bpmn:endEvent id="e1"/>
</bpmn:process>
</bpmn:definitions>`
const r = await semanticLint(xml, { candidateResolver: () => [] })
// 两个 userTask 都没 assignee/po → 但 ends.length=1 + Gateway → single 短路 → only userTask errors
expect(r.errors.some(e => /multiple-end/.test(e))).toBe(false)
})
})