Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion samtranslator/model/api/api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def _openapi_postprocess(self, definition_body):
"""
Convert definitions to openapi 3 in definition body if OpenApiVersion flag is specified.

If the is swagger defined in the definition body, we treat it as a swagger spec and dod not
If the is swagger defined in the definition body, we treat it as a swagger spec and do not
make any openapi 3 changes to it
"""
if definition_body.get('swagger') is not None:
Expand All @@ -555,6 +555,28 @@ def _openapi_postprocess(self, definition_body):
components['schemas'] = definition_body['definitions']
definition_body['components'] = components
del definition_body['definitions']
# removes `consumes` and `produces` options for CORS in openapi3 and
# adds `schema` for the headers in responses for openapi3
if definition_body.get("paths"):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check for openapi3.x.x is already present in _openapi_postprocess so I haven't added that.
Updated the code with removing consumes and produces. Also, updated the code with schema for headers as per ApiGateway export output

for path in definition_body.get("paths"):
if definition_body.get("paths").get(path).get("options"):
definition_body_options = definition_body.get("paths").get(path).get("options").copy()
for field in definition_body_options.keys():
# remove unsupported produces and consumes in options for openapi3
if field in ["produces", "consumes"]:
del definition_body["paths"][path]["options"][field]
# add schema for the headers in options section for openapi3
if field in ["responses"]:
options_path = definition_body["paths"][path]["options"]
if options_path and options_path.get(field).get('200') and options_path.get(field).\
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this line to avoid throwing 5xx errors

get('200').get('headers'):
headers = definition_body["paths"][path]["options"][field]['200']['headers']
for header in headers.keys():
header_value = {"schema": definition_body["paths"][path]["options"][field]
['200']['headers'][header]}
definition_body["paths"][path]["options"][field]['200']['headers'][header] = \
header_value

return definition_body

