diff --git a/samtranslator/model/api/api_generator.py b/samtranslator/model/api/api_generator.py index 7fd0812d9..c46a1b030 100644 --- a/samtranslator/model/api/api_generator.py +++ b/samtranslator/model/api/api_generator.py @@ -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: @@ -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"): + 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).\ + 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): diff --git a/tests/translator/output/api_with_cors_openapi_3.json b/tests/translator/output/api_with_cors_openapi_3.json index 1311ee899..74c0bd65f 100644 --- a/tests/translator/output/api_with_cors_openapi_3.json +++ b/tests/translator/output/api_with_cors_openapi_3.json @@ -116,10 +116,10 @@ } } } - }, + }, "consumes": [ "application/json" - ], + ], "summary": "CORS support", "responses": { "200": { @@ -139,7 +139,7 @@ }, "description": "Default response for CORS method" } - }, + }, "produces": [ "application/json" ] @@ -166,10 +166,10 @@ } } } - }, + }, "consumes": [ "application/json" - ], + ], "summary": "CORS support", "responses": { "200": { @@ -189,7 +189,7 @@ }, "description": "Default response for CORS method" } - }, + }, "produces": [ "application/json" ] @@ -244,7 +244,7 @@ "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { - "Ref": "ServerlessRestApiDeploymentf0997bcd64" + "Ref": "ServerlessRestApiDeploymente3fd36e910" }, "RestApiId": { "Ref": "ServerlessRestApi" @@ -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": { @@ -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": { @@ -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": { diff --git a/tests/translator/output/api_with_open_api_version.json b/tests/translator/output/api_with_open_api_version.json index 4d1df1631..088d0c5f6 100644 --- a/tests/translator/output/api_with_open_api_version.json +++ b/tests/translator/output/api_with_open_api_version.json @@ -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": { @@ -111,7 +111,7 @@ "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { - "Ref": "ServerlessRestApiDeployment83a174fb31" + "Ref": "ServerlessRestApiDeployment1471b71104" }, "RestApiId": { "Ref": "ServerlessRestApi" @@ -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": { diff --git a/tests/translator/output/aws-cn/api_with_cors_openapi_3.json b/tests/translator/output/aws-cn/api_with_cors_openapi_3.json index c7567ce1c..f23c1b3d2 100644 --- a/tests/translator/output/aws-cn/api_with_cors_openapi_3.json +++ b/tests/translator/output/aws-cn/api_with_cors_openapi_3.json @@ -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": { @@ -261,7 +261,7 @@ "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { - "Ref": "ServerlessRestApiDeployment5a1885dc4a" + "Ref": "ServerlessRestApiDeployment147347629d" }, "RestApiId": { "Ref": "ServerlessRestApi" @@ -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": { @@ -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": { diff --git a/tests/translator/output/aws-cn/api_with_open_api_version.json b/tests/translator/output/aws-cn/api_with_open_api_version.json index f0545c7fc..3b6e9057a 100644 --- a/tests/translator/output/aws-cn/api_with_open_api_version.json +++ b/tests/translator/output/aws-cn/api_with_open_api_version.json @@ -36,13 +36,13 @@ } ], "AssumeRolePolicyDocument": { - "Version": "2012-10-17", + "Version": "2012-10-17", "Statement": [ { "Action": [ "sts:AssumeRole" - ], - "Effect": "Allow", + ], + "Effect": "Allow", "Principal": { "Service": [ "lambda.amazonaws.com" @@ -54,16 +54,16 @@ } }, "ImplicitApiFunctionGetHtmlPermissionProd": { - "Type": "AWS::Lambda::Permission", + "Type": "AWS::Lambda::Permission", "Properties": { - "Action": "lambda:invokeFunction", - "Principal": "apigateway.amazonaws.com", + "Action": "lambda:invokeFunction", + "Principal": "apigateway.amazonaws.com", "FunctionName": { "Ref": "ImplicitApiFunction" - }, + }, "SourceArn": { "Fn::Sub": [ - "arn:aws-cn:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/", + "arn:aws-cn:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/", { "__Stage__": "*", "__ApiId__": { @@ -73,146 +73,144 @@ ] } } - }, + }, "ExplicitApi": { - "Type": "AWS::ApiGateway::RestApi", + "Type": "AWS::ApiGateway::RestApi", "Properties": { "Body": { "info": { - "version": "1.0", + "version": "1.0", "title": { "Ref": "AWS::StackName" } - }, - "paths": {}, + }, + "paths": {}, "openapi": "3.0.1" - }, + }, "EndpointConfiguration": { "Types": [ "REGIONAL" ] - }, + }, "Parameters": { "endpointConfigurationTypes": "REGIONAL" } } - }, + }, "ExplicitApiDeploymentd9a0f2ae4f": { - "Type": "AWS::ApiGateway::Deployment", + "Type": "AWS::ApiGateway::Deployment", "Properties": { "RestApiId": { "Ref": "ExplicitApi" - }, + }, "Description": "RestApi deployment id: d9a0f2ae4fe2d97b9b91644934a878b6a08cf1c3" } - }, + }, "ServerlessRestApiProdStage": { - "Type": "AWS::ApiGateway::Stage", + "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { - "Ref": "ServerlessRestApiDeploymentb51bf3171c" - }, + "Ref": "ServerlessRestApiDeployment75f93f5c08" + }, "RestApiId": { "Ref": "ServerlessRestApi" - }, + }, "StageName": "Prod" } - }, + }, "ExplicitApiProdStage": { - "Type": "AWS::ApiGateway::Stage", + "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { "Ref": "ExplicitApiDeploymentd9a0f2ae4f" - }, + }, "RestApiId": { "Ref": "ExplicitApi" - }, + }, "StageName": "Prod" } - }, + }, "ServerlessRestApi": { - "Type": "AWS::ApiGateway::RestApi", + "Type": "AWS::ApiGateway::RestApi", "Properties": { "Body": { "info": { - "version": "1.0", + "version": "1.0", "title": { "Ref": "AWS::StackName" } - }, + }, "paths": { "/": { "options": { "x-amazon-apigateway-integration": { - "type": "mock", + "type": "mock", "requestTemplates": { "application/json": "{\n \"statusCode\" : 200\n}\n" - }, + }, "responses": { "default": { - "statusCode": "200", + "statusCode": "200", "responseTemplates": { "application/json": "{}\n" - }, + }, "responseParameters": { - "method.response.header.Access-Control-Allow-Origin": "*", + "method.response.header.Access-Control-Allow-Origin": "*", "method.response.header.Access-Control-Allow-Methods": "'GET,OPTIONS'" } } } - }, - "consumes": [ - "application/json" - ], - "summary": "CORS support", + }, + "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": { - "httpMethod": "POST", - "type": "aws_proxy", + "httpMethod": "POST", + "type": "aws_proxy", "uri": { "Fn::Sub": "arn:aws-cn:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ImplicitApiFunction.Arn}/invocations" } - }, + }, "responses": {} } } - }, + }, "openapi": "3.0.1" - }, + }, "EndpointConfiguration": { "Types": [ "REGIONAL" ] - }, + }, "Parameters": { "endpointConfigurationTypes": "REGIONAL" } } - }, - "ServerlessRestApiDeploymentb51bf3171c": { - "Type": "AWS::ApiGateway::Deployment", + }, + "ServerlessRestApiDeployment75f93f5c08": { + "Type": "AWS::ApiGateway::Deployment", "Properties": { "RestApiId": { "Ref": "ServerlessRestApi" - }, - "Description": "RestApi deployment id: b51bf3171cc3cd5bdc024b45dda8a6ec14dcd6ef" + }, + "Description": "RestApi deployment id: 75f93f5c08b6bd3326e74e6b22854de32481e155" } } } diff --git a/tests/translator/output/aws-us-gov/api_with_cors_openapi_3.json b/tests/translator/output/aws-us-gov/api_with_cors_openapi_3.json index 98328269b..5ecfd955b 100644 --- a/tests/translator/output/aws-us-gov/api_with_cors_openapi_3.json +++ b/tests/translator/output/aws-us-gov/api_with_cors_openapi_3.json @@ -252,7 +252,7 @@ "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { - "Ref": "ServerlessRestApiDeployment5e7d1b38bf" + "Ref": "ServerlessRestApiDeployment8493e7adfe" }, "RestApiId": { "Ref": "ServerlessRestApi" @@ -355,13 +355,13 @@ ] } }, - "ServerlessRestApiDeployment5e7d1b38bf": { + "ServerlessRestApiDeployment8493e7adfe": { "Type": "AWS::ApiGateway::Deployment", "Properties": { "RestApiId": { "Ref": "ServerlessRestApi" }, - "Description": "RestApi deployment id: 5e7d1b38bf567c46a1fc95b97a22d3cbf171216c" + "Description": "RestApi deployment id: 8493e7adfe1ae5a1112328c0633eea5cdce0633c" } }, "ServerlessRestApi": { @@ -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": { @@ -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": { diff --git a/tests/translator/output/aws-us-gov/api_with_open_api_version.json b/tests/translator/output/aws-us-gov/api_with_open_api_version.json index b6fd6e628..4f1d4afb2 100644 --- a/tests/translator/output/aws-us-gov/api_with_open_api_version.json +++ b/tests/translator/output/aws-us-gov/api_with_open_api_version.json @@ -110,7 +110,7 @@ "Type": "AWS::ApiGateway::Stage", "Properties": { "DeploymentId": { - "Ref": "ServerlessRestApiDeployment7af82eda48" + "Ref": "ServerlessRestApiDeploymenta4ffcdf019" }, "RestApiId": { "Ref": "ServerlessRestApi" @@ -118,13 +118,13 @@ "StageName": "Prod" } }, - "ServerlessRestApiDeployment7af82eda48": { + "ServerlessRestApiDeploymenta4ffcdf019": { "Type": "AWS::ApiGateway::Deployment", "Properties": { "RestApiId": { "Ref": "ServerlessRestApi" }, - "Description": "RestApi deployment id: 7af82eda48a23d84634abd7aa0865898d3b17810" + "Description": "RestApi deployment id: a4ffcdf0196ece94bc157dc06c21dfa3c2df13a1" } }, "ServerlessRestApi": { @@ -157,27 +157,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": {