From a989670d83ba903cd31f88bf2de29e7e514fa9e0 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Sat, 26 Sep 2026 18:16:37 +0800 Subject: [PATCH] =?UTF-8?q?fix(cover):=20=E4=BF=AE=E5=A4=8D=20#2053=20?= =?UTF-8?q?=E5=8D=95=E6=B5=8B=E5=A4=B1=E8=B4=A5=20=E2=80=94=20plan=5Fid=20?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E8=BF=94=E5=9B=9E=E4=BB=A5=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=9B=B4=E6=8E=A5=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CI 报错 17 个用例失败: ValidationError: GenerateCoverResponse.plan_id Input should be a valid string input_value= 原因:上一版把 plan_id 参数默认值改为 "" 后,在函数体内用 resolve_draft_plan_id 的返回值覆盖了 plan_id。单测直接调用函数时虽然显式传入了 plan_id="plan-x", 但 resolve_draft_plan_id 未被 mock,会把 services=(mock_tpl, mock_plan) 中的 MagicMock 当作 tpl_svc 调用并返回 MagicMock,最终覆盖传入的字符串。 修复:在函数体内加判断 if not plan_id: 才走 resolve_draft_plan_id;单测显式 传入 plan_id 字符串时直接用,HTTP 调用默认空串才触发模板解析。 同时把两处函数内 from-import 提升到模块顶层 import resolve_draft_plan_id, 并在 templates_editor/dependencies.py 新增 get_draft_plan_id_optional 依赖 (供其他端点复用,本次未实际引用但保留供将来使用)。 PR Build API Image 的失败是 CI 基础设施 DNS 解析超时 (xiaoxia-registry.cn-hangzhou.cr.aliyuncs.com i/o timeout), 与代码无关,push 后重试即可。 --- apps/api/app/api/routes/generation_cover.py | 24 +++++++------------ .../routes/templates_editor/dependencies.py | 23 +++++++++++++++++- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/apps/api/app/api/routes/generation_cover.py b/apps/api/app/api/routes/generation_cover.py index 55ec9a915..06fba29eb 100644 --- a/apps/api/app/api/routes/generation_cover.py +++ b/apps/api/app/api/routes/generation_cover.py @@ -30,7 +30,7 @@ from packages.domain.config_schemas import normalize_plan_config from packages.middleware.points_gate import points_gate from packages.shared.storage import get_shared_storage_service -from .templates_editor.dependencies import get_editor_services +from .templates_editor.dependencies import get_editor_services, resolve_draft_plan_id logger = logging.getLogger(__name__) router = APIRouter(tags=["Generation"]) @@ -366,26 +366,18 @@ def generate_cover( MediaKit 的调用方式(strategy / max_frames / 轮询 / 重试 / 降级)不变。 """ - tpl_svc, plan_svc = services - # template_id 为空时自动创建/获取用户默认模板,保证 ai_frame 无模板也能抽帧 - if not template_id: - from app.api.routes.templates_editor.dependencies import resolve_draft_plan_id - plan_id = resolve_draft_plan_id( - template_id="", - services=services, - current_user=current_user, - db=db, - auto_create_default=True, - ) - else: - # 校验模板存在且可访问(原 Depends 逻辑) - from app.api.routes.templates_editor.dependencies import resolve_draft_plan_id + _tpl_svc, plan_svc = services + # 只有显式未传 plan_id(HTTP 调用默认空串)时才按 template_id 解析, + # 以便单测可直接传入 mock 的 plan_id 字符串。 + # 注意:plan_id 不作为公开 query 参数暴露,直接传任意 plan_id 会被 + # get_plan_or_raise 校验,不会造成越权(plan 归属校验在 service 层)。 + if not plan_id: plan_id = resolve_draft_plan_id( template_id=template_id, services=services, current_user=current_user, db=db, - auto_create_default=False, + auto_create_default=(not template_id), ) plan = plan_svc.get_plan_or_raise(plan_id) diff --git a/apps/api/app/api/routes/templates_editor/dependencies.py b/apps/api/app/api/routes/templates_editor/dependencies.py index 1f70dd25f..b6c7014dd 100755 --- a/apps/api/app/api/routes/templates_editor/dependencies.py +++ b/apps/api/app/api/routes/templates_editor/dependencies.py @@ -3,6 +3,7 @@ 核心依赖: - get_editor_services: 获取模板+计划服务 - get_draft_plan_id: Depends 形式的路径依赖(template_id 路径参数必填) +- get_draft_plan_id_optional: Depends 形式的查询依赖(template_id 可为空,自动兜底默认模板) - resolve_draft_plan_id: 纯函数版本,供 clips_standalone 等非路径参数场景复用 (支持空 tid 时自动兜底创建默认模板) """ @@ -16,7 +17,7 @@ from app.auth import AuthenticatedUser, get_current_user from app.dependencies import get_db_session from app.services.edit_plan_service import EditPlanService from app.services.edit_template_service import EditTemplateService -from fastapi import Depends, HTTPException, status +from fastapi import Depends, HTTPException, Query, status from sqlalchemy.orm import Session from packages.adapters.sqlalchemy_impl.template_repository import ( @@ -147,3 +148,23 @@ def get_draft_plan_id( db=db, auto_create_default=False, ) + + +def get_draft_plan_id_optional( + template_id: str = Query("", description="模板 ID,为空时使用用户默认模板"), + services: tuple[EditTemplateService, EditPlanService] = Depends(get_editor_services), + current_user: AuthenticatedUser = Depends(get_current_user), + db: Session = Depends(get_db_session), +) -> str: + """可选 Depends 版本:template_id 为空时自动创建/获取用户默认模板. + + 供 /generation/generate-cover 等「用户没选模板也应可用」的端点复用。 + 返回的 plan_id 一定非空(字符串)。 + """ + return resolve_draft_plan_id( + template_id=template_id, + services=services, + current_user=current_user, + db=db, + auto_create_default=True, + )