@@ -236,6 +236,8 @@ def __init__(
236
236
self .template_conditions = template_conditions
237
237
self .mode = mode
238
238
239
+ self .swagger_editor = SwaggerEditor (self .definition_body ) if self .definition_body else None
240
+
239
241
def _construct_rest_api (self ):
240
242
"""Constructs and returns the ApiGateway RestApi.
241
243
@@ -282,7 +284,7 @@ def _construct_rest_api(self):
282
284
rest_api .BodyS3Location = self ._construct_body_s3_dict ()
283
285
elif self .definition_body :
284
286
# # Post Process OpenApi Auth Settings
285
- self .definition_body = self ._openapi_postprocess (self .definition_body )
287
+ self .definition_body = self ._openapi_postprocess (self .swagger_editor . swagger )
286
288
rest_api .Body = self .definition_body
287
289
288
290
if self .name :
@@ -308,9 +310,7 @@ def _add_endpoint_extension(self):
308
310
raise InvalidResourceException (
309
311
self .logical_id , "DisableExecuteApiEndpoint works only within 'DefinitionBody' property."
310
312
)
311
- editor = SwaggerEditor (self .definition_body )
312
- editor .add_disable_execute_api_endpoint_extension (self .disable_execute_api_endpoint )
313
- self .definition_body = editor .swagger
313
+ self .swagger_editor .add_disable_execute_api_endpoint_extension (self .disable_execute_api_endpoint )
314
314
315
315
def _construct_body_s3_dict (self ):
316
316
"""Constructs the RestApi's `BodyS3Location property`_, from the SAM Api's DefinitionUri property.
@@ -620,13 +620,6 @@ def _add_cors(self):
620
620
else :
621
621
raise InvalidResourceException (self .logical_id , INVALID_ERROR )
622
622
623
- if not SwaggerEditor .is_valid (self .definition_body ):
624
- raise InvalidResourceException (
625
- self .logical_id ,
626
- "Unable to add Cors configuration because "
627
- "'DefinitionBody' does not contain a valid Swagger definition." ,
628
- )
629
-
630
623
if properties .AllowCredentials is True and properties .AllowOrigin == _CORS_WILDCARD :
631
624
raise InvalidResourceException (
632
625
self .logical_id ,
@@ -635,10 +628,9 @@ def _add_cors(self):
635
628
"'AllowOrigin' is \" '*'\" or not set" ,
636
629
)
637
630
638
- editor = SwaggerEditor (self .definition_body )
639
- for path in editor .iter_on_path ():
631
+ for path in self .swagger_editor .iter_on_path ():
640
632
try :
641
- editor .add_cors (
633
+ self . swagger_editor .add_cors (
642
634
path ,
643
635
properties .AllowOrigin ,
644
636
properties .AllowHeaders ,
@@ -649,9 +641,6 @@ def _add_cors(self):
649
641
except InvalidTemplateException as ex :
650
642
raise InvalidResourceException (self .logical_id , ex .message )
651
643
652
- # Assign the Swagger back to template
653
- self .definition_body = editor .swagger
654
-
655
644
def _add_binary_media_types (self ):
656
645
"""
657
646
Add binary media types to Swagger
@@ -664,11 +653,7 @@ def _add_binary_media_types(self):
664
653
if self .binary_media and not self .definition_body :
665
654
return
666
655
667
- editor = SwaggerEditor (self .definition_body )
668
- editor .add_binary_media_types (self .binary_media )
669
-
670
- # Assign the Swagger back to template
671
- self .definition_body = editor .swagger
656
+ self .swagger_editor .add_binary_media_types (self .binary_media )
672
657
673
658
def _add_auth (self ):
674
659
"""
@@ -687,40 +672,31 @@ def _add_auth(self):
687
672
if not all (key in AuthProperties ._fields for key in self .auth .keys ()):
688
673
raise InvalidResourceException (self .logical_id , "Invalid value for 'Auth' property" )
689
674
690
- if not SwaggerEditor .is_valid (self .definition_body ):
691
- raise InvalidResourceException (
692
- self .logical_id ,
693
- "Unable to add Auth configuration because "
694
- "'DefinitionBody' does not contain a valid Swagger definition." ,
695
- )
696
- swagger_editor = SwaggerEditor (self .definition_body )
697
675
auth_properties = AuthProperties (** self .auth )
698
676
authorizers = self ._get_authorizers (auth_properties .Authorizers , auth_properties .DefaultAuthorizer )
699
677
700
678
if authorizers :
701
- swagger_editor .add_authorizers_security_definitions (authorizers )
679
+ self . swagger_editor .add_authorizers_security_definitions (authorizers )
702
680
self ._set_default_authorizer (
703
- swagger_editor ,
681
+ self . swagger_editor ,
704
682
authorizers ,
705
683
auth_properties .DefaultAuthorizer ,
706
684
auth_properties .AddDefaultAuthorizerToCorsPreflight ,
707
685
auth_properties .Authorizers ,
708
686
)
709
687
710
688
if auth_properties .ApiKeyRequired :
711
- swagger_editor .add_apikey_security_definition ()
712
- self ._set_default_apikey_required (swagger_editor )
689
+ self . swagger_editor .add_apikey_security_definition ()
690
+ self ._set_default_apikey_required (self . swagger_editor )
713
691
714
692
if auth_properties .ResourcePolicy :
715
693
SwaggerEditor .validate_is_dict (
716
694
auth_properties .ResourcePolicy , "ResourcePolicy must be a map (ResourcePolicyStatement)."
717
695
)
718
- for path in swagger_editor .iter_on_path ():
719
- swagger_editor .add_resource_policy (auth_properties .ResourcePolicy , path , self .stage_name )
696
+ for path in self . swagger_editor .iter_on_path ():
697
+ self . swagger_editor .add_resource_policy (auth_properties .ResourcePolicy , path , self .stage_name )
720
698
if auth_properties .ResourcePolicy .get ("CustomStatements" ):
721
- swagger_editor .add_custom_statements (auth_properties .ResourcePolicy .get ("CustomStatements" ))
722
-
723
- self .definition_body = self ._openapi_postprocess (swagger_editor .swagger )
699
+ self .swagger_editor .add_custom_statements (auth_properties .ResourcePolicy .get ("CustomStatements" ))
724
700
725
701
def _construct_usage_plan (self , rest_api_stage = None ):
726
702
"""Constructs and returns the ApiGateway UsagePlan, ApiGateway UsagePlanKey, ApiGateway ApiKey for Auth.
@@ -923,15 +899,6 @@ def _add_gateway_responses(self):
923
899
),
924
900
)
925
901
926
- if not SwaggerEditor .is_valid (self .definition_body ):
927
- raise InvalidResourceException (
928
- self .logical_id ,
929
- "Unable to add Auth configuration because "
930
- "'DefinitionBody' does not contain a valid Swagger definition." ,
931
- )
932
-
933
- swagger_editor = SwaggerEditor (self .definition_body )
934
-
935
902
# The dicts below will eventually become part of swagger/openapi definition, thus requires using Py27Dict()
936
903
gateway_responses = Py27Dict ()
937
904
for response_type , response in self .gateway_responses .items ():
@@ -943,10 +910,7 @@ def _add_gateway_responses(self):
943
910
)
944
911
945
912
if gateway_responses :
946
- swagger_editor .add_gateway_responses (gateway_responses )
947
-
948
- # Assign the Swagger back to template
949
- self .definition_body = swagger_editor .swagger
913
+ self .swagger_editor .add_gateway_responses (gateway_responses )
950
914
951
915
def _add_models (self ):
952
916
"""
@@ -962,22 +926,10 @@ def _add_models(self):
962
926
self .logical_id , "Models works only with inline Swagger specified in " "'DefinitionBody' property."
963
927
)
964
928
965
- if not SwaggerEditor .is_valid (self .definition_body ):
966
- raise InvalidResourceException (
967
- self .logical_id ,
968
- "Unable to add Models definitions because "
969
- "'DefinitionBody' does not contain a valid Swagger definition." ,
970
- )
971
-
972
929
if not all (isinstance (model , dict ) for model in self .models .values ()):
973
930
raise InvalidResourceException (self .logical_id , "Invalid value for 'Models' property" )
974
931
975
- swagger_editor = SwaggerEditor (self .definition_body )
976
- swagger_editor .add_models (self .models )
977
-
978
- # Assign the Swagger back to template
979
-
980
- self .definition_body = self ._openapi_postprocess (swagger_editor .swagger )
932
+ self .swagger_editor .add_models (self .models )
981
933
982
934
def _openapi_postprocess (self , definition_body ):
983
935
"""
0 commit comments