def _get_authorizers(self, authorizers_config, default_authorizer=None):
Expand Down
58 changes: 27 additions & 31 deletions tests/translator/output/api_with_cors_openapi_3.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@
}
}
}
},
},
"consumes": [
"application/json"
],
],
"summary": "CORS support",
"responses": {
"200": {
Expand All @@ -139,7 +139,7 @@
},
"description": "Default response for CORS method"
}
},
},
"produces": [
"application/json"
]
Expand All @@ -166,10 +166,10 @@
}
}
}
},
},
"consumes": [
"application/json"
],
],
"summary": "CORS support",
"responses": {
"200": {
Expand All @@ -189,7 +189,7 @@
},
"description": "Default response for CORS method"
}
},
},
"produces": [
"application/json"
]
Expand Down Expand Up @@ -244,7 +244,7 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "ServerlessRestApiDeploymentf0997bcd64"
"Ref": "ServerlessRestApiDeploymente3fd36e910"
},
"RestApiId": {
"Ref": "ServerlessRestApi"
Expand Down Expand Up @@ -324,13 +324,13 @@
"Description": "RestApi deployment id: 8cc53ffaa59a19b69e73d5b422142c7396739b16"
}
},
"ServerlessRestApiDeploymentf0997bcd64": {
"ServerlessRestApiDeploymente3fd36e910": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "ServerlessRestApi"
},
"Description": "RestApi deployment id: f0997bcd645ceb1529586bc2b9faf855687b46ce"
"Description": "RestApi deployment id: e3fd36e910d56cf80b1b62823277d19f9b1f1a15"
}
},
"RestApiFunction": {
Expand Down Expand Up @@ -394,27 +394,25 @@
}
}
}
},
"consumes": [
"application/json"
],
},
"summary": "CORS support",
"responses": {
"200": {
"headers": {
"Access-Control-Allow-Origin": {
"type": "string"
"schema": {
"type": "string"
}
},
"Access-Control-Allow-Methods": {
"type": "string"
"schema": {
"type": "string"
}
}
},
"description": "Default response for CORS method"
}
},
"produces": [
"application/json"
]
}
},
"x-amazon-apigateway-any-method": {
"x-amazon-apigateway-integration": {
Expand Down Expand Up @@ -454,27 +452,25 @@
}
}
}
},
"consumes": [
"application/json"
],
},
"summary": "CORS support",
"responses": {
"200": {
"headers": {
"Access-Control-Allow-Origin": {
"type": "string"
},
"schema": {
"type": "string"
}
},
"Access-Control-Allow-Methods": {
"type": "string"
"schema": {
"type": "string"
}
}
},
},
"description": "Default response for CORS method"
}
},
"produces": [
"application/json"
]
}
},
"get": {
"x-amazon-apigateway-integration": {
Expand Down
28 changes: 13 additions & 15 deletions tests/translator/output/api_with_open_api_version.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@
}
}
},
"ServerlessRestApiDeployment83a174fb31": {
"ServerlessRestApiDeployment1471b71104": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "ServerlessRestApi"
},
"Description": "RestApi deployment id: 83a174fb31472bc23ccc2f0d96eebff659e59cd2"
"Description": "RestApi deployment id: 1471b711047af5f4fd8c5c957e2b2075ddcc76be"
}
},
"ImplicitApiFunctionGetHtmlPermissionProd": {
Expand Down Expand Up @@ -111,7 +111,7 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "ServerlessRestApiDeployment83a174fb31"
"Ref": "ServerlessRestApiDeployment1471b71104"
},
"RestApiId": {
"Ref": "ServerlessRestApi"
Expand Down Expand Up @@ -161,27 +161,25 @@
}
}
}
},
"consumes": [
"application/json"
],
},
"summary": "CORS support",
"responses": {
"200": {
"headers": {
"Access-Control-Allow-Origin": {
"type": "string"
},
"schema": {
"type": "string"
}
},
"Access-Control-Allow-Methods": {
"type": "string"
"schema": {
"type": "string"
}
}
},
},
"description": "Default response for CORS method"
}
},
"produces": [
"application/json"
]
}
},
"get": {
"x-amazon-apigateway-integration": {
Expand Down
42 changes: 19 additions & 23 deletions tests/translator/output/aws-cn/api_with_cors_openapi_3.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@
}
}
},
"ServerlessRestApiDeployment5a1885dc4a": {
"ServerlessRestApiDeployment147347629d": {
"Type": "AWS::ApiGateway::Deployment",
"Properties": {
"RestApiId": {
"Ref": "ServerlessRestApi"
},
"Description": "RestApi deployment id: 5a1885dc4a36fdbd8b638b37b7bd6fbd1636e07e"
"Description": "RestApi deployment id: 147347629dc3d0f3815602132def464c995d7245"
}
},
"ImplicitApiFunctionAnyApiPermissionProd": {
Expand Down Expand Up @@ -261,7 +261,7 @@
"Type": "AWS::ApiGateway::Stage",
"Properties": {
"DeploymentId": {
"Ref": "ServerlessRestApiDeployment5a1885dc4a"
"Ref": "ServerlessRestApiDeployment147347629d"
},
"RestApiId": {
"Ref": "ServerlessRestApi"
Expand Down Expand Up @@ -402,27 +402,25 @@
}
}
}
},
"consumes": [
"application/json"
],
},
"summary": "CORS support",
"responses": {
"200": {
"headers": {
"Access-Control-Allow-Origin": {
"type": "string"
"schema": {
"type": "string"
}
},
"Access-Control-Allow-Methods": {
"type": "string"
"schema": {
"type": "string"
}
}
},
"description": "Default response for CORS method"
}
},
"produces": [
"application/json"
]
}
},
"x-amazon-apigateway-any-method": {
"x-amazon-apigateway-integration": {
Expand Down Expand Up @@ -462,27 +460,25 @@
}
}
}
},
"consumes": [
"application/json"
],
},
"summary": "CORS support",
"responses": {
"200": {
"headers": {
"Access-Control-Allow-Origin": {
"type": "string"
"schema": {
"type": "string"
}
},
"Access-Control-Allow-Methods": {
"type": "string"
"schema": {
"type": "string"
}
}
},
"description": "Default response for CORS method"
}
},
"produces": [
"application/json"
]
}
},
"get": {
"x-amazon-apigateway-integration": {
Expand Down
Loading