swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.04k stars 6.03k forks source link

[Python] Incorrect code generated for integer properties with enum #3667

Open zjarkov opened 8 years ago

zjarkov commented 8 years ago
Description

Data type of enum items in Python generated code for property setter does not match to given yaml/json datatype (and/or expected type of property). It leads to permanent validation failures.

Swagger-codegen version

2.2.1

Swagger declaration file content or url

yaml

baudrate:
  type: integer
  format: int32
  enum:
    - 1200
    - 2400
    - 4800
    - 9600
    - 19200

json

"baudrate" : {
    "type" : "integer",
    "format" : "int32",
    "enum" : [1200, 2400, 4800, 9600, 19200]
}
Produced code
@baudrate.setter
def baudrate(self, baudrate):
    """
    Sets the baudrate of this ModbusRtuSettings.

    :param baudrate: The baudrate of this ModbusRtuSettings.
    :type: int
    """
    allowed_values = ["1200", "2400", "4800", "9600", "19200"]
    if baudrate not in allowed_values:
        raise ValueError(
            "Invalid value for `baudrate` ({0}), must be one of {1}"
            .format(baudrate, allowed_values)
        )

    self._baudrate = baudrate
Expected code
    ...
    allowed_values = [1200, 2400, 4800, 9600, 19200]
    if baudrate not in allowed_values:
    ...
Command line used for generation

So far used/generated via swagger-editor (generator)

scottrw93 commented 8 years ago

@wing328 Looking at the code here all enum values are treated as strings. How should maps be handled? Should it just check the values are a subset of the enumerator values?

wing328 commented 8 years ago

@scottrw93 yes, for Python enum, it needs some work to make it support integer enum similar to what we've done for Java, PHP, etc.

For containers (map, list), it should check to ensure the values are a subset of the enumerator values.

wing328 commented 7 years ago

@zjarkov 2.3.0 branch has better support for enum in Python API client. Please give that a try and see if you still encounter the same issue.

moleksyuk commented 6 years ago

Hi @wing328

Still have issue with integer enums in Python.

My example:

    properties:
      simCard:
        type: "integer"
        format: "int32"
        enum: [1, 2]
        default: 1
        required: false

After code generation I'm getting a valid object of enum: image

But a constructor of object with this enum is generated with invalid value: image

wing328 commented 6 years ago

@moleksyuk Thanks for tagging me but I'm no longer involved in this project. I hope others will be able to help you out. Good luck.