Skip to content

Commit

Permalink
fix: 尝试修复 3.10 以及以下序列化 message 时 RetCode 序列化为枚举名称的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Ljzd-PRO committed May 19, 2024
1 parent 8229703 commit 6ddb872
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pydglab_ws/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
此处定义了一些 Pydantic 模型,使用 Pydantic V2
"""
from enum import IntEnum
from typing import Optional, Any, Union

from pydantic import BaseModel, UUID4, ConfigDict, field_serializer, AliasGenerator, model_validator, \
Expand Down Expand Up @@ -53,8 +54,8 @@ def _validate_message(cls, value: Any):
@field_serializer("message", mode="wrap")
def _serialize_message(self, value: Any, nxt: SerializerFunctionWrapHandler, _: FieldSerializationInfo):
"""对于 ``IntEnum`` 的枚举,转化为 ``str``"""
if isinstance(value, int):
return str(value)
if isinstance(value, IntEnum):
return str(value.value)
else:
return nxt(value)

Expand Down

0 comments on commit 6ddb872

Please sign in to comment.