From 3503f5d9f5a683cb7be24e7544a2a70dfaf26981 Mon Sep 17 00:00:00 2001 From: Henry Chen Date: Sat, 28 Mar 2026 21:28:15 +0800 Subject: [PATCH] airflow-ctl: fix variable import to correctly handle falsy values --- .../ctl/commands/variable_command.py | 2 +- .../ctl/commands/test_variable_command.py | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/airflow-ctl/src/airflowctl/ctl/commands/variable_command.py b/airflow-ctl/src/airflowctl/ctl/commands/variable_command.py index 466be2ccac70d..88bf33a0f0197 100644 --- a/airflow-ctl/src/airflowctl/ctl/commands/variable_command.py +++ b/airflow-ctl/src/airflowctl/ctl/commands/variable_command.py @@ -51,7 +51,7 @@ def import_(args, api_client=NEW_API_CLIENT) -> list[str]: vars_to_update = [] for k, v in var_json.items(): value, description = v, None - if isinstance(v, dict) and v.get("value"): + if isinstance(v, dict) and "value" in v: value, description = v["value"], v.get("description") vars_to_update.append( diff --git a/airflow-ctl/tests/airflow_ctl/ctl/commands/test_variable_command.py b/airflow-ctl/tests/airflow_ctl/ctl/commands/test_variable_command.py index 9703c8f866b90..a0598d03459f8 100644 --- a/airflow-ctl/tests/airflow_ctl/ctl/commands/test_variable_command.py +++ b/airflow-ctl/tests/airflow_ctl/ctl/commands/test_variable_command.py @@ -83,6 +83,37 @@ def test_import_success(self, api_client_maker, tmp_path, monkeypatch): ) assert response == [self.key] + @pytest.mark.parametrize( + "falsy_value", + [ + "", + 0, + False, + ], + ids=["empty_string", "zero", "false"], + ) + def test_import_falsy_values(self, api_client_maker, tmp_path, monkeypatch, falsy_value): + """Test that falsy values (empty string, 0, False) are correctly imported.""" + api_client = api_client_maker( + path="/api/v2/variables", + response_json=self.bulk_response_success.model_dump(), + expected_http_status_code=200, + kind=ClientKind.CLI, + ) + + monkeypatch.chdir(tmp_path) + expected_json_path = tmp_path / self.export_file_name + variable_file = { + self.key: {"value": falsy_value, "description": "test falsy value"}, + } + + expected_json_path.write_text(json.dumps(variable_file)) + response = variable_command.import_( + self.parser.parse_args(["variables", "import", expected_json_path.as_posix()]), + api_client=api_client, + ) + assert response == [self.key] + def test_import_error(self, api_client_maker, tmp_path, monkeypatch): api_client = api_client_maker( path="/api/v2/variables",