From 946312cb6e188929f34cbbdeb60744d051fa19ce Mon Sep 17 00:00:00 2001 From: chengkai3 Date: Tue, 16 Jun 2026 17:03:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:[FL-143][=E5=A4=84=E7=90=86=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=9F=A5=E7=9C=8B=E6=8A=A5=E9=94=99]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: multica-agent --- api/app/schemas/system_message.py | 4 +++- api/tests/test_system_message_schema.py | 25 +++++++++++++++++++++++++ memory/2026-06-16.md | 16 ++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 api/tests/test_system_message_schema.py create mode 100644 memory/2026-06-16.md diff --git a/api/app/schemas/system_message.py b/api/app/schemas/system_message.py index f32c73f..fc66f4a 100644 --- a/api/app/schemas/system_message.py +++ b/api/app/schemas/system_message.py @@ -1,10 +1,12 @@ from datetime import datetime from typing import Literal -from pydantic import BaseModel, Field +from pydantic import BaseModel, ConfigDict, Field class SystemMessagePublic(BaseModel): + model_config = ConfigDict(from_attributes=True) + id: str title: str content: str diff --git a/api/tests/test_system_message_schema.py b/api/tests/test_system_message_schema.py new file mode 100644 index 0000000..73f87b0 --- /dev/null +++ b/api/tests/test_system_message_schema.py @@ -0,0 +1,25 @@ +from __future__ import annotations + +from datetime import datetime +from types import SimpleNamespace + +from app.schemas.system_message import SystemMessagePublic + + +def test_system_message_public_validates_attribute_object() -> None: + message = SimpleNamespace( + id="message-1", + title="系统通知", + content="测试内容", + message_type="info", + target_user_id=None, + is_read=False, + created_at=datetime(2026, 1, 1, 12, 0, 0), + read_at=None, + ) + + result = SystemMessagePublic.model_validate(message) + + assert result.id == "message-1" + assert result.title == "系统通知" + assert result.is_read is False diff --git a/memory/2026-06-16.md b/memory/2026-06-16.md new file mode 100644 index 0000000..00c60ab --- /dev/null +++ b/memory/2026-06-16.md @@ -0,0 +1,16 @@ +# Work Log - 修复系统消息查看 500(2026-06-16) + +- 背景: + - `/api/v1/system-messages/me` 返回系统消息列表时,对 SQLAlchemy ORM 对象执行 `SystemMessagePublic.model_validate()`,Pydantic v2 未启用属性读取,导致 500。 + +- 本次处理: + - 为 `SystemMessagePublic` 增加 `ConfigDict(from_attributes=True)`,允许响应 schema 从 ORM/属性对象读取字段。 + - 新增系统消息 schema 回归测试,覆盖属性对象序列化路径。 + +- 验证: + - 基线:未启用 `from_attributes` 时,属性对象 `model_validate` 复现 Pydantic `model_type` 校验失败。 + - 修改后:属性对象 `model_validate` 可正常输出。 + - `pytest api/tests/test_system_message_schema.py` 通过。 + +- 风险与关注点: + - 改动仅影响系统消息公开响应 schema 的序列化方式,不改变接口字段、数据库结构或查询逻辑。