Skip to content

[REQ][python-nextgen] Allow integers as numbers when no format is set #15098

@robertschweizer

Description

@robertschweizer

Is your feature request related to a problem? Please describe.

With the current default floatStrictType=true, an integer passed in a field with "number" type and no format in the JSON schema raises a validation error. This should only happen for numbers with format float according to JSON schema (previously mentioned in #14618 (comment)).

Describe the solution you'd like

For fields with type "number" and no format, integers should be allowed while still being strict about other types such as strings. This could be achieved with a pydantic validator that converts all fields with this type in a model:

class ModelClass(BaseModel):
    field1: StrictFloat
    field2: StrictFloat
    
    @validator("field1", "field2", pre=True)
    def _int_to_float(cls, value):
        if isinstance(value, int):
            value = float(value)
        return value

Describe alternatives you've considered

Currently, I set floatStrictType=false, but this is not strict at all, i.e. it also coerces strings to float instead of raising a validation error. It is also inconvenient when getting started with the generator: The generated client by default does not follow the JSON schema spec.

Additional context

With this change, the need for the floatStrictType config flag might go away. Converting ints to floats is as strict as possible and addresses the original issue #14499 that led to the introduction of the flag.

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