You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by ep...@apache.org on 2023/08/30 07:06:15 UTC

[airflow] branch main updated: Raise 404 from Variable PATCH API if variable is not found (#33885)

This is an automated email from the ASF dual-hosted git repository.

ephraimanierobi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 701c3b8010 Raise 404 from Variable PATCH API if variable is not found (#33885)
701c3b8010 is described below

commit 701c3b80107adb9f4c697f04331c1c7c4e315cd8
Author: Abhishek <ab...@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 <ur...@gmail.com>
---
 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 ee29e38d7f..54d5ac744b 100644
--- a/airflow/api_connexion/endpoints/variable_endpoint.py
+++ b/airflow/api_connexion/endpoints/variable_endpoint.py
@@ -63,7 +63,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)
 
 
@@ -116,6 +116,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",