diff --git a/license_manager/apps/api/tests/test_utils.py b/license_manager/apps/api/tests/test_utils.py index 9feea9be..d93cdaec 100644 --- a/license_manager/apps/api/tests/test_utils.py +++ b/license_manager/apps/api/tests/test_utils.py @@ -158,3 +158,78 @@ def test_release_unacquired_lock(self): utils.acquire_subscription_plan_lock(self.plan, other_stuff='yes') released = utils.release_subscription_plan_lock(self.plan) assert released + + +class TestUtils(TestCase): + """ + Tests for license-manager utils + """ + + def test_make_swagger_var_param_optional(self): + # Sample data structure + result_case1 = { + "paths": { + "/example/path/{var}/": { + "get": { + "parameters": [ + { + "name": "var", + "in": "path", + "type": "string", + "required": True, + } + ] + } + } + } + } + + # Verify that the modification is as expected + expected_result_case1 = { + "paths": { + "/example/path/{var}/": { + "get": { + "parameters": [ + { + "name": "var", + "in": "path", + "type": "string", + "required": True, + "allowEmptyValue": True, # This field should be added + } + ] + } + } + } + } + + updated_result_case1 = utils.make_swagger_var_param_optional(result_case1) + self.assertEqual(updated_result_case1, expected_result_case1) + + # Case 2: Var param exists and allowEmptyValue is already present + + updated_result_case2 = utils.make_swagger_var_param_optional( + expected_result_case1 + ) + self.assertEqual(updated_result_case2, expected_result_case1) + + # Case 3: Var param does not exist + result_case3 = { + "paths": { + "/example/path/{param}/": { + "get": { + "parameters": [ + { + "name": "param", + "in": "path", + "type": "string", + "required": True, + } + ] + } + } + } + } + + updated_result_case3 = utils.make_swagger_var_param_optional(result_case3) + self.assertEqual(updated_result_case3, result_case3) # No change expected diff --git a/license_manager/apps/api/utils.py b/license_manager/apps/api/utils.py index b7f19e3c..3557306d 100644 --- a/license_manager/apps/api/utils.py +++ b/license_manager/apps/api/utils.py @@ -320,3 +320,19 @@ def release_subscription_plan_lock(subscription_plan, **cache_key_kwargs): cache_key = get_cache_key(resource='subscription_plan', plan_uuid=subscription_plan.uuid, **cache_key_kwargs) TieredCache.delete_all_tiers(cache_key) return True + + +# pylint: disable=unused-argument +def make_swagger_var_param_optional(result, generator=None, request=None, public=None): + """ + Iterates through the result object generated by drf-spectacular + and sets `allowEmptyValue=true` for `var` path param in swagger view + """ + for methods in result["paths"].values(): + for info in methods.values(): + parameters = info.get("parameters") + if parameters is not None: + for param in parameters: + if param.get("name") == "var": + param["allowEmptyValue"] = True + return result diff --git a/license_manager/settings/base.py b/license_manager/settings/base.py index 53d44d7d..d0a6b687 100644 --- a/license_manager/settings/base.py +++ b/license_manager/settings/base.py @@ -458,4 +458,7 @@ 'DESCRIPTION': 'API for querying and commanding about license manager records.', 'VERSION': '1.0.0', 'SERVE_INCLUDE_SCHEMA': False, + 'POSTPROCESSING_HOOKS': [ + 'license_manager.apps.api.utils.make_swagger_var_param_optional', + ], }