Skip to content

Commit 95b7eb3

Browse files
dbantyJordi Sanchez
and
Jordi Sanchez
authored
feat: support #/components/parameters references [#288, #615, #653]. Thanks @jsanchez7SC!
Closes #288. Co-authored-by: Jordi Sanchez <[email protected]>
1 parent d512d8b commit 95b7eb3

File tree

15 files changed

+951
-109
lines changed

15 files changed

+951
-109
lines changed

end_to_end_tests/custom-templates-golden-record/my_test_api_client/api/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from .default import DefaultEndpoints
66
from .location import LocationEndpoints
7+
from .parameter_references import ParameterReferencesEndpoints
78
from .parameters import ParametersEndpoints
89
from .responses import ResponsesEndpoints
910
from .tag1 import Tag1Endpoints
@@ -39,3 +40,7 @@ def location(cls) -> Type[LocationEndpoints]:
3940
@classmethod
4041
def true_(cls) -> Type[True_Endpoints]:
4142
return True_Endpoints
43+
44+
@classmethod
45+
def parameter_references(cls) -> Type[ParameterReferencesEndpoints]:
46+
return ParameterReferencesEndpoints
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
""" Contains methods for accessing the API Endpoints """
2+
3+
import types
4+
5+
from . import get_parameter_references_path_param
6+
7+
8+
class ParameterReferencesEndpoints:
9+
@classmethod
10+
def get_parameter_references_path_param(cls) -> types.ModuleType:
11+
"""
12+
Test different types of parameter references
13+
"""
14+
return get_parameter_references_path_param

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

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
from typing import Any, Dict
2+
3+
import httpx
4+
5+
from ...client import Client
6+
from ...types import UNSET, Response
7+
8+
9+
def _get_kwargs(
10+
path_param: str,
11+
*,
12+
client: Client,
13+
string_param: str,
14+
integer_param: int = 0,
15+
header_param: str,
16+
cookie_param: str,
17+
) -> Dict[str, Any]:
18+
url = "{}/parameter-references/{path_param}".format(client.base_url, path_param=path_param)
19+
20+
headers: Dict[str, str] = client.get_headers()
21+
cookies: Dict[str, Any] = client.get_cookies()
22+
23+
headers["header param"] = header_param
24+
25+
cookies["cookie param"] = cookie_param
26+
27+
params: Dict[str, Any] = {}
28+
params["string param"] = string_param
29+
30+
params["integer param"] = integer_param
31+
32+
params = {k: v for k, v in params.items() if v is not UNSET and v is not None}
33+
34+
return {
35+
"method": "get",
36+
"url": url,
37+
"headers": headers,
38+
"cookies": cookies,
39+
"timeout": client.get_timeout(),
40+
"params": params,
41+
}
42+
43+
44+
def _build_response(*, response: httpx.Response) -> Response[Any]:
45+
return Response(
46+
status_code=response.status_code,
47+
content=response.content,
48+
headers=response.headers,
49+
parsed=None,
50+
)
51+
52+
53+
def sync_detailed(
54+
path_param: str,
55+
*,
56+
client: Client,
57+
string_param: str,
58+
integer_param: int = 0,
59+
header_param: str,
60+
cookie_param: str,
61+
) -> Response[Any]:
62+
"""Test different types of parameter references
63+
64+
Args:
65+
path_param (str):
66+
string_param (str):
67+
integer_param (int):
68+
header_param (str):
69+
cookie_param (str):
70+
71+
Returns:
72+
Response[Any]
73+
"""
74+
75+
kwargs = _get_kwargs(
76+
path_param=path_param,
77+
client=client,
78+
string_param=string_param,
79+
integer_param=integer_param,
80+
header_param=header_param,
81+
cookie_param=cookie_param,
82+
)
83+
84+
response = httpx.request(
85+
verify=client.verify_ssl,
86+
**kwargs,
87+
)
88+
89+
return _build_response(response=response)
90+
91+
92+
async def asyncio_detailed(
93+
path_param: str,
94+
*,
95+
client: Client,
96+
string_param: str,
97+
integer_param: int = 0,
98+
header_param: str,
99+
cookie_param: str,
100+
) -> Response[Any]:
101+
"""Test different types of parameter references
102+
103+
Args:
104+
path_param (str):
105+
string_param (str):
106+
integer_param (int):
107+
header_param (str):
108+
cookie_param (str):
109+
110+
Returns:
111+
Response[Any]
112+
"""
113+
114+
kwargs = _get_kwargs(
115+
path_param=path_param,
116+
client=client,
117+
string_param=string_param,
118+
integer_param=integer_param,
119+
header_param=header_param,
120+
cookie_param=cookie_param,
121+
)
122+
123+
async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
124+
response = await _client.request(**kwargs)
125+
126+
return _build_response(response=response)

