@@ -139,16 +139,7 @@ def add_path(self, path, method=None):
139
139
method = self ._normalize_method_name (method )
140
140
141
141
path_dict = self .paths .setdefault (path , {})
142
-
143
- if not isinstance (path_dict , dict ):
144
- # Either customers has provided us an invalid Swagger, or this class has messed it somehow
145
- raise InvalidDocumentException (
146
- [
147
- InvalidTemplateException (
148
- "Value of '{}' path must be a dictionary according to Swagger spec." .format (path )
149
- )
150
- ]
151
- )
142
+ SwaggerEditor .validate_path_item_is_dict (path_dict , path )
152
143
153
144
if self ._CONDITIONAL_IF in path_dict :
154
145
path_dict = path_dict [self ._CONDITIONAL_IF ][1 ]
@@ -536,15 +527,10 @@ def set_path_default_authorizer(
536
527
# It is possible that the method could have two definitions in a Fn::If block.
537
528
for method_definition in self .get_method_contents (method ):
538
529
530
+ SwaggerEditor .validate_is_dict (
531
+ method_definition , "{} for path {} is not a valid dictionary." .format (method_definition , path )
532
+ )
539
533
# If no integration given, then we don't need to process this definition (could be AWS::NoValue)
540
- if not isinstance (method_definition , dict ):
541
- raise InvalidDocumentException (
542
- [
543
- InvalidTemplateException (
544
- "{} for path {} is not a valid dictionary." .format (method_definition , path )
545
- )
546
- ]
547
- )
548
534
if not self .method_definition_has_integration (method_definition ):
549
535
continue
550
536
existing_security = method_definition .get ("security" , [])
@@ -559,14 +545,9 @@ def set_path_default_authorizer(
559
545
# (e.g. sigv4 (AWS_IAM), api_key (API Key/Usage Plans), NONE (marker for ignoring default))
560
546
# We want to ensure only a single Authorizer security entry exists while keeping everything else
561
547
for security in existing_security :
562
- if not isinstance (security , dict ):
563
- raise InvalidDocumentException (
564
- [
565
- InvalidTemplateException (
566
- "{} in Security for path {} is not a valid dictionary." .format (security , path )
567
- )
568
- ]
569
- )
548
+ SwaggerEditor .validate_is_dict (
549
+ security , "{} in Security for path {} is not a valid dictionary." .format (security , path )
550
+ )
570
551
if authorizer_names .isdisjoint (security .keys ()):
571
552
existing_non_authorizer_security .append (security )
572
553
else :
@@ -912,8 +893,7 @@ def add_resource_policy(self, resource_policy, path, stage):
912
893
"""
913
894
if resource_policy is None :
914
895
return
915
- if not isinstance (resource_policy , dict ):
916
- raise InvalidDocumentException ([InvalidTemplateException ("Resource Policy is not a valid dictionary." )])
896
+ SwaggerEditor .validate_is_dict (resource_policy , "Resource Policy is not a valid dictionary." )
917
897
918
898
aws_account_whitelist = resource_policy .get ("AwsAccountWhitelist" )
919
899
aws_account_blacklist = resource_policy .get ("AwsAccountBlacklist" )
@@ -1244,6 +1224,31 @@ def is_valid(data):
1244
1224
)
1245
1225
return False
1246
1226
1227
+ @staticmethod
1228
+ def validate_is_dict (obj , exception_message ):
1229
+ """
1230
+ Throws exception if obj is not a dict
1231
+
1232
+ :param obj: object being validated
1233
+ :param exception_message: message to include in exception if obj is not a dict
1234
+ """
1235
+
1236
+ if not isinstance (obj , dict ):
1237
+ raise InvalidDocumentException ([InvalidTemplateException (exception_message )])
1238
+
1239
+ @staticmethod
1240
+ def validate_path_item_is_dict (path_item , path ):
1241
+ """
1242
+ Throws exception if path_item is not a dict
1243
+
1244
+ :param path_item: path_item (value at the path) being validated
1245
+ :param path: path name
1246
+ """
1247
+
1248
+ SwaggerEditor .validate_is_dict (
1249
+ path_item , "Value of '{}' path must be a dictionary according to Swagger spec." .format (path )
1250
+ )
1251
+
1247
1252
@staticmethod
1248
1253
def gen_skeleton ():
1249
1254
"""
0 commit comments