Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add payloadformatversion to httpapi events #1517

Merged
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
5 changes: 5 additions & 0 deletions samtranslator/model/eventsources/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,7 @@ class HttpApi(PushEventSource):
"Auth": PropertyType(False, is_type(dict)),
"TimeoutInMillis": PropertyType(False, is_type(int)),
"RouteSettings": PropertyType(False, is_type(dict)),
"PayloadFormatVersion": PropertyType(False, is_str()),
}

def resources_to_link(self, resources):
Expand Down Expand Up @@ -1076,6 +1077,10 @@ def _add_openapi_integration(self, api, function, manage_swagger=False):
api=api, path=self.Path, method_name=self.Method, path_parameters=path_parameters
)

if self.PayloadFormatVersion:
editor.add_payload_format_version_to_method(
api=api, path=self.Path, method_name=self.Method, payload_format_version=self.PayloadFormatVersion
)
api["DefinitionBody"] = editor.openapi

def _add_auth_to_openapi_integration(self, api, editor):
Expand Down
16 changes: 15 additions & 1 deletion samtranslator/open_api/open_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def add_lambda_integration(
path_dict[method][self._X_APIGW_INTEGRATION] = {
"type": "aws_proxy",
"httpMethod": "POST",
"payloadFormatVersion": "1.0",
"payloadFormatVersion": "2.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean we default to 2.0 payloadFormatVersion in SAM?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahh.. yes, as stated in the docs below.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, the default value is "2.0" in SAM

"uri": integration_uri,
}

Expand Down Expand Up @@ -289,6 +289,20 @@ def add_path_parameters_to_method(self, api, path, method_name, path_parameters)
parameter = {"name": param, "in": "path", "required": True}
method_definition.get("parameters").append(parameter)

def add_payload_format_version_to_method(self, api, path, method_name, payload_format_version="2.0"):
"""
Adds a payload format version to this path/method.

:param dict api: Reference to the related Api's properties as defined in the template.
:param string path: Path name
:param string method_name: Method name
:param string payload_format_version: payload format version sent to the integration
"""
normalized_method_name = self._normalize_method_name(method_name)
for method_definition in self.get_method_contents(self.get_path(path)[normalized_method_name]):
if self.method_definition_has_integration(method_definition):
method_definition[self._X_APIGW_INTEGRATION]["payloadFormatVersion"] = payload_format_version

def add_authorizers_security_definitions(self, authorizers):
"""
Add Authorizer definitions to the securityDefinitions part of Swagger.
Expand Down
6 changes: 3 additions & 3 deletions tests/openapi/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def test_must_add_new_integration_to_new_path(self):
_X_INTEGRATION: {
"type": "aws_proxy",
"httpMethod": "POST",
"payloadFormatVersion": "1.0",
"payloadFormatVersion": "2.0",
"uri": integration_uri,
},
}
Expand All @@ -241,7 +241,7 @@ def test_must_add_new_integration_with_conditions_to_new_path(self):
_X_INTEGRATION: {
"type": "aws_proxy",
"httpMethod": "POST",
"payloadFormatVersion": "1.0",
"payloadFormatVersion": "2.0",
"uri": {"Fn::If": ["condition", integration_uri, {"Ref": "AWS::NoValue"}]},
},
},
Expand All @@ -268,7 +268,7 @@ def test_must_add_new_integration_to_existing_path(self):
_X_INTEGRATION: {
"type": "aws_proxy",
"httpMethod": "POST",
"payloadFormatVersion": "1.0",
"payloadFormatVersion": "2.0",
"uri": integration_uri,
},
}
Expand Down
1 change: 1 addition & 0 deletions tests/translator/input/http_api_existing_openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Resources:
Properties:
ApiId: !Ref MyApi
TimeoutInMillis: !Ref Timeout
PayloadFormatVersion: '1.0'
PathParametersExisting:
Type: HttpApi
Properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Resources:
Basic: # integration exists
Type: HttpApi
Properties:
PayloadFormatVersion: "2.0"
Path: /basic
Method: post
ApiId: !Ref MyApi
Expand Down
6 changes: 6 additions & 0 deletions tests/translator/input/http_api_explicit_stage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ Parameters:
CorsParam:
Type: String
Default: True
PayloadFormatVersion:
Default: '1.0'
Type: String

Globals:
HttpApi:
RouteSettings:
Expand All @@ -24,6 +28,8 @@ Resources:
RouteSettings:
ThrottlingBurstLimit: 300
LoggingLevel: INFO
PayloadFormatVersion: !Ref PayloadFormatVersion

MyApi:
Type: AWS::Serverless::HttpApi
Properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"responses": {}
}
Expand All @@ -199,7 +199,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"isDefaultRoute": true,
"responses": {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
}
]
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"responses": {}
},
Expand Down Expand Up @@ -187,7 +187,7 @@
}
]
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"responses": {}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"responses": {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"responses": {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"responses": {}
}
Expand All @@ -199,7 +199,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"isDefaultRoute": true,
"responses": {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
}
]
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"responses": {}
},
Expand Down Expand Up @@ -271,7 +271,7 @@
}
]
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"responses": {}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"responses": {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${MyFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"responses": {}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/translator/output/aws-cn/explicit_http_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"security": [
{
Expand Down Expand Up @@ -213,7 +213,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"isDefaultRoute": true,
"security": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${Function.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"isDefaultRoute": true,
"responses": {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"responses": {},
"parameters": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${DifferentFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"security": [
{
Expand Down Expand Up @@ -198,7 +198,7 @@
}
]
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"responses": {}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"CorsParam": {
"Default": true,
"Type": "String"
},
"PayloadFormatVersion": {
"Default": "1.0",
"Type": "String"
}
},
"Resources": {
Expand Down Expand Up @@ -128,7 +132,9 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": {
"Ref": "PayloadFormatVersion"
}
},
"isDefaultRoute": true,
"responses": {}
Expand Down
4 changes: 2 additions & 2 deletions tests/translator/output/aws-cn/http_api_with_cors.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"isDefaultRoute": true,
"responses": {}
Expand Down Expand Up @@ -203,7 +203,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"isDefaultRoute": true,
"responses": {}
Expand Down
8 changes: 4 additions & 4 deletions tests/translator/output/aws-cn/implicit_http_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction2.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"responses": {}
}
Expand All @@ -174,7 +174,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction2.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"responses": {},
"parameters": [
Expand All @@ -199,7 +199,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"isDefaultRoute": true,
"responses": {}
Expand All @@ -213,7 +213,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HttpApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"responses": {}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${RestApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"security": [
{
Expand All @@ -127,7 +127,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${RestApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"security": [
{
Expand All @@ -147,7 +147,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${RestApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"isDefaultRoute": true,
"security": [
Expand All @@ -168,7 +168,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${RestApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"security": [
{
Expand All @@ -186,7 +186,7 @@
"uri": {
"Fn::Sub": "arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${RestApiFunction.Arn}/invocations"
},
"payloadFormatVersion": "1.0"
"payloadFormatVersion": "2.0"
},
"security": [
{
Expand Down
Loading