Skip to content

Commit 7463f60

Browse files
add serializer
1 parent 9564942 commit 7463f60

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

packages/models-library/src/models_library/api_schemas_resource_usage_tracker/credit_transactions.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from datetime import datetime
22
from decimal import Decimal
3+
from typing import Annotated
34

4-
from pydantic import BaseModel, ConfigDict, field_validator
5+
from pydantic import BaseModel, BeforeValidator, PlainSerializer
56

67
from ..products import ProductName
78
from ..resource_tracker import CreditTransactionId
@@ -11,15 +12,11 @@
1112

1213
class WalletTotalCredits(BaseModel):
1314
wallet_id: WalletID
14-
available_osparc_credits: Decimal
15-
16-
model_config = ConfigDict(json_encoders={Decimal: float})
17-
18-
@field_validator("available_osparc_credits", mode="before")
19-
@classmethod
20-
def ensure_rounded(cls, v):
21-
return round(v, 2)
22-
15+
available_osparc_credits: Annotated[
16+
Decimal,
17+
BeforeValidator(lambda x: round(x, 2)),
18+
PlainSerializer(float, return_type=float, when_used='json')
19+
]
2320

2421
class CreditTransactionCreateBody(BaseModel):
2522
product_name: ProductName

0 commit comments

Comments
 (0)