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

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

Closed
robertschweizer opened this issue Mar 31, 2023 · 2 comments · Fixed by #15124
Closed

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

robertschweizer opened this issue Mar 31, 2023 · 2 comments · Fixed by #15124

Comments

@robertschweizer
Copy link
Contributor

robertschweizer commented Mar 31, 2023

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.

@robertschweizer robertschweizer changed the title [REQ][python-experimental] Allow integers as numbers when no format is set [REQ][python-nextgen] Allow integers as numbers when no format is set Mar 31, 2023
@spacether
Copy link
Contributor

I agree and asked for this differentiation in this comment: #14618 (comment)

@wing328
Copy link
Member

wing328 commented Apr 6, 2023

As discussed, doing a float(value behind the scene using a validator may be counter-intuitive the users.

#10541 introduces options to let users pick the right type for their use cases (based on the experience from pydantic project that no single solution fits all)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants