Skip to content

Commit

Permalink
fix: json schema without any ref
Browse files Browse the repository at this point in the history
  • Loading branch information
nannan00 committed Nov 20, 2023
1 parent 383a1c2 commit 0a80d41
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
11 changes: 5 additions & 6 deletions src/bk-user/bkuser/apis/web/idp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -34,7 +34,6 @@
IdpSearchOutputSLZ,
IdpUpdateInputSLZ,
)
from .swagger import get_idp_plugin_cfg_schema_map


class IdpPluginListApi(generics.ListAPIView):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down
9 changes: 0 additions & 9 deletions src/bk-user/tests/apis/web/idp/test_idp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 0a80d41

Please sign in to comment.