Skip to content

Commit fd718bb

Browse files
praneetapkeetonian
authored andcommitted
fix: openapi version type errors (#1104)
1 parent f4581f5 commit fd718bb

13 files changed

+91
-55
lines changed

samtranslator/model/api/api_generator.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from collections import namedtuple
22
from six import string_types
3-
import re
4-
53
from samtranslator.model.intrinsics import ref
64
from samtranslator.model.apigateway import (ApiGatewayDeployment, ApiGatewayRestApi,
75
ApiGatewayStage, ApiGatewayAuthorizer,
@@ -107,9 +105,10 @@ def _construct_rest_api(self):
107105
"Specify either 'DefinitionUri' or 'DefinitionBody' property and not both")
108106

109107
if self.open_api_version:
110-
if re.match(SwaggerEditor.get_openapi_versions_supported_regex(), self.open_api_version) is None:
111-
raise InvalidResourceException(
112-
self.logical_id, "The OpenApiVersion value must be of the format 3.0.0")
108+
if not SwaggerEditor.safe_compare_regex_with_string(SwaggerEditor.get_openapi_versions_supported_regex(),
109+
self.open_api_version):
110+
raise InvalidResourceException(self.logical_id,
111+
"The OpenApiVersion value must be of the format \"3.0.0\"")
113112

114113
self._add_cors()
115114
self._add_auth()
@@ -412,11 +411,12 @@ def _openapi_postprocess(self, definition_body):
412411
if definition_body.get('swagger') is not None:
413412
return definition_body
414413

415-
if definition_body.get('openapi') is not None:
416-
if self.open_api_version is None:
417-
self.open_api_version = definition_body.get('openapi')
414+
if definition_body.get('openapi') is not None and self.open_api_version is None:
415+
self.open_api_version = definition_body.get('openapi')
418416

419-
if self.open_api_version and re.match(SwaggerEditor.get_openapi_version_3_regex(), self.open_api_version):
417+
if self.open_api_version and \
418+
SwaggerEditor.safe_compare_regex_with_string(SwaggerEditor.get_openapi_version_3_regex(),
419+
self.open_api_version):
420420
if definition_body.get('securityDefinitions'):
421421
components = definition_body.get('components', {})
422422
components['securitySchemes'] = definition_body['securityDefinitions']

samtranslator/plugins/globals/globals.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from samtranslator.public.sdk.resource import SamResourceType
22
from samtranslator.public.intrinsics import is_intrinsics
33
from samtranslator.swagger.swagger import SwaggerEditor
4-
import re
4+
from six import string_types
55

66

77
class Globals(object):
@@ -135,8 +135,11 @@ def fix_openapi_definitions(cls, template):
135135
if ("Type" in resource) and (resource["Type"] == cls._API_TYPE):
136136
properties = resource["Properties"]
137137
if (cls._OPENAPIVERSION in properties) and (cls._MANAGE_SWAGGER in properties) and \
138-
(re.match(SwaggerEditor.get_openapi_version_3_regex(),
139-
properties[cls._OPENAPIVERSION]) is not None):
138+
SwaggerEditor.safe_compare_regex_with_string(
139+
SwaggerEditor.get_openapi_version_3_regex(), properties[cls._OPENAPIVERSION]):
140+
if not isinstance(properties[cls._OPENAPIVERSION], string_types):
141+
properties[cls._OPENAPIVERSION] = str(properties[cls._OPENAPIVERSION])
142+
resource["Properties"] = properties
140143
if "DefinitionBody" in properties:
141144
definition_body = properties['DefinitionBody']
142145
definition_body['openapi'] = properties[cls._OPENAPIVERSION]

samtranslator/swagger/swagger.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,8 @@ def add_request_model_to_method(self, path, method_name, request_model):
740740
method_definition['parameters'] = existing_parameters
741741

742742
elif self._doc.get("openapi") and \
743-
re.search(SwaggerEditor.get_openapi_version_3_regex(), self._doc["openapi"]) is not None:
744-
743+
SwaggerEditor.safe_compare_regex_with_string(
744+
SwaggerEditor.get_openapi_version_3_regex(), self._doc["openapi"]):
745745
method_definition['requestBody'] = {
746746
'content': {
747747
"application/json": {
@@ -901,8 +901,8 @@ def is_valid(data):
901901
if bool(data.get("swagger")):
902902
return True
903903
elif bool(data.get("openapi")):
904-
return re.search(SwaggerEditor.get_openapi_version_3_regex(), data["openapi"]) is not None
905-
return False
904+
return SwaggerEditor.safe_compare_regex_with_string(
905+
SwaggerEditor.get_openapi_version_3_regex(), data["openapi"])
906906
return False
907907

908908
@staticmethod
@@ -951,3 +951,7 @@ def get_openapi_versions_supported_regex():
951951
def get_openapi_version_3_regex():
952952
openapi_version_3_regex = r"\A3(\.\d)(\.\d)?$"
953953
return openapi_version_3_regex
954+
955+
@staticmethod
956+
def safe_compare_regex_with_string(regex, data):
957+
return re.match(regex, str(data)) is not None

samtranslator/translator/translator.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import copy
2-
32
from samtranslator.model import ResourceTypeResolver, sam_resources
43
from samtranslator.translator.verify_logical_id import verify_unique_logical_id
54
from samtranslator.model.preferences.deployment_preference_collection import DeploymentPreferenceCollection

tests/translator/input/api_request_model_openapi_3.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Resources:
3333
Type: AWS::Serverless::Api
3434
Properties:
3535
StageName: Prod
36-
OpenApiVersion: '3.0.1'
36+
OpenApiVersion: 3.0
3737
Models:
3838
User:
3939
type: object

tests/translator/input/api_with_open_api_version.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Globals:
22
Api:
3-
OpenApiVersion: '3.0.1'
3+
OpenApiVersion: 3.0.1
44
Cors: '*'
55

66
Resources:
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Resources:
2+
MyApi:
3+
Type: AWS::Serverless::Api
4+
Properties:
5+
DefinitionBody: {}
6+
OpenApiVersion: 3
7+
StageName: 'prod'
8+
Function:
9+
Type: AWS::Serverless::Function
10+
Properties:
11+
Handler: lambda.handler
12+
CodeUri: s3://bucket/api
13+
Runtime: nodejs8.10
14+
Events:
15+
ProxyApiRoot:
16+
Type: Api
17+
Properties:
18+
RestApiId:
19+
Ref: MyApi
20+
Path: "/"
21+
Method: ANY

tests/translator/output/api_request_model_openapi_3.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@
2121
}
2222
}
2323
},
24-
"HtmlApiDeploymentefb667b26e": {
25-
"Type": "AWS::ApiGateway::Deployment",
26-
"Properties": {
27-
"RestApiId": {
28-
"Ref": "HtmlApi"
29-
},
30-
"Description": "RestApi deployment id: efb667b26e8a0b0f733f5dfc27d039c1a2867db0"
31-
}
32-
},
3324
"HtmlFunctionRole": {
3425
"Type": "AWS::IAM::Role",
3526
"Properties": {
@@ -58,7 +49,7 @@
5849
"Type": "AWS::ApiGateway::Stage",
5950
"Properties": {
6051
"DeploymentId": {
61-
"Ref": "HtmlApiDeploymentefb667b26e"
52+
"Ref": "HtmlApiDeployment59eeb787ee"
6253
},
6354
"RestApiId": {
6455
"Ref": "HtmlApi"
@@ -149,7 +140,7 @@
149140
}
150141
}
151142
},
152-
"openapi": "3.0.1",
143+
"openapi": "3.0",
153144
"components": {
154145
"securitySchemes": {
155146
"AWS_IAM": {
@@ -173,6 +164,15 @@
173164
}
174165
}
175166
},
167+
"HtmlApiDeployment59eeb787ee": {
168+
"Type": "AWS::ApiGateway::Deployment",
169+
"Properties": {
170+
"RestApiId": {
171+
"Ref": "HtmlApi"
172+
},
173+
"Description": "RestApi deployment id: 59eeb787ee1561329a07e10162ac3718998e9f91"
174+
}
175+
},
176176
"HtmlFunctionGetHtmlPermissionTest": {
177177
"Type": "AWS::Lambda::Permission",
178178
"Properties": {

tests/translator/output/aws-cn/api_request_model_openapi_3.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
{
22
"Resources": {
3+
"HtmlApiDeployment5ae7e42cea": {
4+
"Type": "AWS::ApiGateway::Deployment",
5+
"Properties": {
6+
"RestApiId": {
7+
"Ref": "HtmlApi"
8+
},
9+
"Description": "RestApi deployment id: 5ae7e42cea0640a3318bf508535850cd792a52dc"
10+
}
11+
},
312
"HtmlFunctionIamPermissionTest": {
413
"Type": "AWS::Lambda::Permission",
514
"Properties": {
@@ -21,15 +30,6 @@
2130
}
2231
}
2332
},
24-
"HtmlApiDeploymentbe02cbff83": {
25-
"Type": "AWS::ApiGateway::Deployment",
26-
"Properties": {
27-
"RestApiId": {
28-
"Ref": "HtmlApi"
29-
},
30-
"Description": "RestApi deployment id: be02cbff831c2acb5672650bc54b204366bef429"
31-
}
32-
},
3333
"HtmlFunctionRole": {
3434
"Type": "AWS::IAM::Role",
3535
"Properties": {
@@ -58,7 +58,7 @@
5858
"Type": "AWS::ApiGateway::Stage",
5959
"Properties": {
6060
"DeploymentId": {
61-
"Ref": "HtmlApiDeploymentbe02cbff83"
61+
"Ref": "HtmlApiDeployment5ae7e42cea"
6262
},
6363
"RestApiId": {
6464
"Ref": "HtmlApi"
@@ -149,7 +149,7 @@
149149
}
150150
}
151151
},
152-
"openapi": "3.0.1",
152+
"openapi": "3.0",
153153
"components": {
154154
"securitySchemes": {
155155
"AWS_IAM": {

tests/translator/output/aws-us-gov/api_request_model_openapi_3.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@
2121
}
2222
}
2323
},
24-
"HtmlApiDeploymente91bc94e87": {
25-
"Type": "AWS::ApiGateway::Deployment",
26-
"Properties": {
27-
"RestApiId": {
28-
"Ref": "HtmlApi"
29-
},
30-
"Description": "RestApi deployment id: e91bc94e874a30084312552d628dd248890ad7f9"
31-
}
32-
},
3324
"HtmlFunctionRole": {
3425
"Type": "AWS::IAM::Role",
3526
"Properties": {
@@ -58,7 +49,7 @@
5849
"Type": "AWS::ApiGateway::Stage",
5950
"Properties": {
6051
"DeploymentId": {
61-
"Ref": "HtmlApiDeploymente91bc94e87"
52+
"Ref": "HtmlApiDeploymenta488cfa4f9"
6253
},
6354
"RestApiId": {
6455
"Ref": "HtmlApi"
@@ -149,7 +140,7 @@
149140
}
150141
}
151142
},
152-
"openapi": "3.0.1",
143+
"openapi": "3.0",
153144
"components": {
154145
"securitySchemes": {
155146
"AWS_IAM": {
@@ -181,6 +172,15 @@
181172
}
182173
}
183174
},
175+
"HtmlApiDeploymenta488cfa4f9": {
176+
"Type": "AWS::ApiGateway::Deployment",
177+
"Properties": {
178+
"RestApiId": {
179+
"Ref": "HtmlApi"
180+
},
181+
"Description": "RestApi deployment id: a488cfa4f9c73604187a3a5dfa5f333ef2c52e1e"
182+
}
183+
},
184184
"HtmlFunctionGetHtmlPermissionTest": {
185185
"Type": "AWS::Lambda::Permission",
186186
"Properties": {

0 commit comments

Comments
 (0)