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

[Python] number type cast to nonexistent BigDecimal instead of float #9612

Closed
hyangda opened this issue Aug 1, 2019 · 3 comments
Closed

Comments

@hyangda
Copy link

hyangda commented Aug 1, 2019

Description

This is coming out of a python flask app, but we are using --lang python:

A number type in our swagger.json gets incorrectly mapped to BigDecimal rather than float, which is a nonexistent model:

import my_client

will cause you to hit this line in the parent model

from my_client.models.big_decimal import BigDecimal  # noqa: F401,E501

which will result in

ModuleNotFoundError: No module named 'my_client.models.big_decimal'

because my_client.models.big_decimal is never generated.

Swagger-codegen version

3.0.10

Swagger declaration file content or url
{
  "OffendingSchema": {
    "additionalProperties": false,
    "properties": {
      "some_object": {
        "items": {
          "items": {
            "type": "number"  // this is the offending type
          },
          "type": "array"
        },
        "type": "array"
      }
    },
    "required": [
      "some_object"
    ],
    "title": "OffendingSchema",
    "type": "object"
  }
}
Command line used for generation
java -jar swagger-codegen-cli.jar generate \
                --lang python \
                --input-spec clients/swagger.json \
                -c clients/swagger-config.json \
                -o clients/python/generated;
Steps to reproduce
  • Make a swagger.json with the above snippet
  • Run the command above to generate a client
  • Import your python client
Related issues/PRs

#8855
#9353

Suggest a fix/enhancement

I think this may be because the following line present in the python-flask generator is not present in the python generator:

typeMapping.put("BigDecimal", "float");

https://github.com/swagger-api/swagger-codegen-generators/blob/781c5a407c831130285c16eac735d56c0f9edd76/src/main/java/io/swagger/codegen/v3/generators/python/PythonFlaskConnexionCodegen.java#L78

absent in

https://github.com/swagger-api/swagger-codegen-generators/blob/781c5a407c831130285c16eac735d56c0f9edd76/src/main/java/io/swagger/codegen/v3/generators/python/PythonClientCodegen.java#L94

Temporary workaround

I was able to get around this by passing in --import-mappings BigDecimal=float to swagger-codegen-cli

@litan1106
Copy link

Thanks for the workaround

@JamesAllen16
Copy link

JamesAllen16 commented Aug 22, 2019

I ran into the same issue today with the same version...

However the fix to run --import-mappings BigDecimal=float caused other-side effects in my codebase.

I ended up using v2.4.7 over v3.0.10 and this seemed to work fine for me.

frantuma added a commit to swagger-api/swagger-codegen-generators that referenced this issue Aug 23, 2019
…en#8855 - fix BigDecimal typeMapping in typescript and python
frantuma added a commit to swagger-api/swagger-codegen-generators that referenced this issue Aug 23, 2019
@frantuma
Copy link
Member

fixed in swagger-api/swagger-codegen-generators/pull/446

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

No branches or pull requests

4 participants