From ccf973e28b48f1044512ece176fab25eb6320a7c Mon Sep 17 00:00:00 2001 From: ramnes Date: Thu, 1 Apr 2021 22:00:09 +0200 Subject: [PATCH 1/2] Support common parameters https://swagger.io/docs/specification/describing-parameters/#common --- openapi_python_client/parser/openapi.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/openapi_python_client/parser/openapi.py b/openapi_python_client/parser/openapi.py index 2361c3c66..56b13664a 100644 --- a/openapi_python_client/parser/openapi.py +++ b/openapi_python_client/parser/openapi.py @@ -54,6 +54,8 @@ def from_data( endpoint, schemas = Endpoint.from_data( data=operation, path=path, method=method, tag=tag, schemas=schemas ) + if not isinstance(endpoint, ParseError): + endpoint, schemas = Endpoint._add_parameters(endpoint=endpoint, data=path_data, schemas=schemas) if isinstance(endpoint, ParseError): endpoint.header = ( f"ERROR parsing {method.upper()} {path} within {tag}. Endpoint will not be generated." @@ -206,7 +208,7 @@ def _add_responses(*, endpoint: "Endpoint", data: oai.Responses, schemas: Schema @staticmethod def _add_parameters( - *, endpoint: "Endpoint", data: oai.Operation, schemas: Schemas + *, endpoint: "Endpoint", data: Union[oai.Operation, oai.PathItem], schemas: Schemas ) -> Tuple[Union["Endpoint", ParseError], Schemas]: endpoint = deepcopy(endpoint) if data.parameters is None: From 92561373889ffe72b03dfcfe890d7eac57f3e719 Mon Sep 17 00:00:00 2001 From: Dylan Anthony Date: Sun, 25 Apr 2021 16:35:20 -0600 Subject: [PATCH 2/2] test: Added e2e snapshot test for common path parameters --- .../api/default/__init__.py | 0 .../api/default/get_common_parameters.py | 72 +++++++++++++++++++ .../api/default/post_common_parameters.py | 72 +++++++++++++++++++ end_to_end_tests/openapi.json | 21 ++++++ openapi_python_client/parser/openapi.py | 4 +- 5 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 end_to_end_tests/golden-record/my_test_api_client/api/default/__init__.py create mode 100644 end_to_end_tests/golden-record/my_test_api_client/api/default/get_common_parameters.py create mode 100644 end_to_end_tests/golden-record/my_test_api_client/api/default/post_common_parameters.py diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/default/__init__.py b/end_to_end_tests/golden-record/my_test_api_client/api/default/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/default/get_common_parameters.py b/end_to_end_tests/golden-record/my_test_api_client/api/default/get_common_parameters.py new file mode 100644 index 000000000..158e04b1d --- /dev/null +++ b/end_to_end_tests/golden-record/my_test_api_client/api/default/get_common_parameters.py @@ -0,0 +1,72 @@ +from typing import Any, Dict, Union + +import httpx + +from ...client import Client +from ...types import UNSET, Response, Unset + + +def _get_kwargs( + *, + client: Client, + common: Union[Unset, str] = UNSET, +) -> Dict[str, Any]: + url = "{}/common_parameters".format(client.base_url) + + headers: Dict[str, Any] = client.get_headers() + cookies: Dict[str, Any] = client.get_cookies() + + params: Dict[str, Any] = { + "common": common, + } + params = {k: v for k, v in params.items() if v is not UNSET and v is not None} + + return { + "url": url, + "headers": headers, + "cookies": cookies, + "timeout": client.get_timeout(), + "params": params, + } + + +def _build_response(*, response: httpx.Response) -> Response[None]: + return Response( + status_code=response.status_code, + content=response.content, + headers=response.headers, + parsed=None, + ) + + +def sync_detailed( + *, + client: Client, + common: Union[Unset, str] = UNSET, +) -> Response[None]: + kwargs = _get_kwargs( + client=client, + common=common, + ) + + response = httpx.get( + **kwargs, + ) + + return _build_response(response=response) + + +async def asyncio_detailed( + *, + client: Client, + common: Union[Unset, str] = UNSET, +) -> Response[None]: + kwargs = _get_kwargs( + client=client, + common=common, + ) + + async with httpx.AsyncClient() as _client: + response = await _client.get(**kwargs) + + return _build_response(response=response) diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/default/post_common_parameters.py b/end_to_end_tests/golden-record/my_test_api_client/api/default/post_common_parameters.py new file mode 100644 index 000000000..bb2d0f395 --- /dev/null +++ b/end_to_end_tests/golden-record/my_test_api_client/api/default/post_common_parameters.py @@ -0,0 +1,72 @@ +from typing import Any, Dict, Union + +import httpx + +from ...client import Client +from ...types import UNSET, Response, Unset + + +def _get_kwargs( + *, + client: Client, + common: Union[Unset, str] = UNSET, +) -> Dict[str, Any]: + url = "{}/common_parameters".format(client.base_url) + + headers: Dict[str, Any] = client.get_headers() + cookies: Dict[str, Any] = client.get_cookies() + + params: Dict[str, Any] = { + "common": common, + } + params = {k: v for k, v in params.items() if v is not UNSET and v is not None} + + return { + "url": url, + "headers": headers, + "cookies": cookies, + "timeout": client.get_timeout(), + "params": params, + } + + +def _build_response(*, response: httpx.Response) -> Response[None]: + return Response( + status_code=response.status_code, + content=response.content, + headers=response.headers, + parsed=None, + ) + + +def sync_detailed( + *, + client: Client, + common: Union[Unset, str] = UNSET, +) -> Response[None]: + kwargs = _get_kwargs( + client=client, + common=common, + ) + + response = httpx.post( + **kwargs, + ) + + return _build_response(response=response) + + +async def asyncio_detailed( + *, + client: Client, + common: Union[Unset, str] = UNSET, +) -> Response[None]: + kwargs = _get_kwargs( + client=client, + common=common, + ) + + async with httpx.AsyncClient() as _client: + response = await _client.post(**kwargs) + + return _build_response(response=response) diff --git a/end_to_end_tests/openapi.json b/end_to_end_tests/openapi.json index 57bee2721..f384dbbdd 100644 --- a/end_to_end_tests/openapi.json +++ b/end_to_end_tests/openapi.json @@ -739,6 +739,27 @@ } } } + }, + "/common_parameters": { + "parameters": [ + { + "schema": { + "type": "string" + }, + "name": "common", + "in": "query" + } + ], + "get": { + "responses": { + "200": {"description": "Success"} + } + }, + "post": { + "responses": { + "200": {"description": "Success"} + } + } } }, "components": { diff --git a/openapi_python_client/parser/openapi.py b/openapi_python_client/parser/openapi.py index 1719b008e..1ff3cd416 100644 --- a/openapi_python_client/parser/openapi.py +++ b/openapi_python_client/parser/openapi.py @@ -55,7 +55,9 @@ def from_data( data=operation, path=path, method=method, tag=tag, schemas=schemas, config=config ) if not isinstance(endpoint, ParseError): - endpoint, schemas = Endpoint._add_parameters(endpoint=endpoint, data=path_data, schemas=schemas) + endpoint, schemas = Endpoint._add_parameters( + endpoint=endpoint, data=path_data, schemas=schemas, config=config + ) if isinstance(endpoint, ParseError): endpoint.header = ( f"ERROR parsing {method.upper()} {path} within {tag}. Endpoint will not be generated."