-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Description
Is your feature request related to a problem? Please describe.
With the current default floatStrictType=true, an integer passed in a field with "number" type and no format in the JSON schema raises a validation error. This should only happen for numbers with format float according to JSON schema (previously mentioned in #14618 (comment)).
Describe the solution you'd like
For fields with type "number" and no format, integers should be allowed while still being strict about other types such as strings. This could be achieved with a pydantic validator that converts all fields with this type in a model:
class ModelClass(BaseModel):
field1: StrictFloat
field2: StrictFloat
@validator("field1", "field2", pre=True)
def _int_to_float(cls, value):
if isinstance(value, int):
value = float(value)
return valueDescribe alternatives you've considered
Currently, I set floatStrictType=false, but this is not strict at all, i.e. it also coerces strings to float instead of raising a validation error. It is also inconvenient when getting started with the generator: The generated client by default does not follow the JSON schema spec.
Additional context
With this change, the need for the floatStrictType config flag might go away. Converting ints to floats is as strict as possible and addresses the original issue #14499 that led to the introduction of the flag.