Skip to content

Commit 4743ae8

Browse files
committed
Refactored response handling to use the same schema generation as input properties.
1 parent c8ae42d commit 4743ae8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+467
-647
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Any request/response field that is not `required` and wasn't specified is now set to `UNSET` instead of `None`.
1313
- Values that are `UNSET` will not be sent along in API calls
14+
- Schemas defined with `type=object` will now be converted into classes, just like if they were created as ref components.
15+
The previous behavior was a combination of skipping and using generic Dicts for these schemas.
16+
- Response schema handling was unified with input schema handling, meaning that responses will behave differently than before.
17+
Specifically, instead of the content-type deciding what the generated Python type is, the schema itself will.
18+
- Instead of skipping input properties with no type, enum, anyOf, or oneOf declared, the property will be declared as `None`.
1419

1520
### Additions
1621

1722
- Added a `--custom-template-path` option for providing custom jinja2 templates (#231 - Thanks @erichulburd!).
1823
- Better compatibility for "required" (whether or not the field must be included) and "nullable" (whether or not the field can be null) (#205 & #208). Thanks @bowenwr & @emannguitar!
24+
- Support for all the same schemas in responses as are supported in parameters.
1925

2026
## 0.6.2 - 2020-11-03
2127

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Client = httpx.Client
66

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

1010
from dateutil.parser import isoparse
1111

@@ -16,9 +16,13 @@
1616

1717
def _parse_response(*, response: httpx.Response) -> Optional[Union[None, HTTPValidationError]]:
1818
if response.status_code == 200:
19-
return None
19+
response_200 = None
20+
21+
return response_200
2022
if response.status_code == 422:
21-
return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json()))
23+
response_422 = HTTPValidationError.from_dict(response.json())
24+
25+
return response_422
2226
return None
2327

2428

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_booleans.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
Client = httpx.Client
66

7+
from typing import List, cast
8+
79

810
def _parse_response(*, response: httpx.Response) -> Optional[List[bool]]:
911
if response.status_code == 200:
10-
return [bool(item) for item in cast(List[bool], response.json())]
12+
response_200 = cast(List[bool], response.json())
13+
14+
return response_200
1115
return None
1216

1317

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_floats.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
Client = httpx.Client
66

7+
from typing import List, cast
8+
79

810
def _parse_response(*, response: httpx.Response) -> Optional[List[float]]:
911
if response.status_code == 200:
10-
return [float(item) for item in cast(List[float], response.json())]
12+
response_200 = cast(List[float], response.json())
13+
14+
return response_200
1115
return None
1216

1317

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_integers.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
Client = httpx.Client
66

7+
from typing import List, cast
8+
79

810
def _parse_response(*, response: httpx.Response) -> Optional[List[int]]:
911
if response.status_code == 200:
10-
return [int(item) for item in cast(List[int], response.json())]
12+
response_200 = cast(List[int], response.json())
13+
14+
return response_200
1115
return None
1216

1317

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_basic_list_of_strings.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
Client = httpx.Client
66

7+
from typing import List, cast
8+
79

810
def _parse_response(*, response: httpx.Response) -> Optional[List[str]]:
911
if response.status_code == 200:
10-
return [str(item) for item in cast(List[str], response.json())]
12+
response_200 = cast(List[str], response.json())
13+
14+
return response_200
1115
return None
1216

1317

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/get_user_list.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Client = httpx.Client
66

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

1010
from ...models.a_model import AModel
1111
from ...models.an_enum import AnEnum
@@ -14,9 +14,17 @@
1414

1515
def _parse_response(*, response: httpx.Response) -> Optional[Union[List[AModel], HTTPValidationError]]:
1616
if response.status_code == 200:
17-
return [AModel.from_dict(item) for item in cast(List[Dict[str, Any]], response.json())]
17+
response_200 = []
18+
for response_200_item_data in response.json():
19+
response_200_item = AModel.from_dict(response_200_item_data)
20+
21+
response_200.append(response_200_item)
22+
23+
return response_200
1824
if response.status_code == 422:
19-
return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json()))
25+
response_422 = HTTPValidationError.from_dict(response.json())
26+
27+
return response_422
2028
return None
2129

