diff --git a/superset/datasets/schemas.py b/superset/datasets/schemas.py index 2390040cc4fa1..06b13a0e121ef 100644 --- a/superset/datasets/schemas.py +++ b/superset/datasets/schemas.py @@ -157,6 +157,17 @@ def fix_extra(self, data: Dict[str, Any], **kwargs: Any) -> Dict[str, Any]: class ImportV1MetricSchema(Schema): + # pylint: disable=no-self-use, unused-argument + @pre_load + def fix_extra(self, data: Dict[str, Any], **kwargs: Any) -> Dict[str, Any]: + """ + Fix for extra initially beeing exported as a string. + """ + if isinstance(data.get("extra"), str): + data["extra"] = json.loads(data["extra"]) + + return data + metric_name = fields.String(required=True) verbose_name = fields.String(allow_none=True) metric_type = fields.String(allow_none=True) diff --git a/tests/unit_tests/datasets/commands/export_test.py b/tests/unit_tests/datasets/commands/export_test.py index a5967350ec537..cb5512448e299 100644 --- a/tests/unit_tests/datasets/commands/export_test.py +++ b/tests/unit_tests/datasets/commands/export_test.py @@ -49,7 +49,11 @@ def test_export(app_context: None, session: Session) -> None: ), ] metrics = [ - SqlMetric(metric_name="cnt", expression="COUNT(*)"), + SqlMetric( + metric_name="cnt", + expression="COUNT(*)", + extra=json.dumps({"warning_markdown": None}), + ), ] sqla_table = SqlaTable( @@ -108,7 +112,8 @@ def test_export(app_context: None, session: Session) -> None: expression: COUNT(*) description: null d3format: null - extra: null + extra: + warning_markdown: null warning_text: null columns: - column_name: profit diff --git a/tests/unit_tests/datasets/commands/importers/v1/import_test.py b/tests/unit_tests/datasets/commands/importers/v1/import_test.py index 922cf9a1f2876..e622c55c3bc27 100644 --- a/tests/unit_tests/datasets/commands/importers/v1/import_test.py +++ b/tests/unit_tests/datasets/commands/importers/v1/import_test.py @@ -68,7 +68,7 @@ def test_import_(app_context: None, session: Session) -> None: "expression": "COUNT(*)", "description": None, "d3format": None, - "extra": None, + "extra": {"warning_markdown": None}, "warning_text": None, } ], @@ -115,7 +115,7 @@ def test_import_(app_context: None, session: Session) -> None: assert sqla_table.metrics[0].expression == "COUNT(*)" assert sqla_table.metrics[0].description is None assert sqla_table.metrics[0].d3format is None - assert sqla_table.metrics[0].extra is None + assert sqla_table.metrics[0].extra == '{"warning_markdown": null}' assert sqla_table.metrics[0].warning_text is None assert len(sqla_table.columns) == 1 assert sqla_table.columns[0].column_name == "profit" @@ -169,7 +169,18 @@ def test_import_column_extra_is_string(app_context: None, session: Session) -> N "fetch_values_predicate": "foo IN (1, 2)", "extra": '{"warning_markdown": "*WARNING*"}', "uuid": dataset_uuid, - "metrics": [], + "metrics": [ + { + "metric_name": "cnt", + "verbose_name": None, + "metric_type": None, + "expression": "COUNT(*)", + "description": None, + "d3format": None, + "extra": '{"warning_markdown": null}', + "warning_text": None, + } + ], "columns": [ { "column_name": "profit", @@ -193,5 +204,6 @@ def test_import_column_extra_is_string(app_context: None, session: Session) -> N dataset_config["database_id"] = database.id sqla_table = import_dataset(session, dataset_config) + assert sqla_table.metrics[0].extra == '{"warning_markdown": null}' assert sqla_table.columns[0].extra == '{"certified_by": "User"}' assert sqla_table.extra == '{"warning_markdown": "*WARNING*"}'