diff --git a/end_to_end_tests/custom-templates-golden-record/my_test_api_client/api/tests/__init__.py b/end_to_end_tests/custom-templates-golden-record/my_test_api_client/api/tests/__init__.py index 5455c2c70..b0615a2d2 100644 --- a/end_to_end_tests/custom-templates-golden-record/my_test_api_client/api/tests/__init__.py +++ b/end_to_end_tests/custom-templates-golden-record/my_test_api_client/api/tests/__init__.py @@ -14,6 +14,7 @@ no_response_tests_no_response_get, octet_stream_tests_octet_stream_get, post_form_data, + post_tests_json_body_string, test_inline_objects, token_with_cookie_auth_token_with_cookie_get, unsupported_content_tests_unsupported_content_get, @@ -86,6 +87,13 @@ def json_body_tests_json_body_post(cls) -> types.ModuleType: """ return json_body_tests_json_body_post + @classmethod + def post_tests_json_body_string(cls) -> types.ModuleType: + """ + Json Body Which is String + """ + return post_tests_json_body_string + @classmethod def defaults_tests_defaults_post(cls) -> types.ModuleType: """ diff --git a/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_tests_json_body_string.py b/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_tests_json_body_string.py new file mode 100644 index 000000000..8b076b59c --- /dev/null +++ b/end_to_end_tests/golden-record/my_test_api_client/api/tests/post_tests_json_body_string.py @@ -0,0 +1,110 @@ +from typing import Any, Dict, Optional, Union, cast + +import httpx + +from ...client import Client +from ...models.http_validation_error import HTTPValidationError +from ...types import Response + + +def _get_kwargs( + *, + client: Client, + json_body: str, +) -> Dict[str, Any]: + url = "{}/tests/json_body/string".format(client.base_url) + + headers: Dict[str, Any] = client.get_headers() + cookies: Dict[str, Any] = client.get_cookies() + + json_json_body = json_body + + return { + "url": url, + "headers": headers, + "cookies": cookies, + "timeout": client.get_timeout(), + "json": json_json_body, + } + + +def _parse_response(*, response: httpx.Response) -> Optional[Union[HTTPValidationError, str]]: + if response.status_code == 200: + response_200 = cast(str, response.json()) + return response_200 + if response.status_code == 422: + response_422 = HTTPValidationError.from_dict(response.json()) + + return response_422 + return None + + +def _build_response(*, response: httpx.Response) -> Response[Union[HTTPValidationError, str]]: + return Response( + status_code=response.status_code, + content=response.content, + headers=response.headers, + parsed=_parse_response(response=response), + ) + + +def sync_detailed( + *, + client: Client, + json_body: str, +) -> Response[Union[HTTPValidationError, str]]: + kwargs = _get_kwargs( + client=client, + json_body=json_body, + ) + + response = httpx.post( + verify=client.verify_ssl, + **kwargs, + ) + + return _build_response(response=response) + + +def sync( + *, + client: Client, + json_body: str, +) -> Optional[Union[HTTPValidationError, str]]: + """ """ + + return sync_detailed( + client=client, + json_body=json_body, + ).parsed + + +async def asyncio_detailed( + *, + client: Client, + json_body: str, +) -> Response[Union[HTTPValidationError, str]]: + kwargs = _get_kwargs( + client=client, + json_body=json_body, + ) + + async with httpx.AsyncClient(verify=client.verify_ssl) as _client: + response = await _client.post(**kwargs) + + return _build_response(response=response) + + +async def asyncio( + *, + client: Client, + json_body: str, +) -> Optional[Union[HTTPValidationError, str]]: + """ """ + + return ( + await asyncio_detailed( + client=client, + json_body=json_body, + ) + ).parsed diff --git a/end_to_end_tests/openapi.json b/end_to_end_tests/openapi.json index e03b60d56..46db24487 100644 --- a/end_to_end_tests/openapi.json +++ b/end_to_end_tests/openapi.json @@ -388,6 +388,46 @@ } } }, + "/tests/json_body/string": { + "post": { + "tags": [ + "tests" + ], + "summary": "Json Body Which is String", + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "string" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "success", + "content": { + "application/json": { + "schema": { + "type": "string" + } + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } + }, "/tests/defaults": { "post": { "tags": [ diff --git a/openapi_python_client/templates/endpoint_macros.py.jinja b/openapi_python_client/templates/endpoint_macros.py.jinja index 60baa4230..a0d1aa32f 100644 --- a/openapi_python_client/templates/endpoint_macros.py.jinja +++ b/openapi_python_client/templates/endpoint_macros.py.jinja @@ -67,6 +67,8 @@ params = {k: v for k, v in params.items() if v is not UNSET and v is not None} {% if property.template %} {% from "property_templates/" + property.template import transform %} {{ transform(property, property.python_name, destination) }} + {% else %} +{{ destination }} = {{ property.python_name }} {% endif %} {% endif %} {% endmacro %} diff --git a/openapi_python_client/templates/endpoint_module.py.jinja b/openapi_python_client/templates/endpoint_module.py.jinja index c8bb190ae..5ddd2591b 100644 --- a/openapi_python_client/templates/endpoint_module.py.jinja +++ b/openapi_python_client/templates/endpoint_module.py.jinja @@ -63,7 +63,7 @@ def _parse_response(*, response: httpx.Response) -> Optional[{{ return_string }} {% from "property_templates/" + response.prop.template import construct %} {{ construct(response.prop, response.source) | indent(8) }} {% else %} - {{ response.prop.python_name }} = {{ response.source }} + {{ response.prop.python_name }} = cast({{ response.prop.get_type_string() }}, {{ response.source }}) {% endif %} return {{ response.prop.python_name }} {% endfor %}