2230

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/int_enum_tests_int_enum_post.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44

55
Client = httpx.Client
66

7+
from typing import Dict
8+
79
from ...models.an_int_enum import AnIntEnum
810
from ...models.http_validation_error import HTTPValidationError
911

1012

1113
def _parse_response(*, response: httpx.Response) -> Optional[Union[None, HTTPValidationError]]:
1214
if response.status_code == 200:
13-
return None
15+
response_200 = None
16+
17+
return response_200
1418
if response.status_code == 422:
15-
return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json()))
19+
response_422 = HTTPValidationError.from_dict(response.json())
20+
21+
return response_422
1622
return None
1723

1824

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/json_body_tests_json_body_post.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44

55
Client = httpx.Client
66

7-
from typing import Dict, cast
8-
97
from ...models.a_model import AModel
108
from ...models.http_validation_error import HTTPValidationError
119

1210

1311
def _parse_response(*, response: httpx.Response) -> Optional[Union[None, HTTPValidationError]]:
1412
if response.status_code == 200:
15-
return None
13+
response_200 = None
14+
15+
return response_200
1616
if response.status_code == 422:
17-
return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json()))
17+
response_422 = HTTPValidationError.from_dict(response.json())
18+
19+
return response_422
1820
return None
1921

2022

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/octet_stream_tests_octet_stream_get.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44

55
Client = httpx.Client
66

7+
from io import BytesIO
78

8-
def _parse_response(*, response: httpx.Response) -> Optional[bytes]:
9+
from ...types import File
10+
11+
12+
def _parse_response(*, response: httpx.Response) -> Optional[File]:
913
if response.status_code == 200:
10-
return bytes(response.content)
14+
response_200 = File(payload=BytesIO(response.content))
15+
16+
return response_200
1117
return None
1218

1319

14-
def _build_response(*, response: httpx.Response) -> httpx.Response[bytes]:
20+
def _build_response(*, response: httpx.Response) -> httpx.Response[File]:
1521
return httpx.Response(
1622
status_code=response.status_code,
1723
content=response.content,
@@ -23,7 +29,7 @@ def _build_response(*, response: httpx.Response) -> httpx.Response[bytes]:
2329
def httpx_request(
2430
*,
2531
client: Client,
26-
) -> httpx.Response[bytes]:
32+
) -> httpx.Response[File]:
2733

2834
response = client.request(
2935
"get",

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/optional_value_tests_optional_query_param.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,21 @@
44

55
Client = httpx.Client
66

7-
from typing import List, Union
7+
from typing import Dict, List, Union
88

99
from ...models.http_validation_error import HTTPValidationError
1010
from ...types import UNSET, Unset
1111

1212

1313
def _parse_response(*, response: httpx.Response) -> Optional[Union[None, HTTPValidationError]]:
1414
if response.status_code == 200:
15-
return None
15+
response_200 = None
16+
17+
return response_200
1618
if response.status_code == 422:
17-
return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json()))
19+
response_422 = HTTPValidationError.from_dict(response.json())
20+
21+
return response_422
1822
return None
1923

2024

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
1+
from typing import Optional
2+
13
import httpx
24

35
Client = httpx.Client
46

57
from ...models.json_body import JsonBody
8+
from ...models.response_200 import Response_200
9+
10+
11+
def _parse_response(*, response: httpx.Response) -> Optional[Response_200]:
12+
if response.status_code == 200:
13+
response_200 = Response_200.from_dict(response.json())
14+
15+
return response_200
16+
return None
617

718

8-
def _build_response(*, response: httpx.Response) -> httpx.Response[None]:
19+
def _build_response(*, response: httpx.Response) -> httpx.Response[Response_200]:
920
return httpx.Response(
1021
status_code=response.status_code,
1122
content=response.content,
1223
headers=response.headers,
13-
parsed=None,
24+
parsed=_parse_response(response=response),
1425
)
1526

1627

1728
def httpx_request(
1829
*,
1930
client: Client,
2031
json_body: JsonBody,
21-
) -> httpx.Response[None]:
32+
) -> httpx.Response[Response_200]:
2233

2334
json_json_body = json_body.to_dict()
2435

end_to_end_tests/golden-record-custom/custom_e2e/api/tests/upload_file_tests_upload_post.py

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

55
Client = httpx.Client
66

7-
from typing import Union
7+
from typing import Dict, Union, cast
88

99
from ...models.body_upload_file_tests_upload_post import BodyUploadFileTestsUploadPost
1010
from ...models.http_validation_error import HTTPValidationError
@@ -16,9 +16,13 @@ def _parse_response(*, response: httpx.Response) -> Optional[Union[
1616
HTTPValidationError
1717
]]:
1818
if response.status_code == 200:
19-
return None
19+
response_200 = None
20+
21+
return response_200
2022
if response.status_code == 422:
21-
return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json()))
23+
response_422 = HTTPValidationError.from_dict(response.json())
24+
25+
return response_422
2226
return None
2327

2428

end_to_end_tests/golden-record-custom/custom_e2e/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
from .different_enum import DifferentEnum
88
from .http_validation_error import HTTPValidationError
99
from .json_body import JsonBody
10+
from .response_200 import Response_200
1011
from .validation_error import ValidationError

end_to_end_tests/golden-record-custom/custom_e2e/models/body_upload_file_tests_upload_post.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from io import BytesIO
12
from typing import Any, Dict
23

34
import attr
@@ -22,7 +23,7 @@ def to_dict(self) -> Dict[str, Any]:
2223

2324
@staticmethod
2425
def from_dict(d: Dict[str, Any]) -> "BodyUploadFileTestsUploadPost":
25-
some_file = d["some_file"]
26+
some_file = File(payload=BytesIO(d["some_file"]))
2627

2728
return BodyUploadFileTestsUploadPost(
2829
some_file=some_file,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import Any, Dict
2+
3+
import attr
4+
5+
6+
@attr.s(auto_attribs=True)
7+
class Response_200:
8+
""" """
9+
10+
a_property: str
11+
12+
def to_dict(self) -> Dict[str, Any]:
13+
a_property = self.a_property
14+
15+
field_dict = {
16+
"a_property": a_property,
17+
}
18+
19+
return field_dict
20+
21+
@staticmethod
22+
def from_dict(d: Dict[str, Any]) -> "Response_200":
23+
a_property = d["a_property"]
24+
25+
return Response_200(
26+
a_property=a_property,
27+
)

end_to_end_tests/golden-record-custom/custom_e2e/models/validation_error.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, List
1+
from typing import Any, Dict, List, cast
22

33
import attr
44

@@ -27,7 +27,7 @@ def to_dict(self) -> Dict[str, Any]:
2727

2828
@staticmethod
2929
def from_dict(d: Dict[str, Any]) -> "ValidationError":
30-
loc = d["loc"]
30+
loc = cast(List[str], d["loc"])
3131

3232
msg = d["msg"]
3333

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import datetime
2-
from typing import Any, Dict, List, Optional, Union, cast
2+
from typing import Any, Dict, List, Optional, Union
33

44
import httpx
55
from dateutil.parser import isoparse
@@ -86,9 +86,13 @@ def _get_kwargs(
8686

8787
def _parse_response(*, response: httpx.Response) -> Optional[Union[None, HTTPValidationError]]:
8888
if response.status_code == 200:
89-
return None
89+
response_200 = None
90+
91+
return response_200
9092
if response.status_code == 422:
91-
return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json()))
93+
response_422 = HTTPValidationError.from_dict(response.json())
94+
95+
return response_422
9296
return None
9397

9498

0 commit comments

Comments
 (0)