Skip to content

Commit 7aa4a75

Browse files
committed
Remove calling orjson.dumps before every data checksum is created. 1) Fixes a bug where dictionary keys were sorted differently which would change the generated checksum. 2) Should remove some processing time on every render that was happening unnecessarily.
1 parent c218c27 commit 7aa4a75

File tree

10 files changed

+79
-45
lines changed

10 files changed

+79
-45
lines changed

django_unicorn/components/unicorn_template_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def render(self):
116116

117117
frontend_context_variables = self.component.get_frontend_context_variables()
118118
frontend_context_variables_dict = orjson.loads(frontend_context_variables)
119-
checksum = generate_checksum(orjson.dumps(frontend_context_variables_dict))
119+
checksum = generate_checksum(str(frontend_context_variables_dict))
120120

121121
soup = BeautifulSoup(content, features="html.parser")
122122
root_element = get_root_element(soup)

django_unicorn/views/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ def _process_component_request(
216216
"data": updated_data,
217217
"errors": component.errors,
218218
"calls": component.calls,
219-
"checksum": generate_checksum(orjson.dumps(component_request.data)),
219+
"checksum": generate_checksum(str(component_request.data)),
220220
}
221221

222222
if partial_doms:
@@ -270,7 +270,7 @@ def _process_component_request(
270270
parent_frontend_context_variables = loads(
271271
parent_component.get_frontend_context_variables()
272272
)
273-
parent_checksum = generate_checksum(dumps(parent_frontend_context_variables))
273+
parent_checksum = generate_checksum(str(parent_frontend_context_variables))
274274

275275
parent = {
276276
"id": parent_component.component_id,

django_unicorn/views/objects.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def validate_checksum(self):
7676
checksum = self.body.get("checksum")
7777
assert checksum, "Missing checksum"
7878

79-
generated_checksum = generate_checksum(dumps(self.data, fix_floats=False))
79+
generated_checksum = generate_checksum(str(self.data))
8080
assert checksum == generated_checksum, "Checksum does not match"
8181

8282

tests/views/fake_components.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
from typing import Dict
23

34
from django import forms
45
from django.shortcuts import redirect
@@ -82,3 +83,11 @@ def set_text_with_validation(self):
8283

8384
def set_number(self, number):
8485
self.number = number
86+
87+
88+
class FakeComponentWithDictionary(UnicornView):
89+
template_name = "templates/test_component.html"
90+
dictionary: Dict = None
91+
92+
def test_method(self):
93+
pass

tests/views/message/test_call_method.py

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,32 @@ def test_message_call_method(client):
2525
assert response["data"].get("method_count") == 1
2626

2727

28+
def test_message_call_method_with_dictionary_checksum(client):
29+
data = {"dictionary": {"1": "test", "2": "anothertest", "3": "", "4": "moretest"}}
30+
message = {
31+
"actionQueue": [
32+
{
33+
"payload": {"name": "test_method"},
34+
"type": "callMethod",
35+
}
36+
],
37+
"data": data,
38+
"checksum": generate_checksum(str(data)),
39+
"id": shortuuid.uuid()[:8],
40+
"epoch": time.time(),
41+
}
42+
43+
response = client.post(
44+
"/message/tests.views.fake_components.FakeComponentWithDictionary",
45+
message,
46+
content_type="application/json",
47+
)
48+
49+
body = orjson.loads(response.content)
50+
51+
assert not body["errors"]
52+
53+
2854
def test_message_call_method_redirect(client):
2955
data = {}
3056
message = {
@@ -35,7 +61,7 @@ def test_message_call_method_redirect(client):
3561
}
3662
],
3763
"data": data,
38-
"checksum": generate_checksum(orjson.dumps(data)),
64+
"checksum": generate_checksum(str(data)),
3965
"id": shortuuid.uuid()[:8],
4066
"epoch": time.time(),
4167
}
@@ -64,7 +90,7 @@ def test_message_call_method_refresh_redirect(client):
6490
}
6591
],
6692
"data": data,
67-
"checksum": generate_checksum(orjson.dumps(data)),
93+
"checksum": generate_checksum(str(data)),
6894
"id": shortuuid.uuid()[:8],
6995
"epoch": time.time(),
7096
}
@@ -94,7 +120,7 @@ def test_message_call_method_hash_update(client):
94120
}
95121
],
96122
"data": data,
97-
"checksum": generate_checksum(orjson.dumps(data)),
123+
"checksum": generate_checksum(str(data)),
98124
"id": shortuuid.uuid()[:8],
99125
"epoch": time.time(),
100126
}
@@ -121,7 +147,7 @@ def test_message_call_method_return_value(client):
121147
}
122148
],
123149
"data": data,
124-
"checksum": generate_checksum(orjson.dumps(data)),
150+
"checksum": generate_checksum(str(data)),
125151
"id": shortuuid.uuid()[:8],
126152
"epoch": time.time(),
127153
}
@@ -152,7 +178,7 @@ def test_message_call_method_poll_update(client):
152178
}
153179
],
154180
"data": data,
155-
"checksum": generate_checksum(orjson.dumps(data)),
181+
"checksum": generate_checksum(str(data)),
156182
"id": shortuuid.uuid()[:8],
157183
"epoch": time.time(),
158184
}
@@ -182,7 +208,7 @@ def test_message_call_method_setter(client):
182208
}
183209
],
184210
"data": data,
185-
"checksum": generate_checksum(orjson.dumps(data)),
211+
"checksum": generate_checksum(str(data)),
186212
"id": shortuuid.uuid()[:8],
187213
"epoch": time.time(),
188214
}
@@ -208,7 +234,7 @@ def test_message_call_method_nested_setter(client):
208234
}
209235
],
210236
"data": data,
211-
"checksum": generate_checksum(orjson.dumps(data)),
237+
"checksum": generate_checksum(str(data)),
212238
"id": shortuuid.uuid()[:8],
213239
"epoch": time.time(),
214240
}
@@ -234,7 +260,7 @@ def test_message_call_method_multiple_nested_setter(client):
234260
}
235261
],
236262
"data": data,
237-
"checksum": generate_checksum(orjson.dumps(data)),
263+
"checksum": generate_checksum(str(data)),
238264
"id": shortuuid.uuid()[:8],
239265
"epoch": time.time(),
240266
}
@@ -260,7 +286,7 @@ def test_message_call_method_toggle(client):
260286
}
261287
],
262288
"data": data,
263-
"checksum": generate_checksum(orjson.dumps(data)),
289+
"checksum": generate_checksum(str(data)),
264290
"id": shortuuid.uuid()[:8],
265291
"epoch": time.time(),
266292
}
@@ -286,7 +312,7 @@ def test_message_call_method_nested_toggle(client):
286312
}
287313
],
288314
"data": data,
289-
"checksum": generate_checksum(orjson.dumps(data)),
315+
"checksum": generate_checksum(str(data)),
290316
"id": shortuuid.uuid()[:8],
291317
"epoch": time.time(),
292318
}
@@ -312,7 +338,7 @@ def test_message_call_method_args(client):
312338
}
313339
],
314340
"data": data,
315-
"checksum": generate_checksum(orjson.dumps(data)),
341+
"checksum": generate_checksum(str(data)),
316342
"id": shortuuid.uuid()[:8],
317343
"epoch": time.time(),
318344
}
@@ -338,7 +364,7 @@ def test_message_call_method_kwargs(client):
338364
}
339365
],
340366
"data": data,
341-
"checksum": generate_checksum(orjson.dumps(data)),
367+
"checksum": generate_checksum(str(data)),
342368
"id": shortuuid.uuid()[:8],
343369
"epoch": time.time(),
344370
}
@@ -364,7 +390,7 @@ def test_message_call_method_no_validation(client):
364390
}
365391
],
366392
"data": data,
367-
"checksum": generate_checksum(orjson.dumps(data)),
393+
"checksum": generate_checksum(str(data)),
368394
"id": shortuuid.uuid()[:8],
369395
"epoch": time.time(),
370396
}
@@ -390,7 +416,7 @@ def test_message_call_method_validation(client):
390416
}
391417
],
392418
"data": data,
393-
"checksum": generate_checksum(orjson.dumps(data)),
419+
"checksum": generate_checksum(str(data)),
394420
"id": shortuuid.uuid()[:8],
395421
"epoch": time.time(),
396422
}
@@ -420,7 +446,7 @@ def test_message_call_method_reset(client):
420446
},
421447
],
422448
"data": data,
423-
"checksum": generate_checksum(orjson.dumps(data)),
449+
"checksum": generate_checksum(str(data)),
424450
"id": shortuuid.uuid()[:8],
425451
"epoch": time.time(),
426452
}
@@ -449,7 +475,7 @@ def test_message_call_method_refresh(client):
449475
},
450476
],
451477
"data": data,
452-
"checksum": generate_checksum(orjson.dumps(data)),
478+
"checksum": generate_checksum(str(data)),
453479
"id": shortuuid.uuid()[:8],
454480
"epoch": time.time(),
455481
}

