Auto-convert plain dicts to DADict for checkboxes fields via API - #984
Auto-convert plain dicts to DADict for checkboxes fields via API#984jacobyoby wants to merge 1 commit into
Conversation
|
This code is in the handler for the |
38146e6 to
705e088
Compare
The API stored a plain dict sent for a checkboxes variable as-is, and the next assembly failed on the missing DADict interface (issue jhpyle#981). A non-empty dict whose values are all booleans is now converted to a gathered DADict before assignment, using the same exec pattern the surrounding assignments use. Empty dicts are left alone: with no values there is no signal the variable is checkbox-shaped.
705e088 to
1f8a541
Compare
|
Never. 😅 request.form only ever gives strings, so that check was dead code where I had it. I've redone the PR: the conversion is now in set_session_variables, where variables comes from json in POST /api/session — the only place a dict would show up (which is how I hit #981 in the first place, driving interviews through the API). The /interview handler is untouched now and i tried to reduce to smallest possible change. |
|
With the existing code, you can set a variable to be a So I don't see why this is necessary. Also code change in this PR would make it impossible to use the API to define a variable like |
|
I appreciate the feedback! Seems to be working as designed. |
Summary
Fixes #981
When driving an interview via the API, submitting a plain dictionary like
{"a": True, "b": False}for acheckboxesfield creates a plain Python dict instead of aDADict. This breaks interviews that later call.true_values()or other DADict methods on the value.Root cause
In
docassemble_webapp/docassemble/webapp/interview/views.py, the code that processes submitted values forcheckboxes/multiselectfields only handles string values ("True","False"). Whenraw_datais a dict, none of the string comparisons match, so it falls through todata = "None"— silently discarding the dict value.Fix
Added an
isinstance(raw_data, dict)check before the string comparisons in both code paths that handle checkboxes values:my_var[a])my_var)When
raw_datais a dict, the fix generates aDADict(...)construction string (withelements=raw_data) that getsexec()'d like other variable assignments, producing a proper DADict that supports.true_values(),.false_values(), etc.The
not isinstance(raw_data, bool)guard prevents false matches sinceboolis a subclass ofintin Python.Verified behavior
{"a": True, "b": False}.true_values()returns["a"]"True"True(unchanged)"False"False(unchanged){}NoneNone(unchanged){"x": True}