fix: cover_templates config double-encoded JSON causing 500 #1363
Reference in New Issue
Block a user
Delete Branch "fix/cover-templates-double-encoded-config"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
GET /api/v1/cover-templatesreturns 500 Internal Server Error.Root cause: Migration 055 seed data used
json.dumps(config)which double-serialized the config dict.SQLAlchemy
JSONcolumn already handles serialization automatically.json.dumps()turned dicts into strings first, then the JSON column serialized them again, resulting in JSON string scalars stored in the database:When Pydantic
CoverTemplateResponsereads this,configis a string"{}"instead of a dict{}, causingValidationError-> 500.Fix
json.dumps(), pass dict directly to SQLAlchemy JSON columnDatabase fix already applied
The data fix has been manually applied to staging. This PR ensures consistency for future deployments.
Root cause: migration 055 seed data used json.dumps(config) which double-serialized the config dict. SQLAlchemy JSON column already handles serialization, so json.dumps() turned dicts into strings, then JSON column serialized them again into JSON string scalars. Example: {} -> json.dumps -> "{}" -> JSON column -> "\"{}\"" When Pydantic CoverTemplateResponse reads this, it gets a string instead of a dict, causing ValidationError -> 500. Fix: 1. migration 055: remove json.dumps(), pass dict directly 2. migration 056: fix existing data by extracting text from JSON string scalar and casting back to JSON object🚀 预览环境已部署
【阻塞级判定】
📊 审查概览
🔴 阻塞级问题(必须修复)
056仅针对postgresql方言进行了处理(if conn.dialect.name == "postgresql":)。如果项目支持 MySQL 或 SQLite 等其他数据库,由055(旧版本)引起的双重序列化问题将无法被修复。这会导致在这些数据库上,config字段依然保持为 JSON 字符串(如"{}")而非 JSON 对象(如{}),从而引发应用层(如 Pydantic 模型)校验失败或运行时错误。assert conn.dialect.name == "postgresql")以防止在非 PG 环境下静默跳过修复。JSON_UNQUOTE(JSON_EXTRACT(config, '$'))或类似的逻辑来处理转义。💡 改进建议(不阻塞合并)
无
✅ 良好实践
json.dumps,利用 SQLAlchemy 的JSON类型自动处理序列化,避免了手动序列化导致的类型错误。jsonb_typeof和#>>操作符,准确地识别并修复了被双重序列化的字符串数据,逻辑针对性强。🤖 由 AI 代码审查机器人自动生成 | 2026-08-13 15:38:46 | 模型:
CI全绿,自动审批通过。
CI全绿,自动审批通过。
🗑️ 预览环境已清理
PR #1363 已关闭或合并,对应的预览环境已被清理。