From 38d6f47e678517aee49c54495b5641d775930125 Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Tue, 13 Oct 2026 16:48:46 +0800 Subject: [PATCH] =?UTF-8?q?PUT=20/catalog/guidance=20=E7=9A=84=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E6=B2=A1=E5=86=99=20guidance=E3=80=81=E6=88=96?= =?UTF-8?q?=E8=80=85=E4=B8=8D=E6=98=AF=E4=B8=80=E6=AE=B5=E6=96=87=E5=AD=97?= =?UTF-8?q?=E6=97=B6=E5=9B=9E=20422=20=E5=B8=A6=E4=B8=80=E5=8F=A5=E4=B8=AD?= =?UTF-8?q?=E6=96=87=E8=AF=B4=E6=98=8E=EF=BC=88=E5=90=8C=E8=B6=85=E9=95=BF?= =?UTF-8?q?=E6=97=B6=E7=9A=84=E5=86=99=E6=B3=95=EF=BC=89=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E5=86=8D=E6=98=AF=20pydantic=20=E7=9A=84=E8=8B=B1=E6=96=87?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E7=BB=93=E6=9E=9C=EF=BC=9B=E8=A1=A8=E5=90=8D?= =?UTF-8?q?=E6=81=B0=E5=A5=BD=E5=8F=AB=20guidance=20=E7=9A=84=E6=95=B4?= =?UTF-8?q?=E4=BB=BD=E6=8F=90=E4=BA=A4=E7=85=A7=E6=97=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/catalog.py | 20 +++++++++++--------- backend/tests/test_catalog_draft_jobs.py | 6 +++++- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/backend/app/api/catalog.py b/backend/app/api/catalog.py index 46362b9b..a0945853 100644 --- a/backend/app/api/catalog.py +++ b/backend/app/api/catalog.py @@ -85,10 +85,11 @@ class DraftJobIn(BaseModel): note: str | None = None -class GuidanceIn(BaseModel): - #: 数据源级的起草要求;空串(或 null)清除。最多 2000 字(超了 422,说明是中文)。必须写这一项:表名恰好叫 - #: guidance 时,整份提交那张表目录的请求也落到这个路径上,不能把它当成「清除起草要求」 - guidance: str | None +#: PUT /catalog/guidance 的请求 {guidance: string}:数据源级的起草要求,空串(或 null)清除,最多 2000 字。必须写 +#: 这一项:表名恰好叫 guidance 时,整份提交那张表目录的请求也落到这个路径上,不能把它当成「清除起草要求」。 +#: 没写、不是文字都回 422,说明是中文的一句话(同超长) +GUIDANCE_MISSING = "请求里要写起草要求(guidance 一项,一段文字;要清除起草要求请传空串)" +GUIDANCE_NOT_TEXT = "起草要求要是一段文字(要清除起草要求请传空串)" class CatalogProfileIn(BaseModel): @@ -260,13 +261,14 @@ async def put_catalog_guidance(source_id: str, body: dict[str, Any] = Body(...), except ValidationError as e: raise RequestValidationError(e.errors()) from e return await put_catalog_table(source_id, "guidance", table_payload, x_actor=x_actor, session=session) - try: - payload = GuidanceIn.model_validate(body) - except ValidationError as e: - raise RequestValidationError(e.errors()) from e + if "guidance" not in body: + raise HTTPException(422, GUIDANCE_MISSING) + guidance = body["guidance"] + if guidance is not None and not isinstance(guidance, str): + raise HTTPException(422, GUIDANCE_NOT_TEXT) row = await _source_row(session, source_id) try: - value = await catalog_jobs.save_guidance(session, row, payload.guidance) + value = await catalog_jobs.save_guidance(session, row, guidance) except catalog_jobs.TextTooLong as e: raise HTTPException(422, str(e)) from e return {"guidance": value} diff --git a/backend/tests/test_catalog_draft_jobs.py b/backend/tests/test_catalog_draft_jobs.py index d766532b..a8a961bb 100644 --- a/backend/tests/test_catalog_draft_jobs.py +++ b/backend/tests/test_catalog_draft_jobs.py @@ -541,7 +541,11 @@ async def test_a_table_named_guidance_can_still_be_saved(client): assert r.status_code == 200, r.text assert r.json()["table_name"] == "guidance" and r.json()["notes"]["label"]["value"] == "操作指引" assert (await client.get(_base(sid))).json()["guidance"] == "金额按元" - assert (await client.put(f"{_base(sid)}/guidance", json={})).status_code == 422 + for body in ({}, {"guidance": 3}, {"guidance": ["金额按元"]}): + r = await client.put(f"{_base(sid)}/guidance", json=body) + assert r.status_code == 422, body + assert isinstance(r.json()["detail"], str) and "起草要求" in r.json()["detail"], "说明是中文的一句话" + assert (await client.get(_base(sid))).json()["guidance"] == "金额按元" async def test_guidance_of_an_uploaded_source_lives_on_its_own_row(client):