swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

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

Open hyangda opened this issue 7 years ago • 3 comments

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

https://github.com/swagger-api/swagger-codegen/issues/8855 https://github.com/swagger-api/swagger-codegen/issues/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

hyangda avatar Aug 01 '19 18:08 hyangda

Thanks for the workaround

litan1106 avatar Aug 21 '19 18:08 litan1106

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.

JamesAllen16 avatar Aug 22 '19 15:08 JamesAllen16

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

frantuma avatar Aug 23 '19 09:08 frantuma