Skip to content

Commit 56198ee

Browse files
ramnesdbanty
authored andcommitted
fix(generator)!: Improved the consistency of snake_cased identifiers which will cause some to be renamed [#413][#432]. Thanks @ramnes!
1 parent 2a85f43 commit 56198ee

File tree

4 files changed

+51
-33
lines changed

4 files changed

+51
-33
lines changed

end_to_end_tests/golden-record/my_test_api_client/models/a_model_with_properties_reference_that_are_not_object.py

+30-30
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class AModelWithPropertiesReferenceThatAreNotObject:
1919
str_properties_ref: List[str]
2020
date_properties_ref: List[datetime.date]
2121
datetime_properties_ref: List[datetime.datetime]
22-
int_32_properties_ref: List[int]
23-
int_64_properties_ref: List[int]
22+
int32_properties_ref: List[int]
23+
int64_properties_ref: List[int]
2424
float_properties_ref: List[float]
2525
double_properties_ref: List[float]
2626
file_properties_ref: List[File]
@@ -29,8 +29,8 @@ class AModelWithPropertiesReferenceThatAreNotObject:
2929
str_properties: List[str]
3030
date_properties: List[datetime.date]
3131
datetime_properties: List[datetime.datetime]
32-
int_32_properties: List[int]
33-
int_64_properties: List[int]
32+
int32_properties: List[int]
33+
int64_properties: List[int]
3434
float_properties: List[float]
3535
double_properties: List[float]
3636
file_properties: List[File]
@@ -39,8 +39,8 @@ class AModelWithPropertiesReferenceThatAreNotObject:
3939
str_property_ref: str
4040
date_property_ref: datetime.date
4141
datetime_property_ref: datetime.datetime
42-
int_32_property_ref: int
43-
int_64_property_ref: int
42+
int32_property_ref: int
43+
int64_property_ref: int
4444
float_property_ref: float
4545
double_property_ref: float
4646
file_property_ref: File
@@ -71,9 +71,9 @@ def to_dict(self) -> Dict[str, Any]:
7171

7272
datetime_properties_ref.append(componentsschemas_an_other_array_of_date_time_item)
7373

74-
int_32_properties_ref = self.int_32_properties_ref
74+
int32_properties_ref = self.int32_properties_ref
7575

76-
int_64_properties_ref = self.int_64_properties_ref
76+
int64_properties_ref = self.int64_properties_ref
7777

7878
float_properties_ref = self.float_properties_ref
7979

@@ -108,9 +108,9 @@ def to_dict(self) -> Dict[str, Any]:
108108

109109
datetime_properties.append(componentsschemas_an_array_of_date_time_item)
110110

111-
int_32_properties = self.int_32_properties
111+
int32_properties = self.int32_properties
112112

113-
int_64_properties = self.int_64_properties
113+
int64_properties = self.int64_properties
114114

115115
float_properties = self.float_properties
116116

@@ -130,8 +130,8 @@ def to_dict(self) -> Dict[str, Any]:
130130
date_property_ref = self.date_property_ref.isoformat()
131131
datetime_property_ref = self.datetime_property_ref.isoformat()
132132

133-
int_32_property_ref = self.int_32_property_ref
134-
int_64_property_ref = self.int_64_property_ref
133+
int32_property_ref = self.int32_property_ref
134+
int64_property_ref = self.int64_property_ref
135135
float_property_ref = self.float_property_ref
136136
double_property_ref = self.double_property_ref
137137
file_property_ref = self.file_property_ref.to_tuple()
@@ -146,8 +146,8 @@ def to_dict(self) -> Dict[str, Any]:
146146
"str_properties_ref": str_properties_ref,
147147
"date_properties_ref": date_properties_ref,
148148
"datetime_properties_ref": datetime_properties_ref,
149-
"int32_properties_ref": int_32_properties_ref,
150-
"int64_properties_ref": int_64_properties_ref,
149+
"int32_properties_ref": int32_properties_ref,
150+
"int64_properties_ref": int64_properties_ref,
151151
"float_properties_ref": float_properties_ref,
152152
"double_properties_ref": double_properties_ref,
153153
"file_properties_ref": file_properties_ref,
@@ -156,8 +156,8 @@ def to_dict(self) -> Dict[str, Any]:
156156
"str_properties": str_properties,
157157
"date_properties": date_properties,
158158
"datetime_properties": datetime_properties,
159-
"int32_properties": int_32_properties,
160-
"int64_properties": int_64_properties,
159+
"int32_properties": int32_properties,
160+
"int64_properties": int64_properties,
161161
"float_properties": float_properties,
162162
"double_properties": double_properties,
163163
"file_properties": file_properties,
@@ -166,8 +166,8 @@ def to_dict(self) -> Dict[str, Any]:
166166
"str_property_ref": str_property_ref,
167167
"date_property_ref": date_property_ref,
168168
"datetime_property_ref": datetime_property_ref,
169-
"int32_property_ref": int_32_property_ref,
170-
"int64_property_ref": int_64_property_ref,
169+
"int32_property_ref": int32_property_ref,
170+
"int64_property_ref": int64_property_ref,
171171
"float_property_ref": float_property_ref,
172172
"double_property_ref": double_property_ref,
173173
"file_property_ref": file_property_ref,
@@ -207,9 +207,9 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
207207

208208
datetime_properties_ref.append(componentsschemas_an_other_array_of_date_time_item)
209209

210-
int_32_properties_ref = cast(List[int], d.pop("int32_properties_ref"))
210+
int32_properties_ref = cast(List[int], d.pop("int32_properties_ref"))
211211

212-
int_64_properties_ref = cast(List[int], d.pop("int64_properties_ref"))
212+
int64_properties_ref = cast(List[int], d.pop("int64_properties_ref"))
213213

214214
float_properties_ref = cast(List[float], d.pop("float_properties_ref"))
215215

@@ -249,9 +249,9 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
249249

250250
datetime_properties.append(componentsschemas_an_array_of_date_time_item)
251251

252-
int_32_properties = cast(List[int], d.pop("int32_properties"))
252+
int32_properties = cast(List[int], d.pop("int32_properties"))
253253

254-
int_64_properties = cast(List[int], d.pop("int64_properties"))
254+
int64_properties = cast(List[int], d.pop("int64_properties"))
255255

256256
float_properties = cast(List[float], d.pop("float_properties"))
257257

@@ -276,9 +276,9 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
276276

277277
datetime_property_ref = isoparse(d.pop("datetime_property_ref"))
278278

279-
int_32_property_ref = d.pop("int32_property_ref")
279+
int32_property_ref = d.pop("int32_property_ref")
280280

281-
int_64_property_ref = d.pop("int64_property_ref")
281+
int64_property_ref = d.pop("int64_property_ref")
282282

283283
float_property_ref = d.pop("float_property_ref")
284284

@@ -293,8 +293,8 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
293293
str_properties_ref=str_properties_ref,
294294
date_properties_ref=date_properties_ref,
295295
datetime_properties_ref=datetime_properties_ref,
296-
int_32_properties_ref=int_32_properties_ref,
297-
int_64_properties_ref=int_64_properties_ref,
296+
int32_properties_ref=int32_properties_ref,
297+
int64_properties_ref=int64_properties_ref,
298298
float_properties_ref=float_properties_ref,
299299
double_properties_ref=double_properties_ref,
300300
file_properties_ref=file_properties_ref,
@@ -303,8 +303,8 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
303303
str_properties=str_properties,
304304
date_properties=date_properties,
305305
datetime_properties=datetime_properties,
306-
int_32_properties=int_32_properties,
307-
int_64_properties=int_64_properties,
306+
int32_properties=int32_properties,
307+
int64_properties=int64_properties,
308308
float_properties=float_properties,
309309
double_properties=double_properties,
310310
file_properties=file_properties,
@@ -313,8 +313,8 @@ def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
313313
str_property_ref=str_property_ref,
314314
date_property_ref=date_property_ref,
315315
datetime_property_ref=datetime_property_ref,
316-
int_32_property_ref=int_32_property_ref,
317-
int_64_property_ref=int_64_property_ref,
316+
int32_property_ref=int32_property_ref,
317+
int64_property_ref=int64_property_ref,
318318
float_property_ref=float_property_ref,
319319
double_property_ref=double_property_ref,
320320
file_property_ref=file_property_ref,

openapi_python_client/parser/properties/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def build_union_property(
354354
sub_properties: List[Property] = []
355355
for i, sub_prop_data in enumerate(chain(data.anyOf, data.oneOf)):
356356
sub_prop, schemas = property_from_data(
357-
name=f"{name}_type{i}",
357+
name=f"{name}_type_{i}",
358358
required=required,
359359
data=sub_prop_data,
360360
schemas=schemas,

openapi_python_client/utils.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ def sanitize(value: str) -> str:
1212

1313

1414
def split_words(value: str) -> List[str]:
15-
"""Split a string on non-capital letters and known delimiters"""
16-
value = " ".join(re.split("([A-Z]?[a-z]+)", value))
15+
"""Split a string on words and known delimiters"""
16+
# We can't guess words if there is no capital letter
17+
if any(c.isupper() for c in value):
18+
value = " ".join(re.split("([A-Z]?[a-z]+)", value))
1719
return re.findall(rf"[^{delimiters}]+", value)
1820

1921

tests/test_utils.py

+16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
from openapi_python_client import utils
44

55

6+
@pytest.mark.parametrize(
7+
"before, after",
8+
[
9+
("connectionID", ["connection", "ID"]),
10+
("connection_id", ["connection", "id"]),
11+
("connection-id", ["connection", "id"]),
12+
("Response200", ["Response", "200"]),
13+
("Response200Okay", ["Response", "200", "Okay"]),
14+
("S3Config", ["S3", "Config"]),
15+
("s3config", ["s3config"]),
16+
],
17+
)
18+
def test_split_words(before, after):
19+
assert utils.split_words(before) == after
20+
21+
622
def test_snake_case_uppercase_str():
723
assert utils.snake_case("HTTP") == "http"
824
assert utils.snake_case("HTTP RESPONSE") == "http_response"

0 commit comments

Comments
 (0)