datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Constraint float casted into integer

Open FredericOutsight opened this issue 3 years ago • 0 comments
trafficstars

Describe the bug When using alias maximum / minimum ... the generated type is Union[int, float, None]. As the class ConstaintsBase does not uses the BaseModel option smart_union, if a float is used, it will be cased into int by pydantic and the generated extremum will the erroneous.

To Reproduce

If you patch the api_constrained.yaml that way

index 6340a51..4794135 100644
--- a/tests/data/openapi/api_constrained.yaml
+++ b/tests/data/openapi/api_constrained.yaml
@@ -169,6 +169,12 @@ components:
               - integer
             minimum: 1
             maximum: 1000
+          iq:
+            type: number
+            format: float
+            minimum: 0.2
+            maximum: 149.5
+
 
     Id:
       type: string

You can see that the generated code will be iq: Optional[float] = Field(None, ge=0.0, le=149.0) while: iq: Optional[float] = Field(None, ge=0.2, le=149.5) is expected

Expected behavior Keep the actual float values for the keys : ge, le, gt, lt instead of converting them to integer and then back to float that create erroneous values

Version:

  • OS: Ubuntu 20.04
  • Python version: 3.8
  • datamodel-code-generator version: 1.12.0

Additional context NA

FredericOutsight avatar Jul 11 '22 14:07 FredericOutsight