Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [python-experimental] openapi-spec number format is ignored #16135

Open
3 of 4 tasks
MalteEbner opened this issue Jul 19, 2023 · 0 comments
Open
3 of 4 tasks

[BUG] [python-experimental] openapi-spec number format is ignored #16135

MalteEbner opened this issue Jul 19, 2023 · 0 comments

Comments

@MalteEbner
Copy link

MalteEbner commented Jul 19, 2023

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 #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

@MalteEbner MalteEbner changed the title [BUG] Description [BUG] [python-experimental] openapi-spec number format is ignored Jul 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants