[REQ] add support for decimals in the python generator
Describe the solution you'd like
Add support to the python generator for the following:
units:
type: string
format: decimal
which would generate a field of type decimal.Decimal that is de/serialized across the wire as a string.
I am aware that support has been added to the python-experimental as part of https://github.com/OpenAPITools/openapi-generator/pull/11282 but we are looking for support in the python generator.
Additional context
We'd be happy to raise a PR but are looking for a steer on what the required changes would be and how long it might take to implement.
Thanks
One could maybe add it by changing the mapping for its type to Decimal, but how would it interact with over validation where the type could be plain string with no formatting? Python-experimental handles those use cases much more elegantly. Just curious, why do you prefer the python client over the python-experimental one? It has the feature that you are looking for, Have you tried it?
I was thinking to essentially do as you say such that the openapi_types mapping in the generated model class maps to a Decimal. So type: string maps to str and type: string, format: decimal maps to Decimal.
For the use case you describe where no formatting is given, it would be treated as a string.
If that sounds like a reasonable approach, we're looking for some guidance as to what code changes would need to be made.
Wrt python-experimental, we use the generated SDK in a production environment and so the "experimental" tag led us to believe it wasn't quite finished. Is that that the case or are there still more features that need adding? We'd be open to helping close any gaps if that is the case.
Thanks
Experimental refers to the fact that it has been out for a shorter period of time and that we allow breaking changes to be made to that generator. If you version pin you should be fine. Others are using it in production. It has many more features than the python generator including
- decimal support
- type hints everywhere
- multiple request and response content type handling
- more robust openapi unit testing
Please give it a try and let me know if it meets your needs. It is not missing any major features.
thanks, will give a try and revert back 👍
Glad to hear it. If you use the latest master branch there are a ton of type hint improvements and it works great in pycharm.
These are the latest improvements: https://github.com/OpenAPITools/openapi-generatorhttps://github.com/OpenAPITools/openapi-generator/pull/13325 https://github.com/OpenAPITools/openapi-generatorhttps://github.com/OpenAPITools/openapi-generator/pull/13323 https://github.com/OpenAPITools/openapi-generatorhttps://github.com/OpenAPITools/openapi-generator/pull/13314 https://github.com/OpenAPITools/openapi-generatorhttps://github.com/OpenAPITools/openapi-generator/pull/13309 https://github.com/OpenAPITools/openapi-generatorhttps://github.com/OpenAPITools/openapi-generator/pull/13299 https://github.com/OpenAPITools/openapi-generatorhttps://github.com/OpenAPITools/openapi-generator/pull/13271
Hi Justin,
I've had a try with the python-experimental generator. There seems to be a number of breaking changes between the it and the python one (which are reasonable and I suppose expected), but this does mean that adoption would take us longer to complete.
I did manage to almost get a couple of calls working but we've hit a blocking issue:
NotImplementedError: Serialization has not yet been implemented for application/json-patch+json
So for now at least we are probably going to need to try to add the feature into the existing python one. Would you be able to provide some guidance as where in the generator code we would need to make the changes?
Thanks
Hi there, I just fixed your blocking issue in this PR which has been merged into master branch. This test verifies that json patch serialization and sending is working and it passed in CI here. So my suggestion is to use python-experimental because:
- it now has the features that you need
- updating your code with the needed changes migrating
pythontopython-experimentalwill probably take less time than writing a PR adding decimal to the python generator - There is a migration guide that points out the changes that you need to make
- the
pythongenerator needs all value stored for a property in multiple validated schemas to be the same. if there are two allOf schemas that constrain the value and one defines a decimal astype: stringand another defines it astype: string format: decimalI do not think that it will work in thepythongenerator. That definitely WILL work in the python-experimental generator.
If you do want to move forward with a PR in the python generator then these are some places that will need to change (from looking at how datetime is done):
- Decimal will need to be added to the AnyType type string generation here: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java#L864
- and here https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java#L892
- You will need to add a
typeMapping.put("decimal", "decimal.Decimal");in PythonClientCodegen.java - You will need to update sanitize_for_serialization, get_simple_class, COERCIBLE_TYPE_PAIRS in the python templates to handle Decimal inputs
- Update many more locations in the python templates, look at where datetime and date are used, and add Decimal handling
Thanks for fixing that so quickly, I'll take a look at the experimental and python changes
my mistake re-opening this
Closing this because the current python generator in v6.2.0 and onward supports decimals