end_to_end_tests/openapi.json

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,30 @@
10821082
}
10831083
}
10841084
}
1085+
},
1086+
"/parameter-references/{path_param}": {
1087+
"get": {
1088+
"tags": [
1089+
"parameter-references"
1090+
],
1091+
"summary": "Test different types of parameter references",
1092+
"parameters": [
1093+
{
1094+
"$ref": "#/components/parameters/string-param"
1095+
},
1096+
{
1097+
"$ref": "#/components/parameters/integer-param"
1098+
},
1099+
{"$ref": "#/components/parameters/header-param"},
1100+
{"$ref": "#/components/parameters/cookie-param"},
1101+
{"$ref": "#/components/parameters/path-param"}
1102+
],
1103+
"responses": {
1104+
"200": {
1105+
"description": "Successful response"
1106+
}
1107+
}
1108+
}
10851109
}
10861110
},
10871111
"components": {
@@ -1956,6 +1980,70 @@
19561980
"type": "object",
19571981
"description": "A Model with periods in its reference"
19581982
}
1983+
},
1984+
"parameters": {
1985+
"integer-param": {
1986+
"name": "integer param",
1987+
"in": "query",
1988+
"required": false,
1989+
"style": "form",
1990+
"explode": true,
1991+
"schema": {
1992+
"type": "integer",
1993+
"default": 0
1994+
}
1995+
},
1996+
"string-param": {
1997+
"name": "string param",
1998+
"in": "query",
1999+
"required": false,
2000+
"style": "form",
2001+
"explode": true,
2002+
"schema": {
2003+
"type": "string"
2004+
}
2005+
},
2006+
"object-param": {
2007+
"name": "object param",
2008+
"in": "query",
2009+
"required": false,
2010+
"schema": {
2011+
"type": "object",
2012+
"properties": {
2013+
"date": {
2014+
"type": "string",
2015+
"format": "date"
2016+
},
2017+
"number": {
2018+
"type": "number"
2019+
}
2020+
}
2021+
}
2022+
},
2023+
"header-param": {
2024+
"name": "header param",
2025+
"in": "header",
2026+
"required": false,
2027+
"schema": {
2028+
"type": "string"
2029+
}
2030+
},
2031+
"cookie-param": {
2032+
"name": "cookie param",
2033+
"in": "cookie",
2034+
"required": false,
2035+
"schema": {
2036+
"type": "string"
2037+
}
2038+
},
2039+
"path-param": {
2040+
"name": "path_param",
2041+
"in": "path",
2042+
"required": true,
2043+
"schema": {
2044+
"type": "string"
2045+
}
2046+
}
19592047
}
19602048
}
19612049
}

openapi_python_client/parser/errors.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from enum import Enum
33
from typing import Optional
44

5-
__all__ = ["ErrorLevel", "GeneratorError", "ParseError", "PropertyError", "ValidationError"]
5+
__all__ = ["ErrorLevel", "GeneratorError", "ParseError", "PropertyError", "ValidationError", "ParameterError"]
66

77
from pydantic import BaseModel
88

@@ -39,5 +39,12 @@ class PropertyError(ParseError):
3939
header = "Problem creating a Property: "
4040

4141

42+
@dataclass
43+
class ParameterError(ParseError):
44+
"""Error raised when there's a problem creating a Parameter."""
45+
46+
header = "Problem creating a Parameter: "
47+
48+
4249
class ValidationError(Exception):
4350
"""Used internally to exit quickly from property parsing due to some internal exception."""

0 commit comments

Comments
 (0)