Closed QSHolzner closed 1 year ago
Does this work for pydantic v1?
Does this work for pydantic v1?
This Type is only used for pydantic v2. For v1 the type DecimalAnnotation
is an Alias of the Decimal
type
from beanie.odm.utils.pydantic import IS_PYDANTIC_V2
if IS_PYDANTIC_V2:
from beanie.odm.custom_types.decimal import DecimalAnnotation
else:
from decimal import Decimal as DecimalAnnotation
Describe the bug If the beanie DecimalAnnotation is used with a model class, then the Decimal values are written out as numbers and read in as floats when serializing to json. But if only Decimal is used, which is supported by Pydantic, then the value is written out in json as String and imported again as Decimal.
To Reproduce
Case 1 - DecimalAnnotation
Console output:
Case 2 - Decimal
Console output:
Expected behavior
The DecimalAnnotaton should behave like the Decimal type when serializing.
Additional context
In the DecimalCustomAnnotation class the float_schema is used. Is there a specific reason for this? If core_schema.decimal_schema() is used here then DecimalAnnotation behaves like the Decimal type when serializing / deserializing.