Skip to content
Open
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
13 changes: 13 additions & 0 deletions .changeset/fix_optional_bodies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
default: patch
---

# Fix optional bodies

If a body is not required (the default), it will now:

1. Have `Unset` as part of its type annotation.
2. Default to a value of `UNSET`
3. Not be included in the request if it is `UNSET`

Thanks @orelmaliach for the report! Fixes #1354
24 changes: 24 additions & 0 deletions end_to_end_tests/baseline_openapi_3.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,30 @@
}
}
},
"/bodies/optional": {
"post": {
"tags": [
"bodies"
],
"description": "Test optional request body",
"operationId": "optional-body",
"requestBody": {
"required": false,
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
},
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/tests/": {
"get": {
"tags": [
Expand Down
24 changes: 24 additions & 0 deletions end_to_end_tests/baseline_openapi_3.1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,30 @@ info:
}
}
},
"/bodies/optional": {
"post": {
"tags": [
"bodies"
],
"description": "Test optional request body",
"operationId": "optional-body",
"requestBody": {
"required": false,
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
},
"responses": {
"200": {
"description": "OK"
}
}
}
},
"/tests/": {
"get": {
"tags": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import types

from . import json_like, post_bodies_multiple, refs
from . import json_like, optional_body, post_bodies_multiple, refs


class BodiesEndpoints:
Expand All @@ -26,3 +26,10 @@ def refs(cls) -> types.ModuleType:
Test request body defined via ref
"""
return refs

@classmethod
def optional_body(cls) -> types.ModuleType:
"""
Test optional request body
"""
return optional_body
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def test_response_union_type(self, post_simple_thing_sync):

def test_request_body(self, post_simple_thing_sync):
assert DocstringParser(post_simple_thing_sync).get_section("Args:") == [
"body (Thing): The thing."
"body (Thing | Unset): The thing."
]

def test_params(self, get_attribute_by_index_sync):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.json_like_body import JsonLikeBody
from ...types import Response
from ...types import UNSET, Response, Unset


def _get_kwargs(
*,
body: JsonLikeBody,
body: JsonLikeBody | Unset = UNSET,
) -> dict[str, Any]:
headers: dict[str, Any] = {}

Expand All @@ -20,7 +20,8 @@ def _get_kwargs(
"url": "/bodies/json-like",
}

_kwargs["json"] = body.to_dict()
if not isinstance(body, Unset):
_kwargs["json"] = body.to_dict()

headers["Content-Type"] = "application/vnd+json"

Expand Down Expand Up @@ -50,12 +51,12 @@ def _build_response(*, client: AuthenticatedClient | Client, response: httpx.Res
def sync_detailed(
*,
client: AuthenticatedClient | Client,
body: JsonLikeBody,
body: JsonLikeBody | Unset = UNSET,
) -> Response[Any]:
"""A content type that works like json but isn't application/json

Args:
body (JsonLikeBody):
body (JsonLikeBody | Unset):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
Expand All @@ -79,12 +80,12 @@ def sync_detailed(
async def asyncio_detailed(
*,
client: AuthenticatedClient | Client,
body: JsonLikeBody,
body: JsonLikeBody | Unset = UNSET,
) -> Response[Any]:
"""A content type that works like json but isn't application/json

Args:
body (JsonLikeBody):
body (JsonLikeBody | Unset):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
from http import HTTPStatus
from typing import Any

import httpx

from ... import errors
from ...client import AuthenticatedClient, Client
from ...models.optional_body_body import OptionalBodyBody
from ...types import UNSET, Response, Unset


def _get_kwargs(
*,
body: OptionalBodyBody | Unset = UNSET,
) -> dict[str, Any]:
headers: dict[str, Any] = {}

_kwargs: dict[str, Any] = {
"method": "post",
"url": "/bodies/optional",
}

if not isinstance(body, Unset):
_kwargs["json"] = body.to_dict()

headers["Content-Type"] = "application/json"

_kwargs["headers"] = headers
return _kwargs


def _parse_response(*, client: AuthenticatedClient | Client, response: httpx.Response) -> Any | None:
if response.status_code == 200:
return None

if client.raise_on_unexpected_status:
raise errors.UnexpectedStatus(response.status_code, response.content)
else:
return None


def _build_response(*, client: AuthenticatedClient | Client, response: httpx.Response) -> Response[Any]:
return Response(
status_code=HTTPStatus(response.status_code),
content=response.content,
headers=response.headers,
parsed=_parse_response(client=client, response=response),
)


def sync_detailed(
*,
client: AuthenticatedClient | Client,
body: OptionalBodyBody | Unset = UNSET,
) -> Response[Any]:
"""Test optional request body

Args:
body (OptionalBodyBody | Unset):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[Any]
"""

kwargs = _get_kwargs(
body=body,
)

response = client.get_httpx_client().request(
**kwargs,
)

return _build_response(client=client, response=response)


async def asyncio_detailed(
*,
client: AuthenticatedClient | Client,
body: OptionalBodyBody | Unset = UNSET,
) -> Response[Any]:
"""Test optional request body

Args:
body (OptionalBodyBody | Unset):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
httpx.TimeoutException: If the request takes longer than Client.timeout.

Returns:
Response[Any]
"""

kwargs = _get_kwargs(
body=body,
)

response = await client.get_async_httpx_client().request(**kwargs)

return _build_response(client=client, response=response)
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
from ...models.post_bodies_multiple_data_body import PostBodiesMultipleDataBody
from ...models.post_bodies_multiple_files_body import PostBodiesMultipleFilesBody
from ...models.post_bodies_multiple_json_body import PostBodiesMultipleJsonBody
from ...types import File, Response
from ...types import UNSET, File, Response, Unset


def _get_kwargs(
*,
body: PostBodiesMultipleJsonBody | File | PostBodiesMultipleDataBody | PostBodiesMultipleFilesBody,
body: PostBodiesMultipleJsonBody
| Unset
| File
| Unset
| PostBodiesMultipleDataBody
| Unset
| PostBodiesMultipleFilesBody
| Unset = UNSET,
Comment on lines +19 to +23
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unions still aren't quite right. This shouldn't list Unset twice and in a different, local union test it was trying to call .to_dict() on an Unset

) -> dict[str, Any]:
headers: dict[str, Any] = {}

Expand All @@ -23,19 +30,23 @@ def _get_kwargs(
}

if isinstance(body, PostBodiesMultipleJsonBody):
_kwargs["json"] = body.to_dict()
if not isinstance(body, Unset):
_kwargs["json"] = body.to_dict()

headers["Content-Type"] = "application/json"
if isinstance(body, File):
_kwargs["content"] = body.payload
if not isinstance(body, Unset):
_kwargs["content"] = body.payload

headers["Content-Type"] = "application/octet-stream"
if isinstance(body, PostBodiesMultipleDataBody):
_kwargs["data"] = body.to_dict()
if not isinstance(body, Unset):
_kwargs["data"] = body.to_dict()

headers["Content-Type"] = "application/x-www-form-urlencoded"
if isinstance(body, PostBodiesMultipleFilesBody):
_kwargs["files"] = body.to_multipart()
if not isinstance(body, Unset):
_kwargs["files"] = body.to_multipart()

headers["Content-Type"] = "multipart/form-data"

Expand Down Expand Up @@ -65,15 +76,22 @@ def _build_response(*, client: AuthenticatedClient | Client, response: httpx.Res
def sync_detailed(
*,
client: AuthenticatedClient | Client,
body: PostBodiesMultipleJsonBody | File | PostBodiesMultipleDataBody | PostBodiesMultipleFilesBody,
body: PostBodiesMultipleJsonBody
| Unset
| File
| Unset
| PostBodiesMultipleDataBody
| Unset
| PostBodiesMultipleFilesBody
| Unset = UNSET,
) -> Response[Any]:
"""Test multiple bodies

Args:
body (PostBodiesMultipleJsonBody):
body (File):
body (PostBodiesMultipleDataBody):
body (PostBodiesMultipleFilesBody):
body (PostBodiesMultipleJsonBody | Unset):
body (File | Unset):
body (PostBodiesMultipleDataBody | Unset):
body (PostBodiesMultipleFilesBody | Unset):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
Expand All @@ -97,15 +115,22 @@ def sync_detailed(
async def asyncio_detailed(
*,
client: AuthenticatedClient | Client,
body: PostBodiesMultipleJsonBody | File | PostBodiesMultipleDataBody | PostBodiesMultipleFilesBody,
body: PostBodiesMultipleJsonBody
| Unset
| File
| Unset
| PostBodiesMultipleDataBody
| Unset
| PostBodiesMultipleFilesBody
| Unset = UNSET,
) -> Response[Any]:
"""Test multiple bodies

Args:
body (PostBodiesMultipleJsonBody):
body (File):
body (PostBodiesMultipleDataBody):
body (PostBodiesMultipleFilesBody):
body (PostBodiesMultipleJsonBody | Unset):
body (File | Unset):
body (PostBodiesMultipleDataBody | Unset):
body (PostBodiesMultipleFilesBody | Unset):

Raises:
errors.UnexpectedStatus: If the server returns an undocumented status code and Client.raise_on_unexpected_status is True.
Expand Down
Loading