diff --git a/src/dashboard/apigateway/apigateway/apis/web/resource_version/serializers.py b/src/dashboard/apigateway/apigateway/apis/web/resource_version/serializers.py index 6b0960710..a9327d183 100644 --- a/src/dashboard/apigateway/apigateway/apis/web/resource_version/serializers.py +++ b/src/dashboard/apigateway/apigateway/apis/web/resource_version/serializers.py @@ -16,6 +16,8 @@ # We undertake not to change the open source license (MIT license) applicable # to the current version of the project delivered to anyone in the future. # +import copy + from rest_framework import serializers from apigateway.apps.plugin.constants import PluginBindingScopeEnum @@ -78,7 +80,7 @@ def get_proxy(self, obj): return proxy def get_plugins(self, obj): - plugins = self.context.get("stage_plugins", {}) + plugins = copy.deepcopy(self.context.get("stage_plugins", {})) # v2 才有plugin数据 if not self.context["is_schema_v2"]: diff --git a/src/dashboard/apigateway/apigateway/tests/apis/web/resource_version/test_serializers.py b/src/dashboard/apigateway/apigateway/tests/apis/web/resource_version/test_serializers.py index 0e0c0d9ee..4654983c2 100644 --- a/src/dashboard/apigateway/apigateway/tests/apis/web/resource_version/test_serializers.py +++ b/src/dashboard/apigateway/apigateway/tests/apis/web/resource_version/test_serializers.py @@ -221,3 +221,53 @@ def test_to_representation_v2( "created_by": fake_resource_version_v2.created_by, } assert slz.data == expected_data + + def test_to_representation_v2_with_resources( + self, + fake_backend, + fake_stage, + fake_gateway, + fake_resource2, + echo_plugin_resource_binding, + fake_resource_version_v2, + echo_plugin_stage_binding, + ): + stage_plugin_bindings = PluginBindingHandler.get_stage_plugin_bindings(fake_gateway.id, fake_stage.id) + stage_plugins = {} + for plugin_type, plugin_binding in stage_plugin_bindings.items(): + plugin_config = plugin_binding.snapshot() + plugin_config["binding_type"] = PluginBindingScopeEnum.STAGE.value + stage_plugins[plugin_type] = plugin_config + slz = serializers.ResourceVersionRetrieveOutputSLZ( + instance=fake_resource_version_v2, + context={ + "resource_backends": BackendHandler.get_id_to_instance(fake_gateway.id), + "resource_backend_configs": BackendHandler.get_backend_configs_by_stage( + fake_gateway.id, fake_stage.id + ), + "is_schema_v2": fake_resource_version_v2.is_schema_v2, + "stage_plugins": stage_plugins, + "resource_doc_updated_time": {}, + }, + ) + for resource in slz.data["resources"]: + if resource["name"] == fake_resource2.name: + assert resource["plugins"] == [ + { + "id": echo_plugin_stage_binding.id, + "type": echo_plugin_stage_binding.get_type(), + "name": echo_plugin_stage_binding.config.type.name, + "config": echo_plugin_stage_binding.get_config(), + "binding_type": echo_plugin_stage_binding.scope_type, + }, + ] + else: + assert resource["plugins"] == [ + { + "id": echo_plugin_resource_binding.id, + "type": echo_plugin_resource_binding.get_type(), + "name": echo_plugin_resource_binding.config.type.name, + "config": echo_plugin_resource_binding.get_config(), + "binding_type": echo_plugin_resource_binding.scope_type, + }, + ]