MarcoMuellner / openapi-python-generator

A client generator from openapi for python.
MIT License
56 stars 25 forks source link

Spaces in enum values are not honored #55

Open AaronKluck opened 11 months ago

AaronKluck commented 11 months ago

Describe the bug With other generators, spaces in enum values work perfectly fine. But with openapi-python-generator, spaces are replaced by underscores. This causes the generated client to be incompatible with the actual service!

I traced this behavior to where this regex determining value_dict["enum"] values to feed the ENUM_TEMPLATE Jinja template. I'm not experienced with Jinja, but I expect a separate key-value pair could be used with it somehow instead.

To Reproduce Define an enum with spaces in one or more values, e.g.

    "components": {
        "schemas": {
            "ThingType": {
                "title": "ThingType",
                "enum": [
                    "big thing",
                    "small thing"
                ],
                "type": "string"
            },

The resulting model looks like this:

class ThingType(str, Enum):

    BIG_THING = "big_thing"
    SMALL_THING = "small_thing"

Expected behavior Enum names should have invalid characters converted to underscores, but the values should be preserved. In the above example, I would expect:

class ThingType(str, Enum):

    BIG_THING = "big thing"
    SMALL_THING = "small thing"

Additional context I attached a sample OpenAPI spec that trivially causes this to happen.

enum_with_spaces.json