Skip to content

Commit d14f579

Browse files
committed
parser / openapi / sanitize endpoints tags with snake case
1 parent af09640 commit d14f579

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2626

2727
### Fixes
2828

29+
- Sanitize endpoints tag during parsing (transform them to snake case), thus two endpoint tagged with respectivily `AMF Subscription Info (Document)` and `AmfSubscriptionInfo (Document)` will be considered to belongs to the same endpoint tagged `amf_subscription_info_document`
2930
- The generated `from_dict` and `to_dict` methods of models will now properly handle `nullable` and `not required` properties that are themselves generated models (#315). Thanks @forest-benchling!
3031

3132
## 0.7.3 - 2020-12-21

openapi_python_client/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ def _build_api(self) -> None:
235235

236236
endpoint_template = self.env.get_template("endpoint_module.py.jinja")
237237
for tag, collection in self.openapi.endpoint_collections_by_tag.items():
238-
tag = utils.snake_case(tag)
239238
tag_dir = api_dir / tag
240239
tag_dir.mkdir()
241240
(tag_dir / "__init__.py").touch()

openapi_python_client/parser/openapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def from_data(
4848
operation: Optional[oai.Operation] = getattr(path_data, method)
4949
if operation is None:
5050
continue
51-
tag = (operation.tags or ["default"])[0]
51+
tag = utils.snake_case((operation.tags or ["default"])[0])
5252
collection = endpoints_by_tag.setdefault(tag, EndpointCollection(tag=tag))
5353
endpoint, schemas = Endpoint.from_data(
5454
data=operation, path=path, method=method, tag=tag, schemas=schemas

tests/test_parser/test_openapi.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,3 +800,51 @@ def test_from_data_errors(self, mocker):
800800
assert result["default"].parse_errors[1].data == "3"
801801
assert result["tag_2"].parse_errors[0].data == "2"
802802
assert result_schemas == schemas_3
803+
804+
def test_from_data_tags_snake_case_sanitizer(self, mocker):
805+
from openapi_python_client.parser.openapi import Endpoint, EndpointCollection
806+
807+
path_1_put = oai.Operation.construct()
808+
path_1_post = oai.Operation.construct(tags=["AMF Subscription Info (Document)", "tag_3"])
809+
path_2_get = oai.Operation.construct()
810+
data = {
811+
"path_1": oai.PathItem.construct(post=path_1_post, put=path_1_put),
812+
"path_2": oai.PathItem.construct(get=path_2_get),
813+
}
814+
endpoint_1 = mocker.MagicMock(autospec=Endpoint, tag="default", relative_imports={"1", "2"})
815+
endpoint_2 = mocker.MagicMock(autospec=Endpoint, tag="AMFSubscriptionInfo (Document)", relative_imports={"2"})
816+
endpoint_3 = mocker.MagicMock(autospec=Endpoint, tag="default", relative_imports={"2", "3"})
817+
schemas_1 = mocker.MagicMock()
818+
schemas_2 = mocker.MagicMock()
819+
schemas_3 = mocker.MagicMock()
820+
endpoint_from_data = mocker.patch.object(
821+
Endpoint,
822+
"from_data",
823+
side_effect=[(endpoint_1, schemas_1), (endpoint_2, schemas_2), (endpoint_3, schemas_3)],
824+
)
825+
schemas = mocker.MagicMock()
826+
827+
result = EndpointCollection.from_data(data=data, schemas=schemas)
828+
829+
endpoint_from_data.assert_has_calls(
830+
[
831+
mocker.call(data=path_1_put, path="path_1", method="put", tag="default", schemas=schemas),
832+
mocker.call(
833+
data=path_1_post,
834+
path="path_1",
835+
method="post",
836+
tag="amf_subscription_info_document",
837+
schemas=schemas_1,
838+
),
839+
mocker.call(data=path_2_get, path="path_2", method="get", tag="default", schemas=schemas_2),
840+
],
841+
)
842+
assert result == (
843+
{
844+
"default": EndpointCollection("default", endpoints=[endpoint_1, endpoint_3]),
845+
"amf_subscription_info_document": EndpointCollection(
846+
"amf_subscription_info_document", endpoints=[endpoint_2]
847+
),
848+
},
849+
schemas_3,
850+
)

0 commit comments

Comments
 (0)