dify icon indicating copy to clipboard operation
dify copied to clipboard

fix: normalize json_schema from string to dict in VariableEntity

Open EvanYao826 opened this issue 6 months ago • 2 comments

Summary

Fixes #36766

When workflow graph data is loaded from the database, the json_schema field of JSON object variables may be stored as a JSON string rather than a native dict. VariableEntity.json_schema expects dict | None, causing a Pydantic ValidationError:

1 validation error for VariableEntity
json_schema
  Input should be a valid dictionary [type=dict_type, input_value='{...}', input_type=str]

Changes

  • api/core/app/app_config/workflow_ui_based_app/variables/manager.py: Added _normalize_json_schema() helper that deserializes a JSON string to a dict before passing to model_validate.
  • api/tests/.../test_workflow_ui_based_app_manager.py: Added regression test test_convert_normalizes_json_schema_string_to_dict.

Root Cause

Database JSON columns may serialize nested dicts as strings depending on the ORM/driver path. When loading an existing workflow with a json_object variable whose json_schema was stored as a string, the VariableEntity model rejects it.

Test Plan

  • [x] Unit test passes: pytest api/tests/unit_tests/core/app/app_config/workflow_ui_based_app/test_workflow_ui_based_app_manager.py -v
  • [x] No regressions in existing variable conversion tests

From Hermes Agent

EvanYao826 avatar May 28 '26 08:05 EvanYao826

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-05-28 08:33:03.678747182 +0000
+++ /tmp/pyrefly_pr.txt	2026-05-28 08:32:50.306689067 +0000
@@ -2738,10 +2738,18 @@
    --> tests/unit_tests/core/app/app_config/test_base_app_config_manager.py:156:69
 ERROR Argument `Literal['CHAT']` is not assignable to parameter `app_mode` with type `AppMode` in function `core.app.app_config.base_app_config_manager.BaseAppConfigManager.convert_features` [bad-argument-type]
    --> tests/unit_tests/core/app/app_config/test_base_app_config_manager.py:178:71
+ERROR Cannot index into `VariableEntity` [bad-index]
+  --> tests/unit_tests/core/app/app_config/workflow_ui_based_app/test_workflow_ui_based_app_manager.py:97:16
+ERROR Cannot index into `VariableEntity` [bad-index]
+   --> tests/unit_tests/core/app/app_config/workflow_ui_based_app/test_workflow_ui_based_app_manager.py:118:16
+ERROR Cannot index into `VariableEntity` [bad-index]
+   --> tests/unit_tests/core/app/app_config/workflow_ui_based_app/test_workflow_ui_based_app_manager.py:137:16
+ERROR Cannot index into `VariableEntity` [bad-index]
+   --> tests/unit_tests/core/app/app_config/workflow_ui_based_app/test_workflow_ui_based_app_manager.py:156:16
 ERROR Cannot index into `RagPipelineVariableEntity` [bad-index]
-   --> tests/unit_tests/core/app/app_config/workflow_ui_based_app/test_workflow_ui_based_app_manager.py:141:16
+   --> tests/unit_tests/core/app/app_config/workflow_ui_based_app/test_workflow_ui_based_app_manager.py:222:16
 ERROR Cannot index into `RagPipelineVariableEntity` [bad-index]
-   --> tests/unit_tests/core/app/app_config/workflow_ui_based_app/test_workflow_ui_based_app_manager.py:166:16
+   --> tests/unit_tests/core/app/app_config/workflow_ui_based_app/test_workflow_ui_based_app_manager.py:247:16
 ERROR Argument `SimpleNamespace` is not assignable to parameter `app_model` with type `App` in function `core.app.apps.advanced_chat.app_config_manager.AdvancedChatAppConfigManager.get_app_config` [bad-argument-type]
   --> tests/unit_tests/core/app/apps/advanced_chat/test_app_config_manager.py:23:70
 ERROR Argument `SimpleNamespace` is not assignable to parameter `workflow` with type `Workflow` in function `core.app.apps.advanced_chat.app_config_manager.AdvancedChatAppConfigManager.get_app_config` [bad-argument-type]

github-actions[bot] avatar May 28 '26 08:05 github-actions[bot]

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 45.74% 45.73% -0.01%
Strict coverage 45.27% 45.26% -0.01%
Typed symbols 24,619 24,619 0
Untyped symbols 29,508 29,520 +12
Modules 2758 2758 0

github-actions[bot] avatar May 28 '26 08:05 github-actions[bot]