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

[BUG][PYTHON] Generator creates Enums with duplicate attributes #19542

Closed
5 of 6 tasks
tobias-watzel opened this issue Sep 6, 2024 · 0 comments
Closed
5 of 6 tasks

[BUG][PYTHON] Generator creates Enums with duplicate attributes #19542

tobias-watzel opened this issue Sep 6, 2024 · 0 comments

Comments

@tobias-watzel
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • 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

Generator creates Enums with duplicate attributes

openapi-generator version

openapi-generator v7.8.0

OpenAPI declaration file content or url
{
  "openapi": "3.1.0",
  "info": {
    "title": "FastAPI",
    "version": "0.1.0"
  },
  "paths": {
    "/status": {
      "get": {
        "summary": "Get Status",
        "operationId": "get_status_status_get",
        "parameters": [
          {
            "name": "Status",
            "in": "query",
            "required": true,
            "schema": {
              "$ref": "#/components/schemas/StatusEnum"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          },
          "422": {
            "description": "Validation Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/HTTPValidationError"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "HTTPValidationError": {
        "properties": {
          "detail": {
            "items": {
              "$ref": "#/components/schemas/ValidationError"
            },
            "type": "array",
            "title": "Detail"
          }
        },
        "type": "object",
        "title": "HTTPValidationError"
      },
      "StatusEnum": {
        "type": "string",
        "enum": [
          "ACTIVE",
          "active"
        ],
        "title": "StatusEnum"
      },
      "ValidationError": {
        "properties": {
          "loc": {
            "items": {
              "anyOf": [
                {
                  "type": "string"
                },
                {
                  "type": "integer"
                }
              ]
            },
            "type": "array",
            "title": "Location"
          },
          "msg": {
            "type": "string",
            "title": "Message"
          },
          "type": {
            "type": "string",
            "title": "Error Type"
          }
        },
        "type": "object",
        "required": [
          "loc",
          "msg",
          "type"
        ],
        "title": "ValidationError"
      }
    }
  }
}

where the important part is:

"StatusEnum": {
  "type": "string",
  "enum": [
    "ACTIVE",
    "active"
  ],
  "title": "StatusEnum"
}
Generation Details

openapi-generator generate -i openapi_example.json -g python --package-name example -o .

Steps to reproduce

Create a simple FastAPI:

import json
from enum import Enum

from fastapi import FastAPI, APIRouter

app = FastAPI()
router = APIRouter()

class StatusEnum(str, Enum):
    ACTIVE = "ACTIVE"
    ENUM_ACTIVE = "active"

@router.get("/status")
async def get_status(Status: StatusEnum):
    return {"status": Status.value}

def write_openap_spec(app):

    openapi_schema = app.openapi()

    with open("openapi_example.json", "w") as f:
        json.dump(openapi_schema, f, indent=2)

if __name__ == "__main__":
    app.include_router(router)
    write_openap_spec(app)

and generate the PythonClient with the generator, resulting in the following Enum in Python:

# coding: utf-8

"""
    FastAPI

    No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)

    The version of the OpenAPI document: 0.1.0
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import json
from enum import Enum
from typing_extensions import Self


class StatusEnum(str, Enum):
    """
    StatusEnum
    """

    """
    allowed enum values
    """
    ACTIVE = 'ACTIVE'
    ACTIVE = 'active'

    @classmethod
    def from_json(cls, json_str: str) -> Self:
        """Create an instance of StatusEnum from a JSON string"""
        return cls(json.loads(json_str))

where we retrieve the duplicate attribute ACTIVE in the StatusEnum.

Related issues/PRs

None

Suggest a fix

Add

@Setter protected boolean allEnumValuesUpperCase = true;

in AbstractPythonCodegen.java and the regarding logic to handle the case where enums shouldn't be capitalized.

@tobias-watzel tobias-watzel closed this as not planned Won't fix, can't repro, duplicate, stale Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant