diff --git a/CHANGELOG.md b/CHANGELOG.md index b8df35d816..e39b8cac71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). - [#4218](https://github.com/plotly/dash/pull/4218) Fix `pages_folder` written with `/` on Windows, such as `"src/pages"`, giving wrong page module names and paths. Fixes [#4217](https://github.com/plotly/dash/issues/4217). - [#4220](https://github.com/plotly/dash/pull/4220) Fix the `DASH_VALIDATE_CALLBACKS` environment variable being ignored, although `Dash.run` and `Dash.enable_dev_tools` document it for `dev_tools_validate_callbacks`. Fixes [#4219](https://github.com/plotly/dash/issues/4219). - [#4235](https://github.com/plotly/dash/pull/4235) Fix FastAPI deep links for mounted Dash apps and disabled or repeated lifespans. Apps sharing a server respect their URL prefixes, and explicit routes take precedence even when added after startup. Fixes [#4234](https://github.com/plotly/dash/issues/4234). +- [#4241](https://github.com/plotly/dash/pull/4241) Fix `ctx.triggered` giving `None` as the value and `ctx.args_grouping` marking nothing as triggered when a pattern-matching id contains non-ASCII characters, such as `{"index": "café"}`. `stringify_id` now keeps those characters, like the renderer. ## [4.5.2] - 2026-11-27 diff --git a/dash/_utils.py b/dash/_utils.py index 8c85398b01..1c9bd62f04 100644 --- a/dash/_utils.py +++ b/dash/_utils.py @@ -210,9 +210,14 @@ def split_callback_id(callback_id): def stringify_id(id_) -> str: + # Must match stringifyId in the renderer, which uses JSON.stringify and so + # keeps non-ASCII characters as they are: the changedPropIds it sends are + # compared with these strings. def _json(k, v): - vstr = v.to_json() if hasattr(v, "to_json") else json.dumps(v) - return f"{json.dumps(k)}:{vstr}" + vstr = ( + v.to_json() if hasattr(v, "to_json") else json.dumps(v, ensure_ascii=False) + ) + return f"{json.dumps(k, ensure_ascii=False)}:{vstr}" if isinstance(id_, dict): return "{" + ",".join(_json(k, id_[k]) for k in sorted(id_)) + "}" diff --git a/tests/unit/library/test_utils.py b/tests/unit/library/test_utils.py index cb677e8355..53b770fa4e 100644 --- a/tests/unit/library/test_utils.py +++ b/tests/unit/library/test_utils.py @@ -74,3 +74,15 @@ def test_ddut001_attribute_dict(): ) def test_ddut002_pascal_case(value, expected): assert utils.pascal_case(value) == expected + + +def test_ddut003_stringify_id_keeps_non_ascii(): + # Same output as the renderer's stringifyId (JSON.stringify), which does + # not escape non-ASCII characters. + assert ( + utils.stringify_id({"type": "b", "index": "caf\u00e9", "n\u00e4me": 1}) + == '{"index":"caf\u00e9","n\u00e4me":1,"type":"b"}' + ) + assert utils.stringify_id({"type": "b", "index": 'a"\n'}) == ( + '{"index":"a\\"\\n","type":"b"}' + ) diff --git a/tests/unit/test_callback_unit.py b/tests/unit/test_callback_unit.py index 4f22657fae..b40701bd11 100644 --- a/tests/unit/test_callback_unit.py +++ b/tests/unit/test_callback_unit.py @@ -163,3 +163,47 @@ def test_create_callback_id_dict_id_without_dots_unaffected(): id_part, prop_part = callback_id.rsplit(".", 1) assert prop_part == "value" assert json.loads(id_part) == {"type": "widget", "index": 2} + + +def test_callback_context_with_non_ascii_pattern_matching_id(): + """The renderer builds changedPropIds with JSON.stringify, which keeps + non-ASCII characters, so the triggered input must still be found when a + pattern-matching id value contains one.""" + app = dash.Dash(__name__) + app.layout = dash.html.Div() + seen = {} + + @app.callback( + Output({"type": "out", "index": "caf\u00e9"}, "children"), + Input({"type": "btn", "index": dash.ALL}, "n_clicks"), + ) + def update(_): + seen["triggered"] = dash.ctx.triggered + seen["args_grouping"] = [ + (g["str_id"], g["triggered"]) for g in dash.ctx.args_grouping + ] + return "clicked" + + clicked = '{"index":"caf\u00e9","type":"btn"}' + other = '{"index":"abc","type":"btn"}' + out_id = {"type": "out", "index": "caf\u00e9"} + body = { + "output": create_callback_id(Output(out_id, "children"), []), + "outputs": {"id": out_id, "property": "children"}, + "inputs": [ + [ + {"id": json.loads(clicked), "property": "n_clicks", "value": 2}, + {"id": json.loads(other), "property": "n_clicks", "value": 1}, + ] + ], + "changedPropIds": [clicked + ".n_clicks"], + "state": [], + } + response = app.server.test_client().post("/_dash-update-component", json=body) + + assert response.status_code == 200 + assert seen["triggered"] == [{"prop_id": clicked + ".n_clicks", "value": 2}] + assert seen["args_grouping"] == [(clicked, True), (other, False)] + assert response.get_json()["response"] == { + '{"index":"caf\u00e9","type":"out"}': {"children": "clicked"} + }