fix: 音频上传422错误 — create_asset project_id改为可选,从library自动推导 #1260

Merged
xiaoxia merged 2 commits from fix/asset-create-optional-project-id into develop 2026-08-07 17:12:37 +08:00
Owner

问题

前端调用 POST /api/v1/assets 创建配音素材时返回 422,因为 CreateAssetRequest 要求 project_id 必填,但前端没传。

修复

  • apps/api/app/schemas/asset.pyproject_id 从必填改为可选 str | None = Field(default=None)
  • apps/api/app/api/routes/assets.pycreate_asset 函数中先获取 library,再从 library.project_id 自动推导 project_id
    • 优先用请求值,不传则从 library 关联的项目推导
    • 增加 library 与 project 归属一致性校验

测试

新增 4 个单元测试:

  • 不传 project_id 时从 library 自动推导
  • 显式传 project_id 时优先使用请求值
  • library 不存在返回 404
  • library 与 project 不匹配返回 400

全量 asset 相关测试 40 个全过。

## 问题 前端调用 POST /api/v1/assets 创建配音素材时返回 422,因为 CreateAssetRequest 要求 project_id 必填,但前端没传。 ## 修复 - `apps/api/app/schemas/asset.py`:`project_id` 从必填改为可选 `str | None = Field(default=None)` - `apps/api/app/api/routes/assets.py`:`create_asset` 函数中先获取 library,再从 `library.project_id` 自动推导 project_id - 优先用请求值,不传则从 library 关联的项目推导 - 增加 library 与 project 归属一致性校验 ## 测试 新增 4 个单元测试: - 不传 project_id 时从 library 自动推导 - 显式传 project_id 时优先使用请求值 - library 不存在返回 404 - library 与 project 不匹配返回 400 全量 asset 相关测试 40 个全过。
xiaoxia added 1 commit 2026-08-07 14:50:17 +08:00
fix: make project_id optional in create_asset endpoint
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 36s
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 55s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 1m8s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m42s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 1m42s
AI Code Review / AI Code Review (pull_request) Successful in 2m3s
CI/CD Pipeline / Validate - Code Quality (pull_request) Has been cancelled
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / PR Build API Image (pull_request) Has been cancelled
CI/CD Pipeline / PR Build Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / CI Gate (pull_request) Has been cancelled
PR Automation / Auto Approve on CI Green (pull_request) Has been cancelled
723f9d1531
- Change CreateAssetRequest.project_id from required to optional
- Auto-derive project_id from library.project_id when not provided
- Reorder validation: fetch library first, then derive project_id
- Add 4 unit tests covering all scenarios
- Fixes 422 error when frontend creates asset without project_id

🚀 预览环境已部署

项目 详情
PR号 #1260
预览链接 https://pr-1260.preview.xiaoxiajianji.com
API环境 staging

💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。

🔄 每次提交新代码后预览环境会自动更新。

🗑️ PR 关闭或合并后,预览环境会自动清理。

🚀 **预览环境已部署** | 项目 | 详情 | |------|------| | PR号 | #1260 | | 预览链接 | [https://pr-1260.preview.xiaoxiajianji.com](https://pr-1260.preview.xiaoxiajianji.com) | | API环境 | staging | > 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。 > > 🔄 每次提交新代码后预览环境会自动更新。 > > 🗑️ PR 关闭或合并后,预览环境会自动清理。
auto-approve-bot added 1 commit 2026-08-07 14:53:19 +08:00
style: auto-format with black + isort + prettier [skip ci-format-check]
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 37s
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 54s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 1m12s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m24s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 1m36s
AI Code Review / AI Code Review (pull_request) Successful in 1m39s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m43s
CI/CD Pipeline / Validate - Code Quality (pull_request) Successful in 4m20s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 5m37s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 5m37s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 2m24s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 9m36s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Successful in 6s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 1m18s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Successful in 1m22s
4487f20c06
Collaborator

【阻塞级判定】

  • 是否存在阻塞级问题:否
  • 阻塞级问题数量:0 个

📊 审查概览

  • 整体评价:通过
  • 建议级问题数量:1 个

🔴 阻塞级问题(必须修复)

💡 改进建议(不阻塞合并)

  1. [apps/api/app/api/routes/assets.py: 679] 潜在的空值 ID 传递风险
    • 具体内容:代码逻辑中 project_id = request.project_id or library.project_id。虽然通常 AssetLibrary 必须关联 Project,但如果数据库中 library.project_id 允许为 NULL 且恰好为 NULL,project_id 将为 None。此时调用 project_repository.find_by_id(None) 可能会导致数据库查询异常(如语法错误)或返回意外的结果。建议在调用 find_by_id 前增加非空校验,确保数据完整性。
    • 修改建议:
      if not project_id:
          raise HTTPException(status_code=400, detail="Project ID cannot be determined")
      

良好实践

  1. 逻辑清晰:将 project_id 的推导逻辑(请求值优先,回退到 library 关联值)处理得非常清晰。
  2. 安全性增强:新增了 library.project_id != project_id 的校验,并返回了更符合语义的 400 错误(原代码在 ID 不匹配时返回 404,容易误导),防止了跨项目操作的潜在风险。
  3. 测试覆盖充分:单元测试覆盖了自动推导、显式指定、资源不存在、ID 冲突等核心场景,保证了代码变更的可靠性。

🤖 由 AI 代码审查机器人自动生成 | 2026-08-07 06:54:59 | 模型:

### 【阻塞级判定】 - 是否存在阻塞级问题:否 - 阻塞级问题数量:0 个 ### 📊 审查概览 - 整体评价:通过 - 建议级问题数量:1 个 ### 🔴 阻塞级问题(必须修复) 无 ### 💡 改进建议(不阻塞合并) 1. **[apps/api/app/api/routes/assets.py: 679] 潜在的空值 ID 传递风险** - 具体内容:代码逻辑中 `project_id = request.project_id or library.project_id`。虽然通常 AssetLibrary 必须关联 Project,但如果数据库中 `library.project_id` 允许为 NULL 且恰好为 NULL,`project_id` 将为 None。此时调用 `project_repository.find_by_id(None)` 可能会导致数据库查询异常(如语法错误)或返回意外的结果。建议在调用 `find_by_id` 前增加非空校验,确保数据完整性。 - 修改建议: ```python if not project_id: raise HTTPException(status_code=400, detail="Project ID cannot be determined") ``` ### ✅ 良好实践 1. **逻辑清晰**:将 `project_id` 的推导逻辑(请求值优先,回退到 library 关联值)处理得非常清晰。 2. **安全性增强**:新增了 `library.project_id != project_id` 的校验,并返回了更符合语义的 400 错误(原代码在 ID 不匹配时返回 404,容易误导),防止了跨项目操作的潜在风险。 3. **测试覆盖充分**:单元测试覆盖了自动推导、显式指定、资源不存在、ID 冲突等核心场景,保证了代码变更的可靠性。 --- <sub>🤖 由 AI 代码审查机器人自动生成 | 2026-08-07 06:54:59 | 模型: </sub> <!-- AI_CODE_REVIEW_AUTO_COMMENT -->
xiaoxia merged commit 4f323b394b into develop 2026-08-07 17:12:37 +08:00

🗑️ 预览环境已清理

PR #1260 已关闭或合并,对应的预览环境已被清理。

如有需要,可以重新打开 PR 来重新生成预览环境。

🗑️ **预览环境已清理** PR #1260 已关闭或合并,对应的预览环境已被清理。 > 如有需要,可以重新打开 PR 来重新生成预览环境。
Sign in to join this conversation.