Skip to content

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

Open
@MalteEbner

Description

@MalteEbner

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions