Skip to content

[BUG] [PYTHON] Unable to map number types to decimal.Decimal #23887

@HYBRID-BEING

Description

@HYBRID-BEING

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

I'm working with a third-party OpenAPI specification, which defines all floating values as number, and python generator generates Pydantic models with Union[StrictFloat, StrictInt] types for these values.

However, i'd like those values to be Decimal instead.

openapi-generator version

7.22.0

OpenAPI declaration file content or url

https://universal-api.1-ofd.ru/api/swagger-ui/v3/api-docs

Generation Details

I tried a few approaches:

With this, floating point values have type Union[StrictFloat, StrictInt]:

java.exe -jar .\openapi-generator-cli-7.22.0.jar generate `
>>   -g python `
>>   -i https://universal-api.1-ofd.ru/api/swagger-ui/v3/api-docs `
>>   -o api `
>>   --type-mappings "number=Decimal" `
>>   --import-mappings "Decimal=decimal.Decimal"

With this, floating point values have type float (mapNumberTo doesnt allow arbitrary types):

java.exe -jar .\openapi-generator-cli-7.22.0.jar generate `
>>   -g python `
>>   -i https://universal-api.1-ofd.ru/api/swagger-ui/v3/api-docs `
>>   -o api`
>>   --type-mappings "float=Decimal" `
>>   --import-mappings "Decimal=decimal.Decimal" `
>>   --additional-properties=mapNumberTo=float
Steps to reproduce
  1. Execute one of the commands above;
  2. Check file "api\openapi_client\models\item_dto.py", and see that field price of model ItemDto does not have type Optional[Decimal].
Related issues/PRs

#2534 #13324 #19198
These issues cover converting specific types into Decimal (number formatted as decimal, string formatted as decimal or just decimal), but i'm talking about coercing chosen type(s) into it.

Suggest a fix

It looks like --type-mappings "number=Decimal" is not respected (unless i misunderstand the usage of it), but i'm not getting any warning about that.

If it's not supposed to work, then, perhaps, allowing passing arbitrary types to mapNumberTo property could be an alternative solution.

Finally, making unformatted numbers be parsed into Decimal fields would work for me, although underlying problem will remain.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions