Skip to content

Fix/sanitize-endpoints-tag-during-parsing #328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixes

- Endpoint tags are now sanitized during parsing to fix an issue where `My Tag` and `MyTag` are seen as two different tags but are then later unified, causing errors when creating directories. Thanks @p1-ra! (#328)
- Parser will softly ignore value error during schema responses' status code convertion from string to integer (not a number). Errors will be reported to the end user and parsing will continue to proceed (#327).
- 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!
- Fix deserialization of `None` and `Unset` properties for all types by unifying the checks (#334). Thanks @forest-benchling!
Expand Down
1 change: 0 additions & 1 deletion openapi_python_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ def _build_api(self) -> None:

endpoint_template = self.env.get_template("endpoint_module.py.jinja")
for tag, collection in self.openapi.endpoint_collections_by_tag.items():
tag = utils.snake_case(tag)
tag_dir = api_dir / tag
tag_dir.mkdir()
(tag_dir / "__init__.py").touch()
Expand Down
2 changes: 1 addition & 1 deletion openapi_python_client/parser/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def from_data(
operation: Optional[oai.Operation] = getattr(path_data, method)
if operation is None:
continue
tag = (operation.tags or ["default"])[0]
tag = utils.snake_case((operation.tags or ["default"])[0])
collection = endpoints_by_tag.setdefault(tag, EndpointCollection(tag=tag))
endpoint, schemas = Endpoint.from_data(
data=operation, path=path, method=method, tag=tag, schemas=schemas
Expand Down
48 changes: 48 additions & 0 deletions tests/test_parser/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,3 +829,51 @@ def test_from_data_errors(self, mocker):
assert result["default"].parse_errors[1].data == "3"
assert result["tag_2"].parse_errors[0].data == "2"
assert result_schemas == schemas_3

def test_from_data_tags_snake_case_sanitizer(self, mocker):
from openapi_python_client.parser.openapi import Endpoint, EndpointCollection

path_1_put = oai.Operation.construct()
path_1_post = oai.Operation.construct(tags=["AMF Subscription Info (Document)", "tag_3"])
path_2_get = oai.Operation.construct()
data = {
"path_1": oai.PathItem.construct(post=path_1_post, put=path_1_put),
"path_2": oai.PathItem.construct(get=path_2_get),
}
endpoint_1 = mocker.MagicMock(autospec=Endpoint, tag="default", relative_imports={"1", "2"})
endpoint_2 = mocker.MagicMock(autospec=Endpoint, tag="AMFSubscriptionInfo (Document)", relative_imports={"2"})
endpoint_3 = mocker.MagicMock(autospec=Endpoint, tag="default", relative_imports={"2", "3"})
schemas_1 = mocker.MagicMock()
schemas_2 = mocker.MagicMock()
schemas_3 = mocker.MagicMock()
endpoint_from_data = mocker.patch.object(
Endpoint,
"from_data",
side_effect=[(endpoint_1, schemas_1), (endpoint_2, schemas_2), (endpoint_3, schemas_3)],
)
schemas = mocker.MagicMock()

result = EndpointCollection.from_data(data=data, schemas=schemas)

endpoint_from_data.assert_has_calls(
[
mocker.call(data=path_1_put, path="path_1", method="put", tag="default", schemas=schemas),
mocker.call(
data=path_1_post,
path="path_1",
method="post",
tag="amf_subscription_info_document",
schemas=schemas_1,
),
mocker.call(data=path_2_get, path="path_2", method="get", tag="default", schemas=schemas_2),
],
)
assert result == (
{
"default": EndpointCollection("default", endpoints=[endpoint_1, endpoint_3]),
"amf_subscription_info_document": EndpointCollection(
"amf_subscription_info_document", endpoints=[endpoint_2]
),
},
schemas_3,
)