Skip to content

Commit

Permalink
Fixed json cls encoder in trigger_client_event
Browse files Browse the repository at this point in the history
  • Loading branch information
CleitonDeLima committed Jan 10, 2022
1 parent e4f0938 commit 849bf02
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/django_htmx/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
from typing import Any, Dict

from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpResponse
from django.http.response import HttpResponseRedirectBase

Expand Down Expand Up @@ -60,4 +61,4 @@ def trigger_client_event(
else:
data = {name: params}

response[header] = json.dumps(data)
response[header] = json.dumps(data, cls=DjangoJSONEncoder)
23 changes: 23 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import uuid
from datetime import datetime
from decimal import Decimal

import pytest
from django.http import HttpResponse
from django.test import SimpleTestCase
Expand Down Expand Up @@ -99,3 +103,22 @@ def test_success_after_swap(self):
assert (
response["HX-Trigger-After-Swap"] == '{"showMessage": {"value": "Great!"}}'
)

def test_django_json_encoder(self):
response = HttpResponse()
uuid_value = uuid.uuid4()
dt = datetime(2022, 1, 1, 10, 0, 0)

params = {
"decimal": Decimal("9.99"),
"uuid": uuid_value,
"datetime": dt,
"date": dt.date(),
}
trigger_client_event(response, "showMessage", params)

expected = (
'{"decimal": "%s", "uuid": "%s", "datetime": "%s", "date": "%s"}'
) % ("9.99", uuid_value, "2022-01-01T10:00:00", "2022-01-01")

assert expected in response["HX-Trigger"]

0 comments on commit 849bf02

Please sign in to comment.