diff --git a/docs/en/docs/release-notes.md b/docs/en/docs/release-notes.md index 4c308ab3..a47b7738 100644 --- a/docs/en/docs/release-notes.md +++ b/docs/en/docs/release-notes.md @@ -17,6 +17,11 @@ hide: - Expose `Controller` in `esmerald` as alternative to `APIView`. This was already available to use but not directly accessible via `from esmerald import Controller`. +## Fixed + +- Fix escaped " in TemplateResponse. +- Fix TemplateResponse's auto-detection of the media-type when used directly. + ## 3.6.2 ### Added diff --git a/esmerald/__init__.py b/esmerald/__init__.py index 9c15f319..52024376 100644 --- a/esmerald/__init__.py +++ b/esmerald/__init__.py @@ -1,4 +1,4 @@ -__version__ = "3.6.2" +__version__ = "3.6.3" from lilya import status diff --git a/esmerald/responses/template.py b/esmerald/responses/template.py index 931355ab..e54bbfa6 100644 --- a/esmerald/responses/template.py +++ b/esmerald/responses/template.py @@ -23,7 +23,7 @@ def __init__( background: Optional[Union["BackgroundTask", "BackgroundTasks"]] = None, headers: Optional[Dict[str, Any]] = None, cookies: Optional["ResponseCookies"] = None, - media_type: Union[MediaType, str] = MediaType.HTML, + media_type: Union[MediaType, str] = MediaType.JSON, ): if media_type == MediaType.JSON: # we assume this is the default suffixes = PurePath(template_name).suffixes @@ -38,6 +38,9 @@ def __init__( self.template = template_engine.get_template(template_name) self.context = context or {} content = self.template.render(**context) + # ensure template string is not mangled + if isinstance(content, str): + content = content.encode(self.charset) super().__init__( content=content, status_code=status_code, diff --git a/tests/test_templates.py b/tests/test_templates.py index 71679801..1f2f6268 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -43,10 +43,15 @@ def test_engine_jinja_and_mako(engine, template_dir: "pathlib.Path") -> None: assert isinstance(app.template_engine, engine) -def test_templates_starlette(template_dir, test_client_factory): +@pytest.mark.parametrize("apostrophe", ["'", '"']) +def test_templates_starlette(template_dir, test_client_factory, apostrophe): path = os.path.join(template_dir, "index.html") with open(path, "w") as file: - file.write("Hello, world") + file.write( + "Hello, world".replace( + "'", apostrophe + ) + ) @get() async def homepage(request: Request) -> Template: @@ -62,7 +67,9 @@ async def homepage(request: Request) -> Template: ) client = EsmeraldTestClient(app) response = client.get("/") - assert response.text == "Hello, world" + assert response.text == "Hello, world".replace( + "'", apostrophe + ) def test_alternative_template(template_dir, test_client_factory):