Skip to content

Commit e87cd13

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

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

CHANGELOG.md

+1
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

-1
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

+1-1
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

+40
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,46 @@ def test_import_string_from_reference_with_prefix(self, mocker):
723723

724724

725725
class TestEndpointCollection:
726+
def test_from_data_tags_snake_case_sanitizer(self, mocker):
727+
from openapi_python_client.parser.openapi import Endpoint, EndpointCollection
728+
729+
path_1_put = oai.Operation.construct()
730+
path_1_post = oai.Operation.construct(tags=["AMF Subscription Info (Document)", "tag_3"])
731+
path_2_get = oai.Operation.construct()
732+
data = {
733+
"path_1": oai.PathItem.construct(post=path_1_post, put=path_1_put),
734+
"path_2": oai.PathItem.construct(get=path_2_get),
735+
}
736+
endpoint_1 = mocker.MagicMock(autospec=Endpoint, tag="default", relative_imports={"1", "2"})
737+
endpoint_2 = mocker.MagicMock(autospec=Endpoint, tag="AMFSubscriptionInfo (Document)", relative_imports={"2"})
738+
endpoint_3 = mocker.MagicMock(autospec=Endpoint, tag="default", relative_imports={"2", "3"})
739+
schemas_1 = mocker.MagicMock()
740+
schemas_2 = mocker.MagicMock()
741+
schemas_3 = mocker.MagicMock()
742+
endpoint_from_data = mocker.patch.object(
743+
Endpoint,
744+
"from_data",
745+
side_effect=[(endpoint_1, schemas_1), (endpoint_2, schemas_2), (endpoint_3, schemas_3)],
746+
)
747+
schemas = mocker.MagicMock()
748+
749+
result = EndpointCollection.from_data(data=data, schemas=schemas)
750+
751+
endpoint_from_data.assert_has_calls(
752+
[
753+
mocker.call(data=path_1_put, path="path_1", method="put", tag="default", schemas=schemas),
754+
mocker.call(data=path_1_post, path="path_1", method="post", tag="amf_subscription_info_document", schemas=schemas_1),
755+
mocker.call(data=path_2_get, path="path_2", method="get", tag="default", schemas=schemas_2),
756+
],
757+
)
758+
assert result == (
759+
{
760+
"default": EndpointCollection("default", endpoints=[endpoint_1, endpoint_3]),
761+
"amf_subscription_info_document": EndpointCollection("amf_subscription_info_document", endpoints=[endpoint_2]),
762+
},
763+
schemas_3,
764+
)
765+
726766
def test_from_data(self, mocker):
727767
from openapi_python_client.parser.openapi import Endpoint, EndpointCollection
728768

0 commit comments

Comments
 (0)