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] make float parsing more lenient #14499

Closed
tnielens opened this issue Jan 21, 2023 · 5 comments · Fixed by #14618
Closed

[REQ][python-nextgen] make float parsing more lenient #14499

tnielens opened this issue Jan 21, 2023 · 5 comments · Fixed by #14618

Comments

@tnielens
Copy link

tnielens commented Jan 21, 2023

If a response json field is an int (1), although the target field type is float (1.0) , the response parsing fails with an error like

ValidationError: 1 validation error for <ClassName>
<fieldName>
  value is not a valid float (type=type_error.float)

Describe the solution you'd like

Can the parsing be more lenient? Former python generators parsed this more leniently.

Describe alternatives you've considered

None

Additional context

I'm trying to use the latest python client code generator. Btw, great job on the source compatibility 👍 .

@spacether
Copy link
Contributor

Fyi if you are open to using the python generator we have a CI verified test for this use case at:
https://github.com/OpenAPITools/openapi-generator/blob/master/samples/openapi3/client/3_0_3_unit_test/python/test/test_models/test_integer_type_matches_integers.py#L50

    def test_a_float_with_zero_fractional_part_is_an_integer_passes(self):
        # a float with zero fractional part is an integer
        IntegerTypeMatchesIntegers.from_openapi_data_oapg(
            1.0,
            _configuration=self._configuration
        )

@tnielens
Copy link
Author

@spacether thanks. We have many projects based on clients generated by the python-prior generator. The python-nextgen sources look more compatible than the current python one. Also the models are based on pydantic which we have experience with and appreciate. python-nextgen looks like a better fit for us.

@nmoreaud
Copy link

nmoreaud commented Feb 1, 2023

It seems legitimate to parse {"field": 1} as a valid float value.

Note: generated code works fine for me if I post-process it like this:

find models -iname '*.py' -type f -print0 | xargs -0 sed -r -i 's/: StrictFloat/: float/g'
find models -iname '*.py' -type f -print0 | xargs -0 sed -r -i 's/: Optional\[StrictFloat\]/: Optional[float]/g'

@wing328
Copy link
Member

wing328 commented Feb 4, 2023

I've filed #14618 to add an option to make float's strict type configurable.

@tnielens
Copy link
Author

Tested successfully on 3746a20.

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.

4 participants