根因: JNPF 后端 VisualdevModelDataCrForm.data 是 String 字段,
controller line 527 调 JsonUtil.stringToMap(data) 转 Map, 当 data 为 null 时
map.get(vModel) 抛 NPE → 'Cannot invoke "Map.get(Object)" because "map" is null'.
web 行为 (CustomForm.vue:78):
state.dataForm.data = JSON.stringify(formData)
createModel(modelId, state.dataForm)
dacli 之前直接 c.postJson('/visualdev/OnlineDev/{modelId}', payload) 发原始对象,
后端 data 字段为 null, NPE, 返 400 'System abnormality'.
修复: core/client.ts 加 2 个 wrapper 方法 (postJsonOnlineDevCreate / putJsonOnlineDevUpdate),
内部包成 { data: JSON.stringify(payload) }. 现有 postJson/putJson 不动, 向后兼容.
实测: jxcj app / jxcj_competition_info (id=838880598830350405)
Create 200 New created successfully (新 id=838902476441649221)
Update 200 update success
Delete 200 Delete successfully
单测 4/4 PASS (create 包装 / 空 payload / update 包装 / postJson 不包装回归)
应用: docs/jxcj 后续 CRUD TC 可直接用新 wrapper.
Co-Authored-By: Claude <[email protected]>
132 lines
4.4 KiB
TypeScript
132 lines
4.4 KiB
TypeScript
/**
|
|
* unit/online_dev_crud.test.ts — OnlineDev CRUD body 包装单测 (BS-53)
|
|
*
|
|
* 覆盖: JnpfClient.postJsonOnlineDevCreate / putJsonOnlineDevUpdate 必须把 payload 包成
|
|
* `{ data: JSON.stringify(payload) }`, 否则 JNPF 后端 VisualdevModelDataCrForm.data
|
|
* (private String) 反序列化失败, 报 "System abnormality".
|
|
*
|
|
* 策略: vi.stubGlobal('fetch', ...) 拦截实际 HTTP 请求, 验证 fetch 被以正确 body 调用。
|
|
*/
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|
import { JnpfClient } from '../../src/core/client.ts'
|
|
|
|
interface CapturedCall {
|
|
url: string
|
|
method: string
|
|
body: unknown
|
|
headers: Record<string, string>
|
|
}
|
|
|
|
let lastCall: CapturedCall | null = null
|
|
let stubbed: ReturnType<typeof vi.fn> | null = null
|
|
|
|
beforeEach(() => {
|
|
lastCall = null
|
|
stubbed = vi.fn(async (url: string, init?: RequestInit) => {
|
|
const method = init?.method || 'GET'
|
|
let body: unknown = null
|
|
if (typeof init?.body === 'string') {
|
|
try {
|
|
body = JSON.parse(init.body)
|
|
} catch {
|
|
body = init.body
|
|
}
|
|
}
|
|
const headers: Record<string, string> = {}
|
|
if (init?.headers) {
|
|
for (const [k, v] of Object.entries(init.headers as Record<string, string>)) {
|
|
headers[k] = v
|
|
}
|
|
}
|
|
lastCall = { url, method, body, headers }
|
|
// 返一个最小 JnpfResp 模拟; 让 code=200, data 指向 record id
|
|
return {
|
|
text: async () => JSON.stringify({ code: 200, msg: 'ok', data: 'mock-id-123' }),
|
|
} as Response
|
|
})
|
|
vi.stubGlobal('fetch', stubbed)
|
|
})
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
function makeClient(): JnpfClient {
|
|
const c = new JnpfClient({
|
|
baseUrl: 'http://127.0.0.1:30000/api',
|
|
appCode: 'jxcj',
|
|
account: 'admin',
|
|
password: 'x',
|
|
})
|
|
c.token = 'mock-token'
|
|
return c
|
|
}
|
|
|
|
describe('JnpfClient.postJsonOnlineDevCreate', () => {
|
|
it('把 payload 包成 { data: JSON.stringify(payload) }, POST 到 /visualdev/OnlineDev/{modelId}', async () => {
|
|
const c = makeClient()
|
|
const MODEL = '838880598830350405'
|
|
const payload = {
|
|
f_competition_name: 'FixTest-2026',
|
|
f_organizer: 'OrgFix',
|
|
f_state: 'published',
|
|
}
|
|
|
|
const r = await c.postJsonOnlineDevCreate(MODEL, payload)
|
|
|
|
expect(r.code).toBe(200)
|
|
expect(lastCall).not.toBeNull()
|
|
expect(lastCall!.method).toBe('POST')
|
|
expect(lastCall!.url).toBe(`http://127.0.0.1:30000/api/visualdev/OnlineDev/${MODEL}`)
|
|
// body 必须 = { data: '{"f_competition_name":"FixTest-2026",...}' }
|
|
const body = lastCall!.body as { data: string }
|
|
expect(typeof body).toBe('object')
|
|
expect(typeof body.data).toBe('string')
|
|
expect(body.data).toBe(JSON.stringify(payload))
|
|
// 反向解析后字段一致 (data 是字符串化的 JSON, 不是字符串嵌套字符串)
|
|
const inner = JSON.parse(body.data)
|
|
expect(inner).toEqual(payload)
|
|
expect(inner.f_competition_name).toBe('FixTest-2026')
|
|
})
|
|
|
|
it('空 payload 也包成 { data: "{}" }', async () => {
|
|
const c = makeClient()
|
|
const MODEL = '838880598830350405'
|
|
await c.postJsonOnlineDevCreate(MODEL, {})
|
|
const body = lastCall!.body as { data: string }
|
|
expect(body.data).toBe('{}')
|
|
expect(JSON.parse(body.data)).toEqual({})
|
|
})
|
|
})
|
|
|
|
describe('JnpfClient.putJsonOnlineDevUpdate', () => {
|
|
it('把 payload 包成 { data: JSON.stringify(payload) }, PUT 到 /visualdev/OnlineDev/{modelId}/{id}', async () => {
|
|
const c = makeClient()
|
|
const MODEL = '838880598830350405'
|
|
const REC_ID = 'rec-abc-123'
|
|
const payload = {
|
|
f_competition_name: 'FixTest-Updated',
|
|
f_state: 'published',
|
|
}
|
|
|
|
const r = await c.putJsonOnlineDevUpdate(MODEL, REC_ID, payload)
|
|
|
|
expect(r.code).toBe(200)
|
|
expect(lastCall!.method).toBe('PUT')
|
|
expect(lastCall!.url).toBe(`http://127.0.0.1:30000/api/visualdev/OnlineDev/${MODEL}/${REC_ID}`)
|
|
const body = lastCall!.body as { data: string }
|
|
expect(body.data).toBe(JSON.stringify(payload))
|
|
const inner = JSON.parse(body.data)
|
|
expect(inner.f_competition_name).toBe('FixTest-Updated')
|
|
})
|
|
})
|
|
|
|
describe('向后兼容 (postJson / putJson 仍可裸用)', () => {
|
|
it('postJson 不做 {data: ...} 包装 (裸 body)', async () => {
|
|
const c = makeClient()
|
|
await c.postJson('/some/other/api', { foo: 'bar' })
|
|
const body = lastCall!.body as { foo: string }
|
|
expect(body.foo).toBe('bar')
|
|
expect((body as Record<string, unknown>).data).toBeUndefined()
|
|
})
|
|
}) |