refactor(契约): samples / answers / testCaseScore 补回旧后端的校验
Some checks failed
Deploy / deploy (push) Has been cancelled
Some checks failed
Deploy / deploy (push) Has been cancelled
旧后端这三个字段都是逐字段校验的(CreateSampleSerializer /
CreateAnswerSerializer / CreateTestCaseScoreSerializer),新后端一路写成
`z.record(z.string(), z.unknown())`,**比旧的松**。松出来的不只是少拦几个
错误请求,还有一处静默的落库形状漂移。
## test_case_score 会越存越胖,而且 score 变成字符串
上传接口的响应有六个键,前端 `{...entry, score}` 原样往回传。旧后端的 DRF
serializer 只认 input_name / output_name / score,多的直接丢,所以生产库
956 行 test_case_score **全部只有三个键**、7990 条 score 全部是 int
(前端算出来是 `(100/n).toFixed(0)` 这种字符串,IntegerField 收下时转了)。
新后端不做这件事,往后在 OJ2 上新建或编辑的题会存六个键、score 是字符串 ——
和旧后端写出来的不是一个形状。这列判题机不读,不影响判题,但它是回滚要
原样交回去的持久化数据,不该在这上面分叉。
现在三个精确 schema 顶上:problemSampleSchema / problemAnswerSchema /
problemTestCaseScoreSchema。zod 的 object 默认剥未知键,和 DRF 同一个行为;
score 用 `z.coerce.number().int().min(0)`,对齐 IntegerField 的收字符串转整数。
前端跟着改:`Testcase` 从 `TestCaseEntry & { score: string }`(六键+字符串分数)
换成契约的 ProblemTestCaseScore(三键+整数),三处构造点只挑落库要的键。
detail.vue 那处写成 `Number((100 / n).toFixed(0))` —— 取值和原来逐字相同,
只是不再包成字符串,分数算法一个字没动。AdminProblem 的 Omit 列表也短了两项
(samples / testCaseScore 现在契约里就是准的),只剩 answers 要把 language
收窄成 LANGUAGE。
## 顺带:一段被 @ts-ignore 压着的死代码
admin/problem/detail.vue 上传测试点那里有
// @ts-ignore
if (res.error) { ... }
—— 拿 { error, data } 信封当返回值判。上一个 commit 拆信封时正是因为
@ts-ignore 压着,vue-tsc 没报出来。res 现在是 UploadTestCaseResponse,
没有 error 这个键,这个分支永远进不去。失败本来就走 catch。
(全仓另外两处 @ts-ignore 查过了,是 skulpt 和 wangeditor 没类型定义,正常。)
## 生产数据依据
956 道题逐条扫过备份:samples 947 条 {input,output} + 9 条空数组(SQL 题没
样例);answers 268 条 {code,language} + 633 null + 55 空数组;
test_case_score 956 条全是三键,score 全 int。收紧不会打到任何存量行。
## 验证
tsc(apps/api) 0 error、check:routes 168 条无遮蔽、vue-tsc 0 error、build 通过。
起服务实打了写路径:
- 后台题目详情 200,三个 schema 都 parse 得过存量数据。
- 故意造脏 PUT:score 传字符串 "20" + 塞进 stripped_output_md5 / input_size /
output_size,落库是干净的 `{input_name, output_name, score: 20(int)}`;
samples 和 answers 里塞的多余键同样被剥掉。
- 三个错误载荷都按预期 400:samples 缺 output、answers 缺 code、score 传负数。
- 浏览器里打开后台题目编辑页,点提交 → PUT 200 → 跳回列表,库里形状正确。
(顺带发现 tags 为空的题在编辑页点提交会被 `tags.min(1)` 挡下 400 —— 旧后端
`allow_empty=False` 也是这个行为,不在本次范围。)
冒烟改动已还原:problem 2 的 answers 复位成 [],测试用的「冒烟」标签删掉。
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -101,10 +101,12 @@ export type {
|
||||
export type { UploadTestCaseResponse } from "@oj2/contract"
|
||||
|
||||
/**
|
||||
* 题目表单里的测试点:上传返回的条目 + 前端本地算出的分值。
|
||||
* `score` **不在响应里** —— 是上传完成后按测试点数量平分补上去的。
|
||||
* 题目表单里的测试点。落库的只有 {input_name, output_name, score} 三个键 ——
|
||||
* 上传响应里多出来的 stripped_output_md5 / input_size / output_size 在保存时
|
||||
* 被剥掉(旧后端的 serializer 也是这么干的,见契约 problemTestCaseScoreSchema)。
|
||||
* score 不在上传响应里,是上传完成后按测试点数量平分补上去的。
|
||||
*/
|
||||
export type Testcase = TestCaseEntry & { score: string }
|
||||
export type { ProblemTestCaseScore as Testcase } from "@oj2/contract"
|
||||
|
||||
/**
|
||||
* 题目详情。以契约的 ProblemDetail 为准,只在这里补两处前端自己的窄化:
|
||||
@@ -140,18 +142,11 @@ export type {
|
||||
/** 后台题目详情:比 oj 侧多 answers / testCase* / astRules */
|
||||
export type AdminProblem = Omit<
|
||||
ContractAdminProblem,
|
||||
| "languages"
|
||||
| "template"
|
||||
| "testCaseScore"
|
||||
| "samples"
|
||||
| "answers"
|
||||
| "astRules"
|
||||
"languages" | "template" | "answers" | "astRules"
|
||||
> & {
|
||||
languages: LANGUAGE[]
|
||||
template: { [key in LANGUAGE]?: string }
|
||||
// 测试点条目的键名由判题沙箱定,保持 snake_case
|
||||
testCaseScore: Testcase[]
|
||||
samples: { input: string; output: string }[]
|
||||
// 契约里 answers[].language 是 string,这里收窄成 LANGUAGE
|
||||
answers: { language: LANGUAGE; code: string }[]
|
||||
astRules?: AstRules | null
|
||||
}
|
||||
@@ -470,7 +465,6 @@ import type {
|
||||
AdminExercise,
|
||||
AdminTutorial,
|
||||
CreateSubmissionRequest,
|
||||
TestCaseEntry,
|
||||
} from "@oj2/contract"
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user