Skip to content

Commit

Permalink
Fix exponent in minimum (#1664)
Browse files Browse the repository at this point in the history
* Fix numerical field with an exponent in the minimum

* Add unittest
  • Loading branch information
koxudaxi authored Nov 7, 2023
1 parent 4a244c4 commit ff63a98
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
11 changes: 11 additions & 0 deletions datamodel_code_generator/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ def validate(cls, v: Any) -> 'UnionIntFloat':
if isinstance(v, UnionIntFloat):
return v
elif not isinstance(v, (int, float)): # pragma: no cover
try:
int(v)
return cls(v)
except (TypeError, ValueError):
pass
try:
float(v)
return cls(v)
except (TypeError, ValueError):
pass

raise TypeError(f'{v} is not int or float')
return cls(v)

Expand Down
1 change: 1 addition & 0 deletions tests/data/expected/main/main_strict_types/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ class User(BaseModel):
tel: Optional[constr(regex=r'^(\([0-9]{3}\))?[0-9]{3}-[0-9]{4}$')] = None
height: Optional[confloat(ge=0.0)] = None
weight: Optional[confloat(ge=0.0)] = None
score: Optional[confloat(ge=1e-08)] = None
active: Optional[bool] = None
photo: Optional[conbytes(min_length=100)] = None
1 change: 1 addition & 0 deletions tests/data/expected/main/main_strict_types_all/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ class User(BaseModel):
] = None
height: Optional[confloat(ge=0.0, strict=True)] = None
weight: Optional[confloat(ge=0.0, strict=True)] = None
score: Optional[confloat(ge=1e-08, strict=True)] = None
active: Optional[StrictBool] = None
photo: Optional[StrictBytes] = None
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ class User(BaseModel):
tel: Optional[StrictStr] = None
height: Optional[StrictFloat] = None
weight: Optional[StrictFloat] = None
score: Optional[StrictFloat] = None
active: Optional[StrictBool] = None
photo: Optional[StrictBytes] = None
6 changes: 5 additions & 1 deletion tests/data/jsonschema/strict_types.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
"type": "number",
"minimum": 0
},
"score": {
"type": "number",
"minimum": 1e-08
},
"active": {
"type": "boolean"
},
Expand All @@ -43,4 +47,4 @@
"minLength": 100
}
}
}
}

0 comments on commit ff63a98

Please sign in to comment.