Skip to content

Fix JSON bodies in generated endpoints. #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion end_to_end_tests/fastapi_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
44 changes: 42 additions & 2 deletions end_to_end_tests/fastapi_app/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"/tests/": {
"get": {
"tags": [
"users"
"tests"
],
"summary": "Get List",
"description": "Get a list of things ",
Expand Down Expand Up @@ -97,7 +97,7 @@
"/tests/upload": {
"post": {
"tags": [
"users"
"tests"
],
"summary": "Upload File",
"description": "Upload a file ",
Expand Down Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion openapi_python_client/templates/async_endpoint_module.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion openapi_python_client/templates/endpoint_module.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_templates/async_endpoint_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down