Skip to content

Commit

Permalink
Fix response issues (#117)
Browse files Browse the repository at this point in the history
* Fix too strict json_encoder_fn enforcment

- fix too strict json_encoder_fn enforcment in JSONResponse
- bump version:

-fix handling empty [] and {} when provided out of context
  • Loading branch information
devkral authored Dec 19, 2024
1 parent 843edb9 commit 4e46ece
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
7 changes: 7 additions & 0 deletions docs/en/docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ hide:
# Release Notes


## 0.11.8

### Fixed

- Fix too strict json_encoder_fn enforcment.
- Fix empty [] and {} becoming incorrectly an empty response in json context.

## 0.11.7

### Added
Expand Down
2 changes: 1 addition & 1 deletion lilya/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.11.7"
__version__ = "0.11.8"
22 changes: 15 additions & 7 deletions lilya/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def make_response(self, content: Any) -> bytes | str:
"""
Makes the Response object type.
"""
if content is None or content is NoReturn or not content:
# only handle empty string not empty bytes. Bytes are handled later
if content is None or content is NoReturn or content == "":
return b""
if isinstance(content, (bytes, memoryview)):
return content
Expand All @@ -116,6 +117,10 @@ def make_response(self, content: Any) -> bytes | str:

if isinstance(content, (bytes, memoryview)):
return content
# handle empty {} or [] gracefully instead of failing
# must be transformed before
if not content:
return b""
return content.encode(self.charset) # type: ignore

def make_headers(
Expand Down Expand Up @@ -311,12 +316,15 @@ def make_response(self, content: Any) -> bytes:
new_params = new_params.copy()
else:
new_params = {}
new_params["json_encode_fn"] = functools.partial(
json.dumps,
ensure_ascii=False,
allow_nan=False,
indent=None,
separators=(",", ":"),
new_params.setdefault(
"json_encode_fn",
functools.partial(
json.dumps,
ensure_ascii=False,
allow_nan=False,
indent=None,
separators=(",", ":"),
),
)
new_params["post_transform_fn"] = None
if content is NoReturn:
Expand Down

0 comments on commit 4e46ece

Please sign in to comment.