tests/views/message/test_call_method_multiple.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_message_single(client, settings):
6464
}
6565
],
6666
"data": data,
67-
"checksum": generate_checksum(orjson.dumps(data)),
67+
"checksum": generate_checksum(str(data)),
6868
"id": component_id,
6969
"epoch": time.time(),
7070
}
@@ -93,7 +93,7 @@ def test_message_two(client, settings):
9393
}
9494
],
9595
"data": data,
96-
"checksum": generate_checksum(orjson.dumps(data)),
96+
"checksum": generate_checksum(str(data)),
9797
"id": component_id,
9898
}
9999
messages = [(client, 0, message), (client, 0.1, message)]
@@ -125,7 +125,7 @@ def test_message_multiple(client, settings):
125125
}
126126
],
127127
"data": data,
128-
"checksum": generate_checksum(orjson.dumps(data)),
128+
"checksum": generate_checksum(str(data)),
129129
"id": component_id,
130130
}
131131
messages = [(client, 0, message), (client, 0.1, message), (client, 0.2, message)]
@@ -158,7 +158,7 @@ def test_message_multiple_return_is_correct(client, settings):
158158
}
159159
],
160160
"data": data,
161-
"checksum": generate_checksum(orjson.dumps(data)),
161+
"checksum": generate_checksum(str(data)),
162162
"id": component_id,
163163
}
164164
messages = [(client, 0, message), (client, 0.1, message), (client, 0.2, message)]
@@ -197,7 +197,7 @@ def test_message_multiple_with_updated_data(client, settings):
197197
}
198198
],
199199
"data": data,
200-
"checksum": generate_checksum(orjson.dumps(data)),
200+
"checksum": generate_checksum(str(data)),
201201
"id": component_id,
202202
}
203203
messages = [(client, 0, message), (client, 0.1, message), (client, 0.2, message)]
@@ -207,7 +207,7 @@ def test_message_multiple_with_updated_data(client, settings):
207207
message_with_new_data = deepcopy(message)
208208
message_with_new_data["data"] = {"counter": 7}
209209
message_with_new_data["checksum"] = generate_checksum(
210-
orjson.dumps(message_with_new_data["data"])
210+
str(message_with_new_data["data"])
211211
)
212212
messages.append((client, 0.4, message_with_new_data))
213213

@@ -239,7 +239,7 @@ def test_message_second_request_not_queued_because_after_first(client, settings)
239239
}
240240
],
241241
"data": data,
242-
"checksum": generate_checksum(orjson.dumps(data)),
242+
"checksum": generate_checksum(str(data)),
243243
"id": component_id,
244244
}
245245
messages = [(client, 0, message), (client, 0.4, message)]
@@ -271,7 +271,7 @@ def test_message_second_request_not_queued_because_serial_timeout(client, settin
271271
}
272272
],
273273
"data": data,
274-
"checksum": generate_checksum(orjson.dumps(data)),
274+
"checksum": generate_checksum(str(data)),
275275
"id": component_id,
276276
}
277277
messages = [(client, 0, message), (client, 0.2, message)]
@@ -303,7 +303,7 @@ def test_message_second_request_not_queued_because_serial_disabled(client, setti
303303
}
304304
],
305305
"data": data,
306-
"checksum": generate_checksum(orjson.dumps(data)),
306+
"checksum": generate_checksum(str(data)),
307307
"id": component_id,
308308
}
309309
messages = [(client, 0, message), (client, 0.2, message)]
@@ -338,7 +338,7 @@ def test_message_second_request_not_queued_because_dummy_cache(client, settings)
338338
}
339339
],
340340
"data": data,
341-
"checksum": generate_checksum(orjson.dumps(data)),
341+
"checksum": generate_checksum(str(data)),
342342
"id": component_id,
343343
}
344344
messages = [(client, 0, message), (client, 0.2, message)]

tests/views/message/test_set_property.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ def test_setter(client):
2424
{"type": "callMethod", "payload": {"name": "check=True"}},
2525
],
2626
"data": data,
27-
"checksum": generate_checksum(orjson.dumps(data)),
27+
"checksum": generate_checksum(str(data)),
2828
"id": shortuuid.uuid()[:8],
2929
"epoch": time.time(),
3030
}
3131

3232
body = _post_message_and_get_body(client, message)
3333

3434
assert not body["errors"]
35-
assert body["data"]["check"] == True
35+
assert body["data"]["check"] is True
3636

3737

3838
def test_nested_setter(client):
@@ -42,15 +42,15 @@ def test_nested_setter(client):
4242
{"type": "callMethod", "payload": {"name": "nested.check=True"}},
4343
],
4444
"data": data,
45-
"checksum": generate_checksum(orjson.dumps(data)),
45+
"checksum": generate_checksum(str(data)),
4646
"id": shortuuid.uuid()[:8],
4747
"epoch": time.time(),
4848
}
4949

5050
body = _post_message_and_get_body(client, message)
5151

5252
assert not body["errors"]
53-
assert body["data"]["nested"]["check"] == True
53+
assert body["data"]["nested"]["check"] is True
5454

5555

5656
def test_equal_sign(client):
@@ -63,7 +63,7 @@ def test_equal_sign(client):
6363
},
6464
],
6565
"data": data,
66-
"checksum": generate_checksum(orjson.dumps(data)),
66+
"checksum": generate_checksum(str(data)),
6767
"id": shortuuid.uuid()[:8],
6868
"epoch": time.time(),
6969
}

0 commit comments

Comments
 (0)