Skip to content

Commit 4cc04b3

Browse files
committed
Fix forward references and properly allow Unset for ModelPropertys
1 parent dc6971d commit 4cc04b3

File tree

17 files changed

+534
-104
lines changed

17 files changed

+534
-104
lines changed

end_to_end_tests/golden-record-custom/my_test_api_client/api/tests/defaults_tests_defaults_post.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
Client = httpx.Client
66

77
import datetime
8-
from typing import Dict, List, Union, cast
8+
from typing import List, Union, cast
99

1010
from dateutil.parser import isoparse
1111

1212
from ...models.an_enum import AnEnum
13-
from ...models.dict_prop import DictProp
1413
from ...models.http_validation_error import HTTPValidationError
1514
from ...types import UNSET, Unset
1615

@@ -35,7 +34,6 @@ def _build_response(*, response: httpx.Response) -> httpx.Response[Union[None, H
3534
def httpx_request(
3635
*,
3736
client: Client,
38-
json_body: DictProp,
3937
string_prop: Union[Unset, str] = "the default string",
4038
datetime_prop: Union[Unset, datetime.datetime] = isoparse("1010-10-10T00:00:00"),
4139
date_prop: Union[Unset, datetime.date] = isoparse("1010-10-10").date(),
@@ -95,12 +93,9 @@ def httpx_request(
9593
if enum_prop is not UNSET:
9694
params["enum_prop"] = json_enum_prop
9795

98-
json_json_body = json_body.to_dict()
99-
10096
response = client.request(
10197
"post",
10298
"/tests/defaults",
103-
json=json_json_body,
10499
params=params,
105100
)
106101

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from typing import Optional
2+
3+
import httpx
4+
5+
Client = httpx.Client
6+
7+
from typing import Dict, cast
8+
9+
from ...models.a_model import AModel
10+
from ...models.http_validation_error import HTTPValidationError
11+
12+
13+
def _parse_response(*, response: httpx.Response) -> Optional[Union[None, HTTPValidationError]]:
14+
if response.status_code == 200:
15+
return None
16+
if response.status_code == 422:
17+
return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json()))
18+
return None
19+
20+
21+
def _build_response(*, response: httpx.Response) -> httpx.Response[Union[None, HTTPValidationError]]:
22+
return httpx.Response(
23+
status_code=response.status_code,
24+
content=response.content,
25+
headers=response.headers,
26+
parsed=_parse_response(response=response),
27+
)
28+
29+
30+
def httpx_request(
31+
*,
32+
client: Client,
33+
json_body: AModel,
34+
) -> httpx.Response[Union[None, HTTPValidationError]]:
35+
36+
json_json_body = json_body.to_dict()
37+
38+
response = client.request(
39+
"post",
40+
"/tests/json_body",
41+
json=json_json_body,
42+
)
43+
44+
return _build_response(response=response)

end_to_end_tests/golden-record-custom/my_test_api_client/api/tests/test_inline_objects.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from typing import Optional
2-
31
import httpx
42

53
Client = httpx.Client
64

5+
from typing import Dict, Union
6+
77
from ...models.json_body import JsonBody
88
from ...types import UNSET, Unset
99

@@ -20,10 +20,10 @@ def _build_response(*, response: httpx.Response) -> httpx.Response[None]:
2020
def httpx_request(
2121
*,
2222
client: Client,
23-
json_body: Optional[JsonBody],
23+
json_body: Union[JsonBody, Unset],
2424
) -> httpx.Response[None]:
2525

26-
json_json_body: Optional[JsonBody] = UNSET
26+
json_json_body: Dict[str, Any] = UNSET
2727
if not isinstance(json_body, Unset):
2828
json_json_body = json_body.to_dict()
2929

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
""" Contains all the data models used in inputs/outputs """
22

3+
from .a_model import AModel
34
from .an_enum import AnEnum
45
from .an_int_enum import AnIntEnum
56
from .body_upload_file_tests_upload_post import BodyUploadFileTestsUploadPost
6-
from .dict_prop import DictProp
77
from .different_enum import DifferentEnum
8+
from .http_validation_error import HTTPValidationError
89
from .json_body import JsonBody
910
from .validation_error import ValidationError
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import datetime
2+
from typing import Any, Dict, List, Optional, Union
3+
4+
import attr
5+
from dateutil.parser import isoparse
6+
7+
from ..models.an_enum import AnEnum
8+
from ..models.different_enum import DifferentEnum
9+
from ..types import UNSET, Unset
10+
11+
12+
@attr.s(auto_attribs=True)
13+
class AModel:
14+
""" A Model for testing all the ways custom objects can be used """
15+
16+
an_enum_value: AnEnum
17+
a_camel_date_time: Union[datetime.datetime, datetime.date]
18+
a_date: datetime.date
19+
required_not_nullable: str
20+
required_nullable: Optional[str]
21+
nested_list_of_enums: Union[Unset, List[List[DifferentEnum]]] = UNSET
22+
attr_1_leading_digit: Union[Unset, str] = UNSET
23+
not_required_nullable: Union[Unset, Optional[str]] = UNSET
24+
not_required_not_nullable: Union[Unset, str] = UNSET
25+
26+
def to_dict(self) -> Dict[str, Any]:
27+
an_enum_value = self.an_enum_value.value
28+
29+
if isinstance(self.a_camel_date_time, datetime.datetime):
30+
a_camel_date_time = self.a_camel_date_time.isoformat()
31+
32+
else:
33+
a_camel_date_time = self.a_camel_date_time.isoformat()
34+
35+
a_date = self.a_date.isoformat()
36+
37+
required_not_nullable = self.required_not_nullable
38+
nested_list_of_enums: Union[Unset, List[Any]] = UNSET
39+
if not isinstance(self.nested_list_of_enums, Unset):
40+
nested_list_of_enums = []
41+
for nested_list_of_enums_item_data in self.nested_list_of_enums:
42+
nested_list_of_enums_item = []
43+
for nested_list_of_enums_item_item_data in nested_list_of_enums_item_data:
44+
nested_list_of_enums_item_item = nested_list_of_enums_item_item_data.value
45+
46+
nested_list_of_enums_item.append(nested_list_of_enums_item_item)
47+
48+
nested_list_of_enums.append(nested_list_of_enums_item)
49+
50+
attr_1_leading_digit = self.attr_1_leading_digit
51+
required_nullable = self.required_nullable
52+
not_required_nullable = self.not_required_nullable
53+
not_required_not_nullable = self.not_required_not_nullable
54+
55+
field_dict = {
56+
"an_enum_value": an_enum_value,
57+
"aCamelDateTime": a_camel_date_time,
58+
"a_date": a_date,
59+
"required_not_nullable": required_not_nullable,
60+
"required_nullable": required_nullable,
61+
}
62+
if nested_list_of_enums is not UNSET:
63+
field_dict["nested_list_of_enums"] = nested_list_of_enums
64+
if attr_1_leading_digit is not UNSET:
65+
field_dict["1_leading_digit"] = attr_1_leading_digit
66+
if not_required_nullable is not UNSET:
67+
field_dict["not_required_nullable"] = not_required_nullable
68+
if not_required_not_nullable is not UNSET:
69+
field_dict["not_required_not_nullable"] = not_required_not_nullable
70+
71+
return field_dict
72+
73+
@staticmethod
74+
def from_dict(d: Dict[str, Any]) -> "AModel":
75+
an_enum_value = AnEnum(d["an_enum_value"])
76+
77+
def _parse_a_camel_date_time(data: Dict[str, Any]) -> Union[datetime.datetime, datetime.date]:
78+
a_camel_date_time: Union[datetime.datetime, datetime.date]
79+
try:
80+
a_camel_date_time = isoparse(d["aCamelDateTime"])
81+
82+
return a_camel_date_time
83+
except: # noqa: E722
84+
pass
85+
a_camel_date_time = isoparse(d["aCamelDateTime"]).date()
86+
87+
return a_camel_date_time
88+
89+
a_camel_date_time = _parse_a_camel_date_time(d["aCamelDateTime"])
90+
91+
a_date = isoparse(d["a_date"]).date()
92+
93+
required_not_nullable = d["required_not_nullable"]
94+
95+
nested_list_of_enums = []
96+
for nested_list_of_enums_item_data in d.get("nested_list_of_enums", UNSET) or []:
97+
nested_list_of_enums_item = []
98+
for nested_list_of_enums_item_item_data in nested_list_of_enums_item_data:
99+
nested_list_of_enums_item_item = DifferentEnum(nested_list_of_enums_item_item_data)
100+
101+
nested_list_of_enums_item.append(nested_list_of_enums_item_item)
102+
103+
nested_list_of_enums.append(nested_list_of_enums_item)
104+
105+
attr_1_leading_digit = d.get("1_leading_digit", UNSET)
106+
107+
required_nullable = d["required_nullable"]
108+
109+
not_required_nullable = d.get("not_required_nullable", UNSET)
110+
111+
not_required_not_nullable = d.get("not_required_not_nullable", UNSET)
112+
113+
return AModel(
114+
an_enum_value=an_enum_value,
115+
a_camel_date_time=a_camel_date_time,
116+
a_date=a_date,
117+
required_not_nullable=required_not_nullable,
118+
nested_list_of_enums=nested_list_of_enums,
119+
attr_1_leading_digit=attr_1_leading_digit,
120+
required_nullable=required_nullable,
121+
not_required_nullable=not_required_nullable,
122+
not_required_not_nullable=not_required_not_nullable,
123+
)

end_to_end_tests/golden-record-custom/my_test_api_client/models/dict_prop.py

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import Any, Dict, List, Union
2+
3+
import attr
4+
5+
from ..models.validation_error import ValidationError
6+
from ..types import UNSET, Unset
7+
8+
9+
@attr.s(auto_attribs=True)
10+
class HTTPValidationError:
11+
""" """
12+
13+
detail: Union[Unset, List[ValidationError]] = UNSET
14+
15+
def to_dict(self) -> Dict[str, Any]:
16+
detail: Union[Unset, List[Any]] = UNSET
17+
if not isinstance(self.detail, Unset):
18+
detail = []
19+
for detail_item_data in self.detail:
20+
detail_item = detail_item_data.to_dict()
21+
22+
detail.append(detail_item)
23+
24+
field_dict = {}
25+
if detail is not UNSET:
26+
field_dict["detail"] = detail
27+
28+
return field_dict
29+
30+
@staticmethod
31+
def from_dict(d: Dict[str, Any]) -> "HTTPValidationError":
32+
detail = []
33+
for detail_item_data in d.get("detail", UNSET) or []:
34+
detail_item = ValidationError.from_dict(detail_item_data)
35+
36+
detail.append(detail_item)
37+
38+
return HTTPValidationError(
39+
detail=detail,
40+
)

end_to_end_tests/golden-record/my_test_api_client/api/tests/defaults_tests_defaults_post.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66

77
from ...client import Client
88
from ...models.an_enum import AnEnum
9-
from ...models.dict_prop import DictProp
109
from ...models.http_validation_error import HTTPValidationError
1110
from ...types import UNSET, Response, Unset
1211

1312

1413
def _get_kwargs(
1514
*,
1615
client: Client,
17-
json_body: DictProp,
1816
string_prop: Union[Unset, str] = "the default string",
1917
datetime_prop: Union[Unset, datetime.datetime] = isoparse("1010-10-10T00:00:00"),
2018
date_prop: Union[Unset, datetime.date] = isoparse("1010-10-10").date(),
@@ -77,14 +75,11 @@ def _get_kwargs(
7775
if enum_prop is not UNSET:
7876
params["enum_prop"] = json_enum_prop
7977

80-
json_json_body = json_body.to_dict()
81-
8278
return {
8379
"url": url,
8480
"headers": headers,
8581
"cookies": client.get_cookies(),
8682
"timeout": client.get_timeout(),
87-
"json": json_json_body,
8883
"params": params,
8984
}
9085

@@ -109,7 +104,6 @@ def _build_response(*, response: httpx.Response) -> Response[Union[None, HTTPVal
109104
def sync_detailed(
110105
*,
111106
client: Client,
112-
json_body: DictProp,
113107
string_prop: Union[Unset, str] = "the default string",
114108
datetime_prop: Union[Unset, datetime.datetime] = isoparse("1010-10-10T00:00:00"),
115109
date_prop: Union[Unset, datetime.date] = isoparse("1010-10-10").date(),
@@ -122,7 +116,6 @@ def sync_detailed(
122116
) -> Response[Union[None, HTTPValidationError]]:
123117
kwargs = _get_kwargs(
124118
client=client,
125-
json_body=json_body,
126119
string_prop=string_prop,
127120
datetime_prop=datetime_prop,
128121
date_prop=date_prop,
@@ -144,7 +137,6 @@ def sync_detailed(
144137
def sync(
145138
*,
146139
client: Client,
147-
json_body: DictProp,
148140
string_prop: Union[Unset, str] = "the default string",
149141
datetime_prop: Union[Unset, datetime.datetime] = isoparse("1010-10-10T00:00:00"),
150142
date_prop: Union[Unset, datetime.date] = isoparse("1010-10-10").date(),
@@ -159,7 +151,6 @@ def sync(
159151

160152
return sync_detailed(
161153
client=client,
162-
json_body=json_body,
163154
string_prop=string_prop,
164155
datetime_prop=datetime_prop,
165156
date_prop=date_prop,
@@ -175,7 +166,6 @@ def sync(
175166
async def asyncio_detailed(
176167
*,
177168
client: Client,
178-
json_body: DictProp,
179169
string_prop: Union[Unset, str] = "the default string",
180170
datetime_prop: Union[Unset, datetime.datetime] = isoparse("1010-10-10T00:00:00"),
181171
date_prop: Union[Unset, datetime.date] = isoparse("1010-10-10").date(),
@@ -188,7 +178,6 @@ async def asyncio_detailed(
188178
) -> Response[Union[None, HTTPValidationError]]:
189179
kwargs = _get_kwargs(
190180
client=client,
191-
json_body=json_body,
192181
string_prop=string_prop,
193182
datetime_prop=datetime_prop,
194183
date_prop=date_prop,
@@ -209,7 +198,6 @@ async def asyncio_detailed(
209198
async def asyncio(
210199
*,
211200
client: Client,
212-
json_body: DictProp,
213201
string_prop: Union[Unset, str] = "the default string",
214202
datetime_prop: Union[Unset, datetime.datetime] = isoparse("1010-10-10T00:00:00"),
215203
date_prop: Union[Unset, datetime.date] = isoparse("1010-10-10").date(),
@@ -225,7 +213,6 @@ async def asyncio(
225213
return (
226214
await asyncio_detailed(
227215
client=client,
228-
json_body=json_body,
229216
string_prop=string_prop,
230217
datetime_prop=datetime_prop,
231218
date_prop=date_prop,

0 commit comments

Comments
 (0)