You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by xd...@apache.org on 2020/11/01 17:07:15 UTC

[airflow] branch master updated: Refine request check in api_connextion Pool endpoints (#12019)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 6e29504  Refine request check in api_connextion Pool endpoints (#12019)
6e29504 is described below

commit 6e295042825081a8b45772fb7623b1f2d7f4d986
Author: Xiaodong DENG <xd...@hotmail.com>
AuthorDate: Sun Nov 1 18:06:33 2020 +0100

    Refine request check in api_connextion Pool endpoints (#12019)
    
    If I miss both required properties in my request, I will only be warned one by one.
    That means I need to fix my error for twice, which is not necessary.
    
    This commit helps check all missing properties in one shot.
---
 airflow/api_connexion/endpoints/pool_endpoint.py    | 15 ++++++++-------
 tests/api_connexion/endpoints/test_pool_endpoint.py | 14 ++++++++++----
 2 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/airflow/api_connexion/endpoints/pool_endpoint.py b/airflow/api_connexion/endpoints/pool_endpoint.py
index 1ed5cbf..6f8b4d5 100644
--- a/airflow/api_connexion/endpoints/pool_endpoint.py
+++ b/airflow/api_connexion/endpoints/pool_endpoint.py
@@ -99,9 +99,10 @@ def patch_pool(pool_name, session, update_mask=None):
         patch_body = _patch_body
 
     else:
-        for field in ["name", "slots"]:
-            if field not in request.json.keys():
-                raise BadRequest(detail=f"'{field}' is a required property")
+        required_fields = {"name", "slots"}
+        fields_diff = required_fields - set(request.json.keys())
+        if fields_diff:
+            raise BadRequest(detail=f"Missing required property(ies): {sorted(fields_diff)}")
 
     for key, value in patch_body.items():
         setattr(pool, key, value)
@@ -113,10 +114,10 @@ def patch_pool(pool_name, session, update_mask=None):
 @provide_session
 def post_pool(session):
     """Create a pool"""
-    required_fields = ["name", "slots"]  # Pool would require both fields in the post request
-    for field in required_fields:
-        if field not in request.json.keys():
-            raise BadRequest(detail=f"'{field}' is a required property")
+    required_fields = {"name", "slots"}  # Pool would require both fields in the post request
+    fields_diff = required_fields - set(request.json.keys())
+    if fields_diff:
+        raise BadRequest(detail=f"Missing required property(ies): {sorted(fields_diff)}")
 
     try:
         post_body = pool_schema.load(request.json, session=session)
diff --git a/tests/api_connexion/endpoints/test_pool_endpoint.py b/tests/api_connexion/endpoints/test_pool_endpoint.py
index e099da3..16e8607 100644
--- a/tests/api_connexion/endpoints/test_pool_endpoint.py
+++ b/tests/api_connexion/endpoints/test_pool_endpoint.py
@@ -293,12 +293,17 @@ class TestPostPool(TestBasePoolEndpoints):
             (
                 "for missing pool name",
                 {"slots": 3},
-                "'name' is a required property",
+                "Missing required property(ies): ['name']",
             ),
             (
                 "for missing slots",
                 {"name": "invalid_pool"},
-                "'slots' is a required property",
+                "Missing required property(ies): ['slots']",
+            ),
+            (
+                "for missing pool name AND slots",
+                {},
+                "Missing required property(ies): ['name', 'slots']",
             ),
             (
                 "for extra fields",
@@ -356,8 +361,9 @@ class TestPatchPool(TestBasePoolEndpoints):
     @parameterized.expand(
         [
             # Missing properties
-            ("'name' is a required property", {"slots": 3}),
-            ("'slots' is a required property", {"name": "test_pool_a"}),
+            ("Missing required property(ies): ['name']", {"slots": 3}),
+            ("Missing required property(ies): ['slots']", {"name": "test_pool_a"}),
+            ("Missing required property(ies): ['name', 'slots']", {}),
             # Extra properties
             (
                 "{'extra_field': ['Unknown field.']}",