Skip to content

Commit 21f872e

Browse files
ramnesdbanty
andauthored
feat: Support common parameters for paths (#376). Thanks @ramnes!
* Support common parameters https://swagger.io/docs/specification/describing-parameters/#common * test: Added e2e snapshot test for common path parameters Co-authored-by: Dylan Anthony <[email protected]> Co-authored-by: Dylan Anthony <[email protected]>
1 parent e7d52b6 commit 21f872e

File tree

5 files changed

+170
-1
lines changed

5 files changed

+170
-1
lines changed

end_to_end_tests/golden-record/my_test_api_client/api/default/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from typing import Any, Dict, Union
2+
3+
import httpx
4+
5+
from ...client import Client
6+
from ...types import UNSET, Response, Unset
7+
8+
9+
def _get_kwargs(
10+
*,
11+
client: Client,
12+
common: Union[Unset, str] = UNSET,
13+
) -> Dict[str, Any]:
14+
url = "{}/common_parameters".format(client.base_url)
15+
16+
headers: Dict[str, Any] = client.get_headers()
17+
cookies: Dict[str, Any] = client.get_cookies()
18+
19+
params: Dict[str, Any] = {
20+
"common": common,
21+
}
22+
params = {k: v for k, v in params.items() if v is not UNSET and v is not None}
23+
24+
return {
25+
"url": url,
26+
"headers": headers,
27+
"cookies": cookies,
28+
"timeout": client.get_timeout(),
29+
"params": params,
30+
}
31+
32+
33+
def _build_response(*, response: httpx.Response) -> Response[None]:
34+
return Response(
35+
status_code=response.status_code,
36+
content=response.content,
37+
headers=response.headers,
38+
parsed=None,
39+
)
40+
41+
42+
def sync_detailed(
43+
*,
44+
client: Client,
45+
common: Union[Unset, str] = UNSET,
46+
) -> Response[None]:
47+
kwargs = _get_kwargs(
48+
client=client,
49+
common=common,
50+
)
51+
52+
response = httpx.get(
53+
**kwargs,
54+
)
55+
56+
return _build_response(response=response)
57+
58+
59+
async def asyncio_detailed(
60+
*,
61+
client: Client,
62+
common: Union[Unset, str] = UNSET,
63+
) -> Response[None]:
64+
kwargs = _get_kwargs(
65+
client=client,
66+
common=common,
67+
)
68+
69+
async with httpx.AsyncClient() as _client:
70+
response = await _client.get(**kwargs)
71+
72+
return _build_response(response=response)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from typing import Any, Dict, Union
2+
3+
import httpx
4+
5+
from ...client import Client
6+
from ...types import UNSET, Response, Unset
7+
8+
9+
def _get_kwargs(
10+
*,
11+
client: Client,
12+
common: Union[Unset, str] = UNSET,
13+
) -> Dict[str, Any]:
14+
url = "{}/common_parameters".format(client.base_url)
15+
16+
headers: Dict[str, Any] = client.get_headers()
17+
cookies: Dict[str, Any] = client.get_cookies()
18+
19+
params: Dict[str, Any] = {
20+
"common": common,
21+
}
22+
params = {k: v for k, v in params.items() if v is not UNSET and v is not None}
23+
24+
return {
25+
"url": url,
26+
"headers": headers,
27+
"cookies": cookies,
28+
"timeout": client.get_timeout(),
29+
"params": params,
30+
}
31+
32+
33+
def _build_response(*, response: httpx.Response) -> Response[None]:
34+
return Response(
35+
status_code=response.status_code,
36+
content=response.content,
37+
headers=response.headers,
38+
parsed=None,
39+
)
40+
41+
42+
def sync_detailed(
43+
*,
44+
client: Client,
45+
common: Union[Unset, str] = UNSET,
46+
) -> Response[None]:
47+
kwargs = _get_kwargs(
48+
client=client,
49+
common=common,
50+
)
51+
52+
response = httpx.post(
53+
**kwargs,
54+
)
55+
56+
return _build_response(response=response)
57+
58+
59+
async def asyncio_detailed(
60+
*,
61+
client: Client,
62+
common: Union[Unset, str] = UNSET,
63+
) -> Response[None]:
64+
kwargs = _get_kwargs(
65+
client=client,
66+
common=common,
67+
)
68+
69+
async with httpx.AsyncClient() as _client:
70+
response = await _client.post(**kwargs)
71+
72+
return _build_response(response=response)

end_to_end_tests/openapi.json

+21
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,27 @@
739739
}
740740
}
741741
}
742+
},
743+
"/common_parameters": {
744+
"parameters": [
745+
{
746+
"schema": {
747+
"type": "string"
748+
},
749+
"name": "common",
750+
"in": "query"
751+
}
752+
],
753+
"get": {
754+
"responses": {
755+
"200": {"description": "Success"}
756+
}
757+
},
758+
"post": {
759+
"responses": {
760+
"200": {"description": "Success"}
761+
}
762+
}
742763
}
743764
},
744765
"components": {

openapi_python_client/parser/openapi.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ def from_data(
5454
endpoint, schemas = Endpoint.from_data(
5555
data=operation, path=path, method=method, tag=tag, schemas=schemas, config=config
5656
)
57+
if not isinstance(endpoint, ParseError):
58+
endpoint, schemas = Endpoint._add_parameters(
59+
endpoint=endpoint, data=path_data, schemas=schemas, config=config
60+
)
5761
if isinstance(endpoint, ParseError):
5862
endpoint.header = (
5963
f"ERROR parsing {method.upper()} {path} within {tag}. Endpoint will not be generated."
@@ -209,7 +213,7 @@ def _add_responses(
209213

210214
@staticmethod
211215
def _add_parameters(
212-
*, endpoint: "Endpoint", data: oai.Operation, schemas: Schemas, config: Config
216+
*, endpoint: "Endpoint", data: Union[oai.Operation, oai.PathItem], schemas: Schemas, config: Config
213217
) -> Tuple[Union["Endpoint", ParseError], Schemas]:
214218
endpoint = deepcopy(endpoint)
215219
if data.parameters is None:

0 commit comments

Comments
 (0)