-
Notifications
You must be signed in to change notification settings - Fork 18
Description
Describe the bug
The json model builder for Decimal types results in the following signature:
"anyOf": [
{
"type": "number"
},
{
"type": "string"
}
],This was a change from pydantic v1 that allows decimal numbers to be encoded as strings in order to preserve their numeric precision. (see details here).
Unfortunately, Pydantic goes a bit rogue and comes up with their own standard (at least I couldn't find anything in json-schema nor OpenApi??) of what a Decimal means by choosing a AnyOf[str | number] and solving the deserialization with custom logic (see source here).
This is unfortunate since the union is not discriminated, and thus makes it impossible to know what the type should resolve as.
Other users have also raised this issue. See this discussion for more details
Describe the solution you'd like
It would be nice to change the model class and force decimals to either always be strings or numbers. This can be done in two ways:
- Set the property of the model_json() method to
Serializeinstead of the defaultValidate - Override the
decimal_schemamethod in the implementation mentioned above and create our own custom model builder.