Skip to content

Commit c0d101b

Browse files
authored
Fix JSON bodies in generated endpoints. Fixes #139 (#147)
1 parent b0a2c07 commit c0d101b

File tree

8 files changed

+100
-6
lines changed

8 files changed

+100
-6
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77

8+
## 0.5.3 - Unrelease
9+
### Fixes
10+
- JSON bodies will now be assigned correctly in generated clients(#139 & #147). Thanks @pawamoy!
11+
12+
813
## 0.5.2 - 2020-08-06
914
### Additions
1015
- Added `project_name_override` and `package_name_override` config options to override the name of the generated project/package (#123)

end_to_end_tests/fastapi_app/__init__.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ async def upload_file(some_file: UploadFile = File(...)):
6161
return (some_file.filename, some_file.content_type, data)
6262

6363

64-
app.include_router(test_router, prefix="/tests", tags=["users"])
64+
@test_router.post("/json_body")
65+
def json_body(body: AModel):
66+
""" Try sending a JSON body """
67+
return
68+
69+
70+
app.include_router(test_router, prefix="/tests", tags=["tests"])
6571

6672

6773
def generate_openapi_json():

end_to_end_tests/fastapi_app/openapi.json

+42-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"/tests/": {
3030
"get": {
3131
"tags": [
32-
"users"
32+
"tests"
3333
],
3434
"summary": "Get List",
3535
"description": "Get a list of things ",
@@ -97,7 +97,7 @@
9797
"/tests/upload": {
9898
"post": {
9999
"tags": [
100-
"users"
100+
"tests"
101101
],
102102
"summary": "Upload File",
103103
"description": "Upload a file ",
@@ -133,6 +133,46 @@
133133
}
134134
}
135135
}
136+
},
137+
"/tests/json_body": {
138+
"post": {
139+
"tags": [
140+
"tests"
141+
],
142+
"summary": "Json Body",
143+
"description": "Try sending a JSON body ",
144+
"operationId": "json_body_tests_json_body_post",
145+
"requestBody": {
146+
"content": {
147+
"application/json": {
148+
"schema": {
149+
"$ref": "#/components/schemas/AModel"
150+
}
151+
}
152+
},
153+
"required": true
154+
},
155+
"responses": {
156+
"200": {
157+
"description": "Successful Response",
158+
"content": {
159+
"application/json": {
160+
"schema": {}
161+
}
162+
}
163+
},
164+
"422": {
165+
"description": "Validation Error",
166+
"content": {
167+
"application/json": {
168+
"schema": {
169+
"$ref": "#/components/schemas/HTTPValidationError"
170+
}
171+
}
172+
}
173+
}
174+
}
175+
}
136176
}
137177
},
138178
"components": {

end_to_end_tests/golden-master/my_test_api_client/api/users.py renamed to end_to_end_tests/golden-master/my_test_api_client/api/tests.py

+21
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,24 @@ def upload_file_tests_upload_post(
6565
return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json()))
6666
else:
6767
raise ApiResponseError(response=response)
68+
69+
70+
def json_body_tests_json_body_post(
71+
*, client: Client, json_body: AModel,
72+
) -> Union[
73+
None, HTTPValidationError,
74+
]:
75+
76+
""" Try sending a JSON body """
77+
url = "{}/tests/json_body".format(client.base_url)
78+
79+
json_json_body = json_body.to_dict()
80+
81+
response = httpx.post(url=url, headers=client.get_headers(), json=json_json_body,)
82+
83+
if response.status_code == 200:
84+
return None
85+
if response.status_code == 422:
86+
return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json()))
87+
else:
88+
raise ApiResponseError(response=response)

end_to_end_tests/golden-master/my_test_api_client/async_api/users.py renamed to end_to_end_tests/golden-master/my_test_api_client/async_api/tests.py

+22
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,25 @@ async def upload_file_tests_upload_post(
6767
return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json()))
6868
else:
6969
raise ApiResponseError(response=response)
70+
71+
72+
async def json_body_tests_json_body_post(
73+
*, client: Client, json_body: AModel,
74+
) -> Union[
75+
None, HTTPValidationError,
76+
]:
77+
78+
""" Try sending a JSON body """
79+
url = "{}/tests/json_body".format(client.base_url,)
80+
81+
json_json_body = json_body.to_dict()
82+
83+
async with httpx.AsyncClient() as _client:
84+
response = await _client.post(url=url, headers=client.get_headers(), json=json_json_body,)
85+
86+
if response.status_code == 200:
87+
return None
88+
if response.status_code == 422:
89+
return HTTPValidationError.from_dict(cast(Dict[str, Any], response.json()))
90+
else:
91+
raise ApiResponseError(response=response)

openapi_python_client/templates/async_endpoint_module.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async def {{ endpoint.name | snakecase }}(
6464
files=multipart_data.to_dict(),
6565
{% endif %}
6666
{% if endpoint.json_body %}
67-
json={{ endpoint.json_body.python_name }},
67+
json={{ "json_" + endpoint.json_body.python_name }},
6868
{% endif %}
6969
{% if endpoint.query_parameters %}
7070
params=params,

openapi_python_client/templates/endpoint_module.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def {{ endpoint.name | snakecase }}(
6565
files=multipart_data.to_dict(),
6666
{% endif %}
6767
{% if endpoint.json_body %}
68-
json={{ endpoint.json_body.python_name }},
68+
json={{ "json_" + endpoint.json_body.python_name }},
6969
{% endif %}
7070
{% if endpoint.query_parameters %}
7171
params=params,

tests/test_templates/async_endpoint_module.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async def camel_case(
6161
headers=client.get_headers(),
6262
data=asdict(form_data),
6363
files=multipart_data.to_dict(),
64-
json=json_body,
64+
json=json_json_body,
6565
)
6666

6767
if response.status_code == 200:

0 commit comments

Comments
 (0)