From 0a80d417ae868594e6307aabcf503b87afc48d56 Mon Sep 17 00:00:00 2001 From: nannan00 <17491932+nannan00@users.noreply.github.com> Date: Mon, 20 Nov 2023 20:41:11 +0800 Subject: [PATCH] fix: json schema without any ref --- .../apis/web/idp/{swagger.py => schema.py} | 17 +++++++++++++---- src/bk-user/bkuser/apis/web/idp/views.py | 11 +++++------ src/bk-user/tests/apis/web/idp/test_idp.py | 9 --------- 3 files changed, 18 insertions(+), 19 deletions(-) rename src/bk-user/bkuser/apis/web/idp/{swagger.py => schema.py} (62%) diff --git a/src/bk-user/bkuser/apis/web/idp/swagger.py b/src/bk-user/bkuser/apis/web/idp/schema.py similarity index 62% rename from src/bk-user/bkuser/apis/web/idp/swagger.py rename to src/bk-user/bkuser/apis/web/idp/schema.py index c47dfc407..fe3fa4014 100644 --- a/src/bk-user/bkuser/apis/web/idp/swagger.py +++ b/src/bk-user/bkuser/apis/web/idp/schema.py @@ -8,17 +8,26 @@ an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ -from typing import Dict +import json +from typing import Any, Dict +import jsonref from drf_yasg import openapi -from bkuser.idp_plugins.base import list_plugin_cls +from bkuser.idp_plugins.base import get_plugin_cfg_cls, list_plugin_cls from bkuser.utils.pydantic import gen_openapi_schema -def get_idp_plugin_cfg_schema_map() -> Dict[str, openapi.Schema]: - """获取认证插件配置类 JsonSchema 映射表""" +def get_idp_plugin_cfg_openapi_schema_map() -> Dict[str, openapi.Schema]: + """获取认证插件配置类 JsonSchema 映射表,输出是以openapi schema格式""" return { f"plugin_config:{plugin_cls.id}": gen_openapi_schema(plugin_cls.config_class) for plugin_cls in list_plugin_cls() } + + +def get_idp_plugin_cfg_json_schema(plugin_id: str) -> Dict[str, Any]: + """获取认证源插件配置类的 JsonSchema, without any jsonRef""" + json_schema = get_plugin_cfg_cls(plugin_id).model_json_schema() + # replace json refs + return jsonref.loads(json.dumps(json_schema)) diff --git a/src/bk-user/bkuser/apis/web/idp/views.py b/src/bk-user/bkuser/apis/web/idp/views.py index 38f44916b..e7a1a1d4c 100644 --- a/src/bk-user/bkuser/apis/web/idp/views.py +++ b/src/bk-user/bkuser/apis/web/idp/views.py @@ -21,8 +21,8 @@ from bkuser.apps.permission.constants import PermAction from bkuser.apps.permission.permissions import perm_class from bkuser.common.error_codes import error_codes -from bkuser.idp_plugins.base import get_plugin_cfg_cls +from .schema import get_idp_plugin_cfg_json_schema, get_idp_plugin_cfg_openapi_schema_map from .serializers import ( IdpCreateInputSLZ, IdpCreateOutputSLZ, @@ -34,7 +34,6 @@ IdpSearchOutputSLZ, IdpUpdateInputSLZ, ) -from .swagger import get_idp_plugin_cfg_schema_map class IdpPluginListApi(generics.ListAPIView): @@ -66,7 +65,7 @@ def get(self, request, *args, **kwargs): instance = self.get_object() try: - json_schema = get_plugin_cfg_cls(instance.id).model_json_schema() + json_schema = get_idp_plugin_cfg_json_schema(instance.id) except NotImplementedError: raise error_codes.IDP_PLUGIN_NOT_LOAD @@ -115,7 +114,7 @@ def get(self, request, *args, **kwargs): tags=["idp"], operation_description="新建认证源", request_body=IdpCreateInputSLZ(), - responses={status.HTTP_201_CREATED: IdpCreateOutputSLZ(), **get_idp_plugin_cfg_schema_map()}, + responses={status.HTTP_201_CREATED: IdpCreateOutputSLZ(), **get_idp_plugin_cfg_openapi_schema_map()}, ) def post(self, request, *args, **kwargs): current_tenant_id = self.get_current_tenant_id() @@ -154,7 +153,7 @@ def get_queryset(self): operation_description="认证源详情", responses={ status.HTTP_200_OK: IdpRetrieveOutputSLZ(), - **get_idp_plugin_cfg_schema_map(), + **get_idp_plugin_cfg_openapi_schema_map(), }, ) def get(self, request, *args, **kwargs): @@ -183,7 +182,7 @@ def patch(self, request, *args, **kwargs): tags=["idp"], operation_description="更新认证源", request_body=IdpUpdateInputSLZ(), - responses={status.HTTP_204_NO_CONTENT: "", **get_idp_plugin_cfg_schema_map()}, + responses={status.HTTP_204_NO_CONTENT: "", **get_idp_plugin_cfg_openapi_schema_map()}, ) def put(self, request, *args, **kwargs): idp = self.get_object() diff --git a/src/bk-user/tests/apis/web/idp/test_idp.py b/src/bk-user/tests/apis/web/idp/test_idp.py index 26acf9e42..2e525d890 100644 --- a/src/bk-user/tests/apis/web/idp/test_idp.py +++ b/src/bk-user/tests/apis/web/idp/test_idp.py @@ -13,7 +13,6 @@ import pytest from bkuser.apps.data_source.models import DataSource from bkuser.apps.idp.models import Idp, IdpPlugin -from bkuser.idp_plugins.base import list_plugin_cls from bkuser.idp_plugins.constants import BuiltinIdpPluginEnum from django.urls import reverse from rest_framework import status @@ -80,14 +79,6 @@ def test_list(self, api_client): assert BuiltinIdpPluginEnum.LOCAL in [i["id"] for i in resp.data] -class TestIdpPluginConfigMetaRetrieveApi: - def test_retrieve(self, api_client): - for plugin_cls in list_plugin_cls(): - resp = api_client.get(reverse("idp_plugin_config_meta.retrieve", kwargs={"id": plugin_cls.id})) - assert resp.data["id"] == plugin_cls.id - assert resp.data["json_schema"] == plugin_cls.config_class.model_json_schema() - - class TestIdpCreateApi: def test_create_with_wecom_idp(self, api_client, wecom_plugin_cfg, data_source_match_rules): resp = api_client.post(