OpenAPITools / OpenAPITools/openapi-generator
[BUG] [python-experimental] openapi-spec number format is ignored
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
Description
With this openapi-object:
SomeObject:
properties:
some_float:
type: number
format: float
minimum: -10000000000
maximum: 10000000000
the generator from version 6.6.0 generates this object:
class SomeObject(BaseModel):
"""
SomeObject
"""
some_float: Optional[Union[confloat(le=10000000000, ge=-10000000000, strict=True), conint(le=2147483647, ge=-2147483648, strict=True)]] = None
__properties = ["some_float"]
When using that object with an API, it might happen that the API returns a passed float(1e10) as string "10000000000". That is interpreted by pydantic as an int but is bigger than int32, thus it causes an error:
string_from_server = '{"some_float": 10000000000}'
a = SomeObject.from_dict(json.loads(string_from_server))
errors with
File "pydantic/main.py", line 526, in pydantic.main.BaseModel.parse_obj
File "pydantic/main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 2 validation errors for SomeObject
some_float
value is not a valid float (type=type_error.float)
some_float
ensure this value is less than or equal to 2147483647 (type=value_error.number.not_le; limit_value=2147483647)
Instead, it should interpret the number always as float, as specified in the openapi specs.
openapi-generator version
6.6.0
Related issues/PRs
The problem was probably introduced in https://github.com/OpenAPITools/openapi-generator/pull/15124, which was written by @wing328 and reviewed by @spacether.
Suggest a fix
We should use the format attribute of the API specification. If it is float or double, don't add the conint to the generated python code. This probably requires changes in these lines.
Note: I cannot use the mapNumberTo: float cli option, as other numbers with an unspecified format are and should stay integers
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonNextgenClientCodegen.java around lines 512-516, where numeric constraints are generated. Check how the OpenAPI format is handled for number properties, then verify that float and double fields no longer receive an integer constraint while unspecified numeric formats retain their existing behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi, python
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100