1
1
import copy
2
2
import re
3
- from typing import Dict , Any , Optional
3
+ from typing import Callable , Dict , Any , Optional , TypeVar
4
4
5
+ from samtranslator .metrics .method_decorator import cw_timer
5
6
from samtranslator .model .apigateway import ApiGatewayAuthorizer
6
7
from samtranslator .model .intrinsics import ref , make_conditional , fnSub
7
8
from samtranslator .model .exceptions import InvalidDocumentException , InvalidTemplateException
11
12
from samtranslator .utils .py27hash_fix import Py27Dict , Py27UniStr
12
13
from samtranslator .utils .utils import InvalidValueType , dict_deep_set
13
14
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
+
14
21
15
22
class SwaggerEditor (BaseEditor ):
16
23
"""
@@ -41,6 +48,9 @@ class SwaggerEditor(BaseEditor):
41
48
_POLICY_TYPE_VPC = "Vpc"
42
49
_DISABLE_EXECUTE_API_ENDPOINT = "disableExecuteApiEndpoint"
43
50
51
+ # Attributes:
52
+ _doc : Dict [str , Any ]
53
+
44
54
def __init__ (self , doc : Optional [Dict [str , Any ]]) -> None :
45
55
"""
46
56
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:
53
63
if not doc or not SwaggerEditor .is_valid (doc ):
54
64
raise InvalidDocumentException ([InvalidTemplateException ("Invalid Swagger document" )])
55
65
56
- self ._doc = copy . deepcopy (doc )
66
+ self ._doc = _deepcopy (doc )
57
67
self .paths = self ._doc ["paths" ]
58
68
self .security_definitions = self ._doc .get ("securityDefinitions" , Py27Dict ())
59
69
self .gateway_responses = self ._doc .get (self ._X_APIGW_GATEWAY_RESPONSES , Py27Dict ())
@@ -1206,7 +1216,7 @@ def swagger(self) -> Dict[str, Any]:
1206
1216
if self .definitions :
1207
1217
self ._doc ["definitions" ] = self .definitions
1208
1218
1209
- return copy . deepcopy (self ._doc )
1219
+ return _deepcopy (self ._doc )
1210
1220
1211
1221
@staticmethod
1212
1222
def is_valid (data : Any ) -> bool :
0 commit comments