|
17 | 17 | from __future__ import annotations |
18 | 18 |
|
19 | 19 | import json |
| 20 | +from unittest.mock import patch |
20 | 21 |
|
21 | 22 | import pytest |
22 | 23 |
|
23 | 24 | from airflowctl.api.client import ClientKind |
24 | 25 | from airflowctl.api.datamodels.generated import ( |
25 | 26 | BulkActionResponse, |
26 | 27 | BulkResponse, |
| 28 | + ConnectionBody, |
27 | 29 | ConnectionCollectionResponse, |
28 | 30 | ConnectionResponse, |
29 | 31 | ) |
@@ -125,3 +127,52 @@ def test_import_error(self, api_client_maker, tmp_path, monkeypatch): |
125 | 127 | self.parser.parse_args(["connections", "import", expected_json_path.as_posix()]), |
126 | 128 | api_client=api_client, |
127 | 129 | ) |
| 130 | + |
| 131 | + def test_import_without_extra_field(self, api_client_maker, tmp_path, monkeypatch): |
| 132 | + """Import succeeds when JSON omits the ``extra`` field (#62653). |
| 133 | +
|
| 134 | + Before the fix, ``v.get("extra", {})`` returned ``{}`` (a dict) when |
| 135 | + the key was absent, but ``ConnectionBody.extra`` expects ``str | None``, |
| 136 | + causing a Pydantic ``ValidationError``. |
| 137 | + """ |
| 138 | + api_client = api_client_maker( |
| 139 | + path="/api/v2/connections", |
| 140 | + response_json=self.bulk_response_success.model_dump(), |
| 141 | + expected_http_status_code=200, |
| 142 | + kind=ClientKind.CLI, |
| 143 | + ) |
| 144 | + |
| 145 | + monkeypatch.chdir(tmp_path) |
| 146 | + json_path = tmp_path / self.export_file_name |
| 147 | + # Intentionally omit "extra" (and several other optional keys) to |
| 148 | + # mirror a minimal real-world connection JSON export. |
| 149 | + connection_file = { |
| 150 | + self.connection_id: { |
| 151 | + "conn_type": "test_type", |
| 152 | + "host": "test_host", |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + json_path.write_text(json.dumps(connection_file)) |
| 157 | + |
| 158 | + with patch( |
| 159 | + "airflowctl.ctl.commands.connection_command.ConnectionBody", |
| 160 | + wraps=ConnectionBody, |
| 161 | + ) as mock_body: |
| 162 | + connection_command.import_( |
| 163 | + self.parser.parse_args(["connections", "import", json_path.as_posix()]), |
| 164 | + api_client=api_client, |
| 165 | + ) |
| 166 | + |
| 167 | + # Verify that ``extra`` was passed as None (not {} which would fail |
| 168 | + # Pydantic validation) and all other absent keys default correctly. |
| 169 | + mock_body.assert_called_once_with( |
| 170 | + connection_id=self.connection_id, |
| 171 | + conn_type="test_type", |
| 172 | + host="test_host", |
| 173 | + login=None, |
| 174 | + password=None, |
| 175 | + port=None, |
| 176 | + extra=None, |
| 177 | + description="", |
| 178 | + ) |
0 commit comments