@@ -236,8 +236,6 @@ 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
-
241
239
def _construct_rest_api (self ):
242
240
"""Constructs and returns the ApiGateway RestApi.
243
241
@@ -284,7 +282,7 @@ def _construct_rest_api(self):
284
282
rest_api .BodyS3Location = self ._construct_body_s3_dict ()
285
283
elif self .definition_body :
286
284
# # Post Process OpenApi Auth Settings
287
- self .definition_body = self ._openapi_postprocess (self .swagger_editor . swagger )
285
+ self .definition_body = self ._openapi_postprocess (self .definition_body )
288
286
rest_api .Body = self .definition_body
289
287
290
288
if self .name :
@@ -310,7 +308,9 @@ def _add_endpoint_extension(self):
310
308
raise InvalidResourceException (
311
309
self .logical_id , "DisableExecuteApiEndpoint works only within 'DefinitionBody' property."
312
310
)
313
- self .swagger_editor .add_disable_execute_api_endpoint_extension (self .disable_execute_api_endpoint )
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
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.
@@ -626,6 +626,13 @@ def _add_cors(self):
626
626
else :
627
627
raise InvalidResourceException (self .logical_id , INVALID_ERROR )
628
628
629
+ if not SwaggerEditor .is_valid (self .definition_body ):
630
+ raise InvalidResourceException (
631
+ self .logical_id ,
632
+ "Unable to add Cors configuration because "
633
+ "'DefinitionBody' does not contain a valid Swagger definition." ,
634
+ )
635
+
629
636
if properties .AllowCredentials is True and properties .AllowOrigin == _CORS_WILDCARD :
630
637
raise InvalidResourceException (
631
638
self .logical_id ,
@@ -634,9 +641,10 @@ def _add_cors(self):
634
641
"'AllowOrigin' is \" '*'\" or not set" ,
635
642
)
636
643
637
- for path in self .swagger_editor .iter_on_path ():
644
+ editor = SwaggerEditor (self .definition_body )
645
+ for path in editor .iter_on_path ():
638
646
try :
639
- self . swagger_editor .add_cors (
647
+ editor .add_cors (
640
648
path ,
641
649
properties .AllowOrigin ,
642
650
properties .AllowHeaders ,
@@ -647,6 +655,9 @@ def _add_cors(self):
647
655
except InvalidTemplateException as ex :
648
656
raise InvalidResourceException (self .logical_id , ex .message )
649
657
658
+ # Assign the Swagger back to template
659
+ self .definition_body = editor .swagger
660
+
650
661
def _add_binary_media_types (self ):
651
662
"""
652
663
Add binary media types to Swagger
@@ -659,7 +670,11 @@ def _add_binary_media_types(self):
659
670
if self .binary_media and not self .definition_body :
660
671
return
661
672
662
- self .swagger_editor .add_binary_media_types (self .binary_media )
673
+ editor = SwaggerEditor (self .definition_body )
674
+ editor .add_binary_media_types (self .binary_media )
675
+
676
+ # Assign the Swagger back to template
677
+ self .definition_body = editor .swagger
663
678
664
679
def _add_auth (self ):
665
680
"""
@@ -678,31 +693,40 @@ def _add_auth(self):
678
693
if not all (key in AuthProperties ._fields for key in self .auth .keys ()):
679
694
raise InvalidResourceException (self .logical_id , "Invalid value for 'Auth' property" )
680
695
696
+ if not SwaggerEditor .is_valid (self .definition_body ):
697
+ raise InvalidResourceException (
698
+ self .logical_id ,
699
+ "Unable to add Auth configuration because "
700
+ "'DefinitionBody' does not contain a valid Swagger definition." ,
701
+ )
702
+ swagger_editor = SwaggerEditor (self .definition_body )
681
703
auth_properties = AuthProperties (** self .auth )
682
704
authorizers = self ._get_authorizers (auth_properties .Authorizers , auth_properties .DefaultAuthorizer )
683
705
684
706
if authorizers :
685
- self . swagger_editor .add_authorizers_security_definitions (authorizers )
707
+ swagger_editor .add_authorizers_security_definitions (authorizers )
686
708
self ._set_default_authorizer (
687
- self . swagger_editor ,
709
+ swagger_editor ,
688
710
authorizers ,
689
711
auth_properties .DefaultAuthorizer ,
690
712
auth_properties .AddDefaultAuthorizerToCorsPreflight ,
691
713
auth_properties .Authorizers ,
692
714
)
693
715
694
716
if auth_properties .ApiKeyRequired :
695
- self . swagger_editor .add_apikey_security_definition ()
696
- self ._set_default_apikey_required (self . swagger_editor )
717
+ swagger_editor .add_apikey_security_definition ()
718
+ self ._set_default_apikey_required (swagger_editor )
697
719
698
720
if auth_properties .ResourcePolicy :
699
721
SwaggerEditor .validate_is_dict (
700
722
auth_properties .ResourcePolicy , "ResourcePolicy must be a map (ResourcePolicyStatement)."
701
723
)
702
- for path in self . swagger_editor .iter_on_path ():
703
- self . swagger_editor .add_resource_policy (auth_properties .ResourcePolicy , path , self .stage_name )
724
+ for path in swagger_editor .iter_on_path ():
725
+ swagger_editor .add_resource_policy (auth_properties .ResourcePolicy , path , self .stage_name )
704
726
if auth_properties .ResourcePolicy .get ("CustomStatements" ):
705
- self .swagger_editor .add_custom_statements (auth_properties .ResourcePolicy .get ("CustomStatements" ))
727
+ swagger_editor .add_custom_statements (auth_properties .ResourcePolicy .get ("CustomStatements" ))
728
+
729
+ self .definition_body = self ._openapi_postprocess (swagger_editor .swagger )
706
730
707
731
def _construct_usage_plan (self , rest_api_stage = None ):
708
732
"""Constructs and returns the ApiGateway UsagePlan, ApiGateway UsagePlanKey, ApiGateway ApiKey for Auth.
@@ -905,6 +929,15 @@ def _add_gateway_responses(self):
905
929
),
906
930
)
907
931
932
+ if not SwaggerEditor .is_valid (self .definition_body ):
933
+ raise InvalidResourceException (
934
+ self .logical_id ,
935
+ "Unable to add Auth configuration because "
936
+ "'DefinitionBody' does not contain a valid Swagger definition." ,
937
+ )
938
+
939
+ swagger_editor = SwaggerEditor (self .definition_body )
940
+
908
941
# The dicts below will eventually become part of swagger/openapi definition, thus requires using Py27Dict()
909
942
gateway_responses = Py27Dict ()
910
943
for response_type , response in self .gateway_responses .items ():
@@ -916,7 +949,10 @@ def _add_gateway_responses(self):
916
949
)
917
950
918
951
if gateway_responses :
919
- self .swagger_editor .add_gateway_responses (gateway_responses )
952
+ swagger_editor .add_gateway_responses (gateway_responses )
953
+
954
+ # Assign the Swagger back to template
955
+ self .definition_body = swagger_editor .swagger
920
956
921
957
def _add_models (self ):
922
958
"""
@@ -932,10 +968,22 @@ def _add_models(self):
932
968
self .logical_id , "Models works only with inline Swagger specified in " "'DefinitionBody' property."
933
969
)
934
970
971
+ if not SwaggerEditor .is_valid (self .definition_body ):
972
+ raise InvalidResourceException (
973
+ self .logical_id ,
974
+ "Unable to add Models definitions because "
975
+ "'DefinitionBody' does not contain a valid Swagger definition." ,
976
+ )
977
+
935
978
if not all (isinstance (model , dict ) for model in self .models .values ()):
936
979
raise InvalidResourceException (self .logical_id , "Invalid value for 'Models' property" )
937
980
938
- self .swagger_editor .add_models (self .models )
981
+ swagger_editor = SwaggerEditor (self .definition_body )
982
+ swagger_editor .add_models (self .models )
983
+
984
+ # Assign the Swagger back to template
985
+
986
+ self .definition_body = self ._openapi_postprocess (swagger_editor .swagger )
939
987
940
988
def _openapi_postprocess (self , definition_body ):
941
989
"""
0 commit comments