Skip to content
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

strings should be only mangled when the response type is JSON #473

Merged
merged 2 commits into from
Jan 17, 2025
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
1 change: 1 addition & 0 deletions docs/en/docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ accessible via `from esmerald import Controller`.

- Fix escaped " in TemplateResponse.
- Fix TemplateResponse's auto-detection of the media-type when used directly.
- Don't mangle strings by default for other media-types than json.

## 3.6.2

Expand Down
21 changes: 13 additions & 8 deletions esmerald/responses/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,25 @@ def make_response(self, content: Any) -> bytes | memoryview | str:
transform_kwargs = RESPONSE_TRANSFORM_KWARGS.get()
if transform_kwargs:
transform_kwargs = transform_kwargs.copy()
elif isinstance(content, str) and self.media_type != MediaType.JSON:
# treat strings special when not using json and disable mangling when no context is active.
transform_kwargs = None
else:
transform_kwargs = {}
transform_kwargs.setdefault(
"json_encode_fn",
partial(
orjson.dumps,
option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS,
),
)
if transform_kwargs is not None:
transform_kwargs.setdefault(
"json_encode_fn",
partial(
orjson.dumps,
option=orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_OMIT_MICROSECONDS,
),
)
try:
# switch to a special mode for MediaType.JSON (default handlers)
if self.media_type == MediaType.JSON:
# keep it a serialized json object
transform_kwargs.setdefault("post_transform_fn", None)
if transform_kwargs is not None:
transform_kwargs.setdefault("post_transform_fn", None)
# otherwise use default logic of lilya striping '"'
with self.with_transform_kwargs(transform_kwargs):
# if content is bytes it won't be transformed and
Expand Down
13 changes: 13 additions & 0 deletions tests/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from lilya import status

from esmerald import Response
from esmerald.enums import MediaType
from esmerald.responses.encoders import ORJSONResponse, UJSONResponse
from esmerald.routing.gateways import Gateway
from esmerald.routing.handlers import get
Expand Down Expand Up @@ -54,6 +55,11 @@ def route_nine() -> None:
pass


@get("/ten", media_type=MediaType.TEXT)
def route_ten() -> str:
return "hel\"'lo"


def test_ujson_response(test_client_factory):
with create_client(routes=[Gateway(handler=route_one)]) as client:
response = client.get("/one")
Expand Down Expand Up @@ -113,6 +119,13 @@ def test_str_returnal(test_client_factory):
assert response.text == '"hello"'


def test_str_returnal_non_json(test_client_factory):
with create_client(routes=[Gateway(handler=route_ten)]) as client:
response = client.get("/ten")

assert response.text == "hel\"'lo"


def test_implicit_none_returnal(test_client_factory):
with create_client(routes=[route_nine]) as client:
response = client.get("/nine")
Expand Down
Loading