swagger-api / swagger-api/swagger-codegen

[Python] Incorrect code generated for integer properties with enum

Open
#3,667 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python Feature: Enum help wanted Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Python generator used by swagger-codegen 2.2.1 and reproduce generation from the YAML or JSON declaration shown here. Trace how integer enum values become the setter's allowed_values list, then verify that generated values remain integers and no longer cause validation failures.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.