This is an automated email from the ASF dual-hosted git repository. ephraimanierobi pushed a commit to branch v2-7-test in repository https://gitbox.apache.org/repos/asf/airflow.git
commit 2d16c702d05d4a2e8a28174167efac4f7ffe8c59 Author: Abhishek <abhishek.bha...@hotmail.com> AuthorDate: Wed Aug 30 12:36:08 2023 +0530 Raise 404 from Variable PATCH API if variable is not found (#33885) * Raise variable not found if session returns empty * Added detail to the exception for json reponse * tests for patch api when variable doesn't exist * Dropped fstring * Unify varialbe not found message --------- Co-authored-by: Tzu-ping Chung <uranu...@gmail.com> (cherry picked from commit 701c3b80107adb9f4c697f04331c1c7c4e315cd8) --- airflow/api_connexion/endpoints/variable_endpoint.py | 4 +++- tests/api_connexion/endpoints/test_variable_endpoint.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/airflow/api_connexion/endpoints/variable_endpoint.py b/airflow/api_connexion/endpoints/variable_endpoint.py index 61a1871104..8c11e1f075 100644 --- a/airflow/api_connexion/endpoints/variable_endpoint.py +++ b/airflow/api_connexion/endpoints/variable_endpoint.py @@ -59,7 +59,7 @@ def get_variable(*, variable_key: str, session: Session = NEW_SESSION) -> Respon """Get a variable by key.""" var = session.scalar(select(Variable).where(Variable.key == variable_key).limit(1)) if not var: - raise NotFound("Variable not found") + raise NotFound("Variable not found", detail="Variable does not exist") return variable_schema.dump(var) @@ -112,6 +112,8 @@ def patch_variable( raise BadRequest("Invalid post body", detail="key from request body doesn't match uri parameter") non_update_fields = ["key"] variable = session.scalar(select(Variable).filter_by(key=variable_key).limit(1)) + if not variable: + raise NotFound("Variable not found", detail="Variable does not exist") if update_mask: data = extract_update_mask_data(update_mask, non_update_fields, data) for key, val in data.items(): diff --git a/tests/api_connexion/endpoints/test_variable_endpoint.py b/tests/api_connexion/endpoints/test_variable_endpoint.py index 7c6c55d783..c622e0d673 100644 --- a/tests/api_connexion/endpoints/test_variable_endpoint.py +++ b/tests/api_connexion/endpoints/test_variable_endpoint.py @@ -244,6 +244,21 @@ class TestPatchVariable(TestVariableEndpoint): _check_last_log(session, dag_id=None, event="variable.edit", execution_date=None) def test_should_reject_invalid_update(self): + response = self.client.patch( + "/api/v1/variables/var1", + json={ + "key": "var1", + "value": "foo", + }, + environ_overrides={"REMOTE_USER": "test"}, + ) + assert response.status_code == 404 + assert response.json == { + "title": "Variable not found", + "status": 404, + "type": EXCEPTIONS_LINK_MAP[404], + "detail": "Variable does not exist", + } Variable.set("var1", "foo") response = self.client.patch( "/api/v1/variables/var1",