Skip to content

Commit 821dac8

Browse files
alexifmdbanty
andauthored
fix: treat period as a delimiter in names (#546). Thanks @alexifm!
If a model was named protoname.v1.MessageName (as if generated from `protoc`), the processed name would be Protonamev1MessageName and the model filename would be protonamev_1_message_name.py. With the change, it's ProtonameV1MessageName and protoname_v1_message_name.py, respectively. BREAKING CHANGE: Model names generated from OpenAPI names with periods (`.`) in them will be different. Co-authored-by: Dylan Anthony <[email protected]>
1 parent 12e8f92 commit 821dac8

File tree

5 files changed

+53
-3
lines changed

5 files changed

+53
-3
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from .import_ import Import
2424
from .model_from_all_of import ModelFromAllOf
2525
from .model_name import ModelName
26+
from .model_reference_with_periods import ModelReferenceWithPeriods
2627
from .model_with_additional_properties_inlined import ModelWithAdditionalPropertiesInlined
2728
from .model_with_additional_properties_inlined_additional_property import (
2829
ModelWithAdditionalPropertiesInlinedAdditionalProperty,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from typing import Any, Dict, List, Type, TypeVar
2+
3+
import attr
4+
5+
T = TypeVar("T", bound="ModelReferenceWithPeriods")
6+
7+
8+
@attr.s(auto_attribs=True)
9+
class ModelReferenceWithPeriods:
10+
"""A Model with periods in its reference"""
11+
12+
additional_properties: Dict[str, Any] = attr.ib(init=False, factory=dict)
13+
14+
def to_dict(self) -> Dict[str, Any]:
15+
16+
field_dict: Dict[str, Any] = {}
17+
field_dict.update(self.additional_properties)
18+
field_dict.update({})
19+
20+
return field_dict
21+
22+
@classmethod
23+
def from_dict(cls: Type[T], src_dict: Dict[str, Any]) -> T:
24+
d = src_dict.copy()
25+
model_reference_with_periods = cls()
26+
27+
model_reference_with_periods.additional_properties = d
28+
return model_reference_with_periods
29+
30+
@property
31+
def additional_keys(self) -> List[str]:
32+
return list(self.additional_properties.keys())
33+
34+
def __getitem__(self, key: str) -> Any:
35+
return self.additional_properties[key]
36+
37+
def __setitem__(self, key: str, value: Any) -> None:
38+
self.additional_properties[key] = value
39+
40+
def __delitem__(self, key: str) -> None:
41+
del self.additional_properties[key]
42+
43+
def __contains__(self, key: str) -> bool:
44+
return key in self.additional_properties

end_to_end_tests/openapi.json

+4
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,10 @@
19021902
},
19031903
"None": {
19041904
"type": "object"
1905+
},
1906+
"model.reference.with.Periods": {
1907+
"type": "object",
1908+
"description": "A Model with periods in its reference"
19051909
}
19061910
}
19071911
}

openapi_python_client/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from keyword import iskeyword
44
from typing import Any, List
55

6-
DELIMITERS = " _-"
6+
DELIMITERS = r"\. _-"
77

88

99
class PythonIdentifier(str):

tests/test_utils.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def test_empty_is_prefixed(self):
4747
("Response200Okay", ["Response", "200", "Okay"]),
4848
("S3Config", ["S3", "Config"]),
4949
("s3config", ["s3config"]),
50+
("fully.qualified.Name", ["fully", "qualified", "Name"]),
5051
],
5152
)
5253
def test_split_words(before, after):
@@ -83,8 +84,8 @@ def test_kebab_case():
8384
assert utils.kebab_case("keep_alive") == "keep-alive"
8485

8586

86-
def test__sanitize():
87-
assert utils.sanitize("something*~with lots_- of weird things}=") == "somethingwith lots_- of weird things"
87+
def test_sanitize():
88+
assert utils.sanitize("some.thing*~with lots_- of weird things}=") == "some.thingwith lots_- of weird things"
8889

8990

9091
def test_no_string_escapes():

0 commit comments

Comments
 (0)