Skip to content

Commit db988b6

Browse files
committed
Ensure enum varient idents are valid python symbols, and enum values are
unaltered. Fixes #30 , #55 , and #87 .
1 parent d8a2879 commit db988b6

File tree

6 files changed

+195
-5
lines changed

6 files changed

+195
-5
lines changed

src/openapi_python_generator/language_converters/python/model_generator.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,8 @@ def generate_models(
414414
name = common.normalize_symbol(schema_name)
415415
if schema_or_reference.enum is not None:
416416
value_dict = schema_or_reference.model_dump()
417-
regex = re.compile(r"[\s\/=\*\+]+")
418417
value_dict["enum"] = [
419-
re.sub(regex, "_", i) if isinstance(i, str) else f"value_{i}"
418+
(common.normalize_symbol(str(i)).upper(), i)
420419
for i in value_dict["enum"]
421420
]
422421
m = Model(
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from enum import Enum
22

33
class {{ name }}(str, Enum):
4-
{% for enumItem in enum %}
4+
{% for enumItemIdent, enumItem in enum %}
55

66
{% if enumItem is string %}
7-
{{ enumItem.upper() }} = '{{ enumItem }}'{% else %}
8-
value_{{ enumItem }} = {{ enumItem }}{% endif %}
7+
{{ enumItemIdent }} = '{{ enumItem }}'
8+
{% else %}
9+
{{ enumItemIdent }} = {{ enumItem }}
10+
{% endif %}
911
{% endfor %}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pytest
2+
3+
from openapi_python_generator.common import HTTPLibrary
4+
from openapi_python_generator.generate_data import get_open_api
5+
from openapi_python_generator.parsers import generate_code_3_1
6+
from tests.conftest import test_data_folder
7+
8+
9+
@pytest.mark.parametrize(
10+
"library",
11+
[HTTPLibrary.httpx, HTTPLibrary.aiohttp, HTTPLibrary.requests],
12+
)
13+
def test_issue_30_87(library) -> None:
14+
"""
15+
https://github.com/MarcoMuellner/openapi-python-generator/issues/30
16+
https://github.com/MarcoMuellner/openapi-python-generator/issues/87
17+
"""
18+
openapi_obj, version = get_open_api(str(test_data_folder / "issue_30_87.json"))
19+
result = generate_code_3_1(
20+
openapi_obj, # type: ignore
21+
library
22+
)
23+
24+
expected_model = [m for m in result.models if m.openapi_object.title == "UserType"][0]
25+
assert "ADMIN_USER = 'admin-user'" in expected_model.content

tests/regression/test_issue_55.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pytest
2+
3+
from openapi_python_generator.common import HTTPLibrary
4+
from openapi_python_generator.generate_data import get_open_api
5+
from openapi_python_generator.parsers import generate_code_3_1
6+
from tests.conftest import test_data_folder
7+
8+
9+
@pytest.mark.parametrize(
10+
"library",
11+
[HTTPLibrary.httpx, HTTPLibrary.aiohttp, HTTPLibrary.requests],
12+
)
13+
def test_issue_55(library) -> None:
14+
"""
15+
https://github.com/MarcoMuellner/openapi-python-generator/issues/55
16+
"""
17+
openapi_obj, version = get_open_api(str(test_data_folder / "issue_55.json"))
18+
result = generate_code_3_1(
19+
openapi_obj, # type: ignore
20+
library
21+
)
22+
23+
expected_model = [m for m in result.models if m.openapi_object.title == "UserType"][0]
24+
assert "ADMIN_USER = 'admin user'" in expected_model.content

tests/test_data/issue_30_87.json

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"openapi": "3.0.2",
3+
"info": {
4+
"title": "Title",
5+
"version": "1.0"
6+
},
7+
"paths": {
8+
"/users": {
9+
"get": {
10+
"summary": "Get users",
11+
"description": "Returns a list of users.",
12+
"operationId": "users_get",
13+
"parameters": [
14+
{
15+
"name": "type",
16+
"in": "query",
17+
"required": true,
18+
"schema": {
19+
"$ref": "#/components/schemas/UserType"
20+
}
21+
}
22+
],
23+
"responses": {
24+
"200": {
25+
"description": "Successful response",
26+
"content": {
27+
"application/json": {
28+
"schema": {
29+
"type": "array",
30+
"items": {
31+
"$ref": "#/components/schemas/User"
32+
}
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
}
40+
},
41+
"components": {
42+
"schemas": {
43+
"UserType": {
44+
"title": "UserType",
45+
"description": "An enumeration.",
46+
"enum": ["admin-user", "regular-user"]
47+
},
48+
"User": {
49+
"title": "User",
50+
"description": "A user.",
51+
"type": "object",
52+
"properties": {
53+
"id": {
54+
"type": "string",
55+
"format": "uuid"
56+
},
57+
"name": {
58+
"type": "string"
59+
},
60+
"type": {
61+
"$ref": "#/components/schemas/UserType"
62+
},
63+
"30d_active": {
64+
"type": "boolean"
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}

tests/test_data/issue_55.json

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"openapi": "3.0.2",
3+
"info": {
4+
"title": "Title",
5+
"version": "1.0"
6+
},
7+
"paths": {
8+
"/users": {
9+
"get": {
10+
"summary": "Get users",
11+
"description": "Returns a list of users.",
12+
"operationId": "users_get",
13+
"parameters": [
14+
{
15+
"name": "type",
16+
"in": "query",
17+
"required": true,
18+
"schema": {
19+
"$ref": "#/components/schemas/UserType"
20+
}
21+
}
22+
],
23+
"responses": {
24+
"200": {
25+
"description": "Successful response",
26+
"content": {
27+
"application/json": {
28+
"schema": {
29+
"type": "array",
30+
"items": {
31+
"$ref": "#/components/schemas/User"
32+
}
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
}
40+
},
41+
"components": {
42+
"schemas": {
43+
"UserType": {
44+
"title": "UserType",
45+
"description": "An enumeration.",
46+
"enum": ["admin user", "regular user"]
47+
},
48+
"User": {
49+
"title": "User",
50+
"description": "A user.",
51+
"type": "object",
52+
"properties": {
53+
"id": {
54+
"type": "string",
55+
"format": "uuid"
56+
},
57+
"name": {
58+
"type": "string"
59+
},
60+
"type": {
61+
"$ref": "#/components/schemas/UserType"
62+
},
63+
"30d_active": {
64+
"type": "boolean"
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)