Python generator emits trailing commas on enum values, creating tuples instead of strings
- Dominant language
- C#
- Stars
- 3.8k
- Forks
- 333
- Avg merge
- 16h 29m
- Merged PRs (30d)
- 116
Description
### What are you generating using Kiota, clients or plugins?
API Client/SDK
### In what context or format are you using Kiota?
Mac executable
### Client library/SDK language
Python
### Describe the bug
The Kiota Python generator emits trailing commas on `str, Enum` member assignments, which in Python creates **tuples** instead of strings. This causes all enum deserialization via `get_enum_value()` to silently return `None`, since the incoming string from the API response never matches the tuple values stored in the enum.
The command below outputs:
```python
from enum import Enum
class Color(str, Enum):
Red = "red",
Green = "green",
Blue = "blue",
```
In Python, `"red",` is a **tuple** `("red",)`, not the string `"red"`.
All enum deserialization is broken. When `get_enum_value()` receives a string like `"red"` from the API response and tries to match it against enum values, it fails because the enum values are tuples (e.g. `("red",)`), not strings. The method returns `None` instead of the expected enum member.
This affects **every `str, Enum`** generated by the Python generator.
I found one workaround - post-process generated files to strip trailing commas from enum assignments:
```bash
find output/models -name '*.py' \
-exec sed -i 's/^\([[:space:]]*[A-Za-z_]* = ".*"\),$/\1/' {} +
```
### Expected behavior
Expecting to produce this:
```python
from enum import Enum
class Color(str, Enum):
Red = "red"
Green = "green"
Blue = "blue"
```
### How to reproduce
```bash
mkdir -p output
docker run -v ./output:/app/output -v ./openapi.json:/app/openapi.json \
mcr.microsoft.com/openapi/kiota:1.30.0 generate -d /app/openapi.json --language python
cat output/models/color.py
```
### Open API description file
**`openapi.json`:**
```json
{
"openapi": "3.0.3",
"info": { "title": "Minimal Enum Bug Repro", "version": "1.0.0" },
"paths": {
"/items": {
"get": {
"operationId": "getItems",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/Item" }
}
}
}
}
}
}
},
"components": {
"schemas": {
"Color": {
"type": "string",
"enum": ["red", "green", "blue"]
},
"Item": {
"type": "object",
"properties": {
"name": { "type": "string" },
"color": { "$ref": "#/components/schemas/Color" }
}
}
}
}
}
```
### Kiota Version
1.30.0 (also tried on 1.32 with the same result)
### Latest Kiota version known to work for scenario above?(Not required)
_No response_
### Known Workarounds
_No response_
### Configuration
_No response_
### Debug output
Click to expand log
```
```
### Other information
_No response_
Contributor guide
Research direction
Run the supplied Docker reproduction with the OpenAPI example, then inspect output/models/color.py and the generated Python enum values. Check how get_enum_value() matches API strings against those values; done means generated enum assignments are strings without trailing commas and enum deserialization returns the expected member.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, python
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100