diff --git a/CHANGELOG.md b/CHANGELOG.md index e73136e39..27341ec60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 0.5.3 - Unrelease +### Fixes +- JSON bodies will now be assigned correctly in generated clients(#139 & #147). Thanks @pawamoy! + + ## 0.5.2 - 2020-08-06 ### Additions - Added `project_name_override` and `package_name_override` config options to override the name of the generated project/package (#123) diff --git a/end_to_end_tests/fastapi_app/__init__.py b/end_to_end_tests/fastapi_app/__init__.py index 47df62a13..8bccdeec2 100644 --- a/end_to_end_tests/fastapi_app/__init__.py +++ b/end_to_end_tests/fastapi_app/__init__.py @@ -61,7 +61,13 @@ async def upload_file(some_file: UploadFile = File(...)): return (some_file.filename, some_file.content_type, data) -app.include_router(test_router, prefix="/tests", tags=["users"]) +@test_router.post("/json_body") +def json_body(body: AModel): + """ Try sending a JSON body """ + return + + +app.include_router(test_router, prefix="/tests", tags=["tests"]) def generate_openapi_json(): diff --git a/end_to_end_tests/fastapi_app/openapi.json b/end_to_end_tests/fastapi_app/openapi.json index 975ad3c5e..4c3a796ff 100644 --- a/end_to_end_tests/fastapi_app/openapi.json +++ b/end_to_end_tests/fastapi_app/openapi.json @@ -29,7 +29,7 @@ "/tests/": { "get": { "tags": [ - "users" + "tests" ], "summary": "Get List", "description": "Get a list of things ", @@ -97,7 +97,7 @@ "/tests/upload": { "post": { "tags": [ - "users" + "tests" ], "summary": "Upload File", "description": "Upload a file ", @@ -133,6 +133,46 @@ } } } + }, + "/tests/json_body": { + "post": { + "tags": [ + "tests" + ], + "summary": "Json Body", + "description": "Try sending a JSON body ", + "operationId": "json_body_tests_json_body_post", + "requestBody": { + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/AModel" + } + } + }, + "required": true + }, + "responses": { + "200": { + "description": "Successful Response", + "content": { + "application/json": { + "schema": {} + } + } + }, + "422": { + "description": "Validation Error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/HTTPValidationError" + } + } + } + } + } + } } }, "components": { diff --git a/end_to_end_tests/golden-master/my_test_api_client/api/users.py b/end_to_end_tests/golden-master/my_test_api_client/api/tests.py similarity index 78% rename from end_to_end_tests/golden-master/my_test_api_client/api/users.py rename to end_to_end_tests/golden-master/my_test_api_client/api/tests.py index 123666b58..68d7d087d 100644 --- a/end_to_end_tests/golden-master/my_test_api_client/api/users.py +++ b/end_to_end_tests/golden-master/my_test_api_client/api/tests.py @@ -65,3 +65,24 @@ def upload_file_tests_upload_post( return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json())) else: raise ApiResponseError(response=response) + + +def json_body_tests_json_body_post( + *, client: Client, json_body: AModel, +) -> Union[ + None, HTTPValidationError, +]: + + """ Try sending a JSON body """ + url = "{}/tests/json_body".format(client.base_url) + + json_json_body = json_body.to_dict() + + response = httpx.post(url=url, headers=client.get_headers(), json=json_json_body,) + + if response.status_code == 200: + return None + if response.status_code == 422: + return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json())) + else: + raise ApiResponseError(response=response) diff --git a/end_to_end_tests/golden-master/my_test_api_client/async_api/users.py b/end_to_end_tests/golden-master/my_test_api_client/async_api/tests.py similarity index 77% rename from end_to_end_tests/golden-master/my_test_api_client/async_api/users.py rename to end_to_end_tests/golden-master/my_test_api_client/async_api/tests.py index 92ec5fdbe..19dfb3f8c 100644 --- a/end_to_end_tests/golden-master/my_test_api_client/async_api/users.py +++ b/end_to_end_tests/golden-master/my_test_api_client/async_api/tests.py @@ -67,3 +67,25 @@ async def upload_file_tests_upload_post( return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json())) else: raise ApiResponseError(response=response) + + +async def json_body_tests_json_body_post( + *, client: Client, json_body: AModel, +) -> Union[ + None, HTTPValidationError, +]: + + """ Try sending a JSON body """ + url = "{}/tests/json_body".format(client.base_url,) + + json_json_body = json_body.to_dict() + + async with httpx.AsyncClient() as _client: + response = await _client.post(url=url, headers=client.get_headers(), json=json_json_body,) + + if response.status_code == 200: + return None + if response.status_code == 422: + return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json())) + else: + raise ApiResponseError(response=response) diff --git a/openapi_python_client/templates/async_endpoint_module.pyi b/openapi_python_client/templates/async_endpoint_module.pyi index 3c5368f1b..4119f0651 100644 --- a/openapi_python_client/templates/async_endpoint_module.pyi +++ b/openapi_python_client/templates/async_endpoint_module.pyi @@ -64,7 +64,7 @@ async def {{ endpoint.name | snakecase }}( files=multipart_data.to_dict(), {% endif %} {% if endpoint.json_body %} - json={{ endpoint.json_body.python_name }}, + json={{ "json_" + endpoint.json_body.python_name }}, {% endif %} {% if endpoint.query_parameters %} params=params, diff --git a/openapi_python_client/templates/endpoint_module.pyi b/openapi_python_client/templates/endpoint_module.pyi index 176fe94af..a61d51495 100644 --- a/openapi_python_client/templates/endpoint_module.pyi +++ b/openapi_python_client/templates/endpoint_module.pyi @@ -65,7 +65,7 @@ def {{ endpoint.name | snakecase }}( files=multipart_data.to_dict(), {% endif %} {% if endpoint.json_body %} - json={{ endpoint.json_body.python_name }}, + json={{ "json_" + endpoint.json_body.python_name }}, {% endif %} {% if endpoint.query_parameters %} params=params, diff --git a/tests/test_templates/async_endpoint_module.py b/tests/test_templates/async_endpoint_module.py index 67db4351b..106fdfb7d 100644 --- a/tests/test_templates/async_endpoint_module.py +++ b/tests/test_templates/async_endpoint_module.py @@ -61,7 +61,7 @@ async def camel_case( headers=client.get_headers(), data=asdict(form_data), files=multipart_data.to_dict(), - json=json_body, + json=json_json_body, ) if response.status_code == 200: