Skip to content

[BUG][PYTHON] Enums whose values are floats generate invalid class #21322

@pcaisse

Description

@pcaisse

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

Python specs that include an enum whose values are floats result in generating invalid enum member names since they cannot contain dots.

Example bad output:

class Type(int, Enum):
    """
    Type
    """

    """
    allowed enum values
    """
    NUMBER_2.0 = 2.0
    NUMBER_1.0 = 1.0
    NUMBER_0.5 = 0.5
    NUMBER_0.25 = 0.25
openapi-generator version

7.14.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: Sample API
  description: API description in Markdown.
  version: 1.0.0
paths:
  /pony-sizes:
    get:
      summary: Returns all pony sizes.
      description: Optional extended description in Markdown.
      responses:
        200:
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/PonySizes'
components:
  schemas:
    PonySizes:
      type: object
      properties:
        type:
          $ref: '#/components/schemas/Type'
    Type:
      type: float
      enum:
        - 2.0
        - 1.0
        - 0.5
        - 0.25
Steps to reproduce

Run this command (YAML referenced is the same as what's in the previous section):

docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
    -i https://raw.githubusercontent.com/pcaisse/openapi-generator/refs/heads/master/modules/openapi-generator/src/test/resources/3_0/enum_float.yaml \
    -g python \
    -o /local/out/python

Then compile and note the syntax error:

python -m py_compile out/python/openapi_client/models/type.py
  File "out/python/openapi_client/models/type.py", line 29
    NUMBER_2.0 = 2.0
            ^^
SyntaxError: invalid syntax
Suggest a fix

I believe replacing all periods with the string "DOT" (eg. name = name.replace(".", "DOT");) here would fix the issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions