Skip to content

Commit 49580ec

Browse files
authored
fix: Basic types as JSON bodies and responses [#487 & #550]. Thanks @Gelbpunkt!
1 parent c8e2899 commit 49580ec

File tree

5 files changed

+161
-1
lines changed

5 files changed

+161
-1
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
no_response_tests_no_response_get,
1515
octet_stream_tests_octet_stream_get,
1616
post_form_data,
17+
post_tests_json_body_string,
1718
test_inline_objects,
1819
token_with_cookie_auth_token_with_cookie_get,
1920
unsupported_content_tests_unsupported_content_get,
@@ -86,6 +87,13 @@ def json_body_tests_json_body_post(cls) -> types.ModuleType:
8687
"""
8788
return json_body_tests_json_body_post
8889

90+
@classmethod
91+
def post_tests_json_body_string(cls) -> types.ModuleType:
92+
"""
93+
Json Body Which is String
94+
"""
95+
return post_tests_json_body_string
96+
8997
@classmethod
9098
def defaults_tests_defaults_post(cls) -> types.ModuleType:
9199
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
from typing import Any, Dict, Optional, Union, cast
2+
3+
import httpx
4+
5+
from ...client import Client
6+
from ...models.http_validation_error import HTTPValidationError
7+
from ...types import Response
8+
9+
10+
def _get_kwargs(
11+
*,
12+
client: Client,
13+
json_body: str,
14+
) -> Dict[str, Any]:
15+
url = "{}/tests/json_body/string".format(client.base_url)
16+
17+
headers: Dict[str, Any] = client.get_headers()
18+
cookies: Dict[str, Any] = client.get_cookies()
19+
20+
json_json_body = json_body
21+
22+
return {
23+
"url": url,
24+
"headers": headers,
25+
"cookies": cookies,
26+
"timeout": client.get_timeout(),
27+
"json": json_json_body,
28+
}
29+
30+
31+
def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidationError, str]]:
32+
if response.status_code == 200:
33+
response_200 = cast(str, response.json())
34+
return response_200
35+
if response.status_code == 422:
36+
response_422 = HTTPValidationError.from_dict(response.json())
37+
38+
return response_422
39+
return None
40+
41+
42+
def _build_response(*, response: httpx.Response) -> Response[Union[HTTPValidationError, str]]:
43+
return Response(
44+
status_code=response.status_code,
45+
content=response.content,
46+
headers=response.headers,
47+
parsed=_parse_response(response=response),
48+
)
49+
50+
51+
def sync_detailed(
52+
*,
53+
client: Client,
54+
json_body: str,
55+
) -> Response[Union[HTTPValidationError, str]]:
56+
kwargs = _get_kwargs(
57+
client=client,
58+
json_body=json_body,
59+
)
60+
61+
response = httpx.post(
62+
verify=client.verify_ssl,
63+
**kwargs,
64+
)
65+
66+
return _build_response(response=response)
67+
68+
69+
def sync(
70+
*,
71+
client: Client,
72+
json_body: str,
73+
) -> Optional[Union[HTTPValidationError, str]]:
74+
""" """
75+
76+
return sync_detailed(
77+
client=client,
78+
json_body=json_body,
79+
).parsed
80+
81+
82+
async def asyncio_detailed(
83+
*,
84+
client: Client,
85+
json_body: str,
86+
) -> Response[Union[HTTPValidationError, str]]:
87+
kwargs = _get_kwargs(
88+
client=client,
89+
json_body=json_body,
90+
)
91+
92+
async with httpx.AsyncClient(verify=client.verify_ssl) as _client:
93+
response = await _client.post(**kwargs)
94+
95+
return _build_response(response=response)
96+
97+
98+
async def asyncio(
99+
*,
100+
client: Client,
101+
json_body: str,
102+
) -> Optional[Union[HTTPValidationError, str]]:
103+
""" """
104+
105+
return (
106+
await asyncio_detailed(
107+
client=client,
108+
json_body=json_body,
109+
)
110+
).parsed

end_to_end_tests/openapi.json

+40
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,46 @@
388388
}
389389
}
390390
},
391+
"/tests/json_body/string": {
392+
"post": {
393+
"tags": [
394+
"tests"
395+
],
396+
"summary": "Json Body Which is String",
397+
"requestBody": {
398+
"content": {
399+
"application/json": {
400+
"schema": {
401+
"type": "string"
402+
}
403+
}
404+
},
405+
"required": true
406+
},
407+
"responses": {
408+
"200": {
409+
"description": "success",
410+
"content": {
411+
"application/json": {
412+
"schema": {
413+
"type": "string"
414+
}
415+
}
416+
}
417+
},
418+
"422": {
419+
"description": "Validation Error",
420+
"content": {
421+
"application/json": {
422+
"schema": {
423+
"$ref": "#/components/schemas/HTTPValidationError"
424+
}
425+
}
426+
}
427+
}
428+
}
429+
}
430+
},
391431
"/tests/defaults": {
392432
"post": {
393433
"tags": [

openapi_python_client/templates/endpoint_macros.py.jinja

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ params = {k: v for k, v in params.items() if v is not UNSET and v is not None}
6767
{% if property.template %}
6868
{% from "property_templates/" + property.template import transform %}
6969
{{ transform(property, property.python_name, destination) }}
70+
{% else %}
71+
{{ destination }} = {{ property.python_name }}
7072
{% endif %}
7173
{% endif %}
7274
{% endmacro %}

openapi_python_client/templates/endpoint_module.py.jinja

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def _parse_response(*, response: httpx.Response) -> Optional[{{ return_string }}
6363
{% from "property_templates/" + response.prop.template import construct %}
6464
{{ construct(response.prop, response.source) | indent(8) }}
6565
{% else %}
66-
{{ response.prop.python_name }} = {{ response.source }}
66+
{{ response.prop.python_name }} = cast({{ response.prop.get_type_string() }}, {{ response.source }})
6767
{% endif %}
6868
return {{ response.prop.python_name }}
6969
{% endfor %}

0 commit comments

Comments
 (0)