Skip to content

Commit 2f53aeb

Browse files
authored
chore: Add cw_timer to deepcopy in SwaggerEditor and OpenApiEditor (#2764)
1 parent bdfcc0a commit 2f53aeb

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

samtranslator/open_api/open_api.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import copy
22
import re
3-
from typing import Any, Dict, Optional
3+
from typing import Callable, Any, Dict, Optional, TypeVar
44

5+
from samtranslator.metrics.method_decorator import cw_timer
56
from samtranslator.model.apigatewayv2 import ApiGatewayV2Authorizer
67
from samtranslator.model.intrinsics import ref, make_conditional, is_intrinsic
78
from samtranslator.model.exceptions import InvalidDocumentException, InvalidTemplateException
@@ -12,6 +13,13 @@
1213
import json
1314

1415

16+
T = TypeVar("T")
17+
18+
19+
# Wrap around copy.deepcopy to isolate time cost to deepcopy the doc.
20+
_deepcopy: Callable[[T], T] = cw_timer(prefix="OpenApiEditor")(copy.deepcopy)
21+
22+
1523
class OpenApiEditor(BaseEditor):
1624
"""
1725
Wrapper class capable of parsing and generating OpenApi JSON. This implements OpenApi spec just enough that SAM
@@ -32,6 +40,9 @@ class OpenApiEditor(BaseEditor):
3240
_DEFAULT_PATH = "$default"
3341
_DEFAULT_OPENAPI_TITLE = ref("AWS::StackName")
3442

43+
# Attributes:
44+
_doc: Dict[str, Any]
45+
3546
def __init__(self, doc: Optional[Dict[str, Any]]) -> None:
3647
"""
3748
Initialize the class with a swagger dictionary. This class creates a copy of the Swagger and performs all
@@ -49,7 +60,7 @@ def __init__(self, doc: Optional[Dict[str, Any]]) -> None:
4960
]
5061
)
5162

52-
self._doc = copy.deepcopy(doc)
63+
self._doc = _deepcopy(doc)
5364
self.paths = self._doc["paths"]
5465
try:
5566
self.security_schemes = dict_deep_get(self._doc, "components.securitySchemes") or Py27Dict()
@@ -527,7 +538,7 @@ def openapi(self) -> Dict[str, Any]:
527538
if self.info:
528539
self._doc["info"] = self.info
529540

530-
return copy.deepcopy(self._doc)
541+
return _deepcopy(self._doc)
531542

532543
@staticmethod
533544
def is_valid(data: Any) -> bool:

samtranslator/swagger/swagger.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import copy
22
import re
3-
from typing import Dict, Any, Optional
3+
from typing import Callable, Dict, Any, Optional, TypeVar
44

5+
from samtranslator.metrics.method_decorator import cw_timer
56
from samtranslator.model.apigateway import ApiGatewayAuthorizer
67
from samtranslator.model.intrinsics import ref, make_conditional, fnSub
78
from samtranslator.model.exceptions import InvalidDocumentException, InvalidTemplateException
@@ -11,6 +12,12 @@
1112
from samtranslator.utils.py27hash_fix import Py27Dict, Py27UniStr
1213
from samtranslator.utils.utils import InvalidValueType, dict_deep_set
1314

15+
T = TypeVar("T")
16+
17+
18+
# Wrap around copy.deepcopy to isolate time cost to deepcopy the doc.
19+
_deepcopy: Callable[[T], T] = cw_timer(prefix="SwaggerEditor")(copy.deepcopy)
20+
1421

1522
class SwaggerEditor(BaseEditor):
1623
"""
@@ -41,6 +48,9 @@ class SwaggerEditor(BaseEditor):
4148
_POLICY_TYPE_VPC = "Vpc"
4249
_DISABLE_EXECUTE_API_ENDPOINT = "disableExecuteApiEndpoint"
4350

51+
# Attributes:
52+
_doc: Dict[str, Any]
53+
4454
def __init__(self, doc: Optional[Dict[str, Any]]) -> None:
4555
"""
4656
Initialize the class with a swagger dictionary. This class creates a copy of the Swagger and performs all
@@ -53,7 +63,7 @@ def __init__(self, doc: Optional[Dict[str, Any]]) -> None:
5363
if not doc or not SwaggerEditor.is_valid(doc):
5464
raise InvalidDocumentException([InvalidTemplateException("Invalid Swagger document")])
5565

56-
self._doc = copy.deepcopy(doc)
66+
self._doc = _deepcopy(doc)
5767
self.paths = self._doc["paths"]
5868
self.security_definitions = self._doc.get("securityDefinitions", Py27Dict())
5969
self.gateway_responses = self._doc.get(self._X_APIGW_GATEWAY_RESPONSES, Py27Dict())
@@ -1206,7 +1216,7 @@ def swagger(self) -> Dict[str, Any]:
12061216
if self.definitions:
12071217
self._doc["definitions"] = self.definitions
12081218

1209-
return copy.deepcopy(self._doc)
1219+
return _deepcopy(self._doc)
12101220

12111221
@staticmethod
12121222
def is_valid(data: Any) -> bool:

0 commit comments

Comments
 (0)