From 93d2b3d28d0262a568a150a86b5fdab06bb6664d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Tue, 21 Apr 2020 13:06:41 -0400 Subject: [PATCH] Mark optional API Gateway v2 HTTP Request fields with 'omitempty'. --- events/apigw.go | 32 ++++++++--------- events/apigw_test.go | 35 +++++++++++++++++++ .../apigw-v2-request-no-authorizer.json | 35 +++++++++++++++++++ 3 files changed, 86 insertions(+), 16 deletions(-) create mode 100644 events/testdata/apigw-v2-request-no-authorizer.json diff --git a/events/apigw.go b/events/apigw.go index 8db0d2e5..0fbc19df 100644 --- a/events/apigw.go +++ b/events/apigw.go @@ -48,29 +48,29 @@ type APIGatewayV2HTTPRequest struct { RouteKey string `json:"routeKey"` RawPath string `json:"rawPath"` RawQueryString string `json:"rawQueryString"` - Cookies []string `json:"cookies"` + Cookies []string `json:"cookies,omitempty"` Headers map[string]string `json:"headers"` - QueryStringParameters map[string]string `json:"queryStringParameters"` - PathParameters map[string]string `json:"pathParameters"` + QueryStringParameters map[string]string `json:"queryStringParameters,omitempty"` + PathParameters map[string]string `json:"pathParameters,omitempty"` RequestContext APIGatewayV2HTTPRequestContext `json:"requestContext"` - StageVariables map[string]string `json:"stageVariables"` - Body string `json:"body"` + StageVariables map[string]string `json:"stageVariables,omitempty"` + Body string `json:"body,omitempty"` IsBase64Encoded bool `json:"isBase64Encoded"` } // APIGatewayV2HTTPRequestContext contains the information to identify the AWS account and resources invoking the Lambda function. type APIGatewayV2HTTPRequestContext struct { - RouteKey string `json:"routeKey"` - AccountID string `json:"accountId"` - Stage string `json:"stage"` - RequestID string `json:"requestId"` - Authorizer APIGatewayV2HTTPRequestContextAuthorizerDescription `json:"authorizer"` - APIID string `json:"apiId"` // The API Gateway HTTP API Id - DomainName string `json:"domainName"` - DomainPrefix string `json:"domainPrefix"` - Time string `json:"time"` - TimeEpoch int64 `json:"timeEpoch"` - HTTP APIGatewayV2HTTPRequestContextHTTPDescription `json:"http"` + RouteKey string `json:"routeKey"` + AccountID string `json:"accountId"` + Stage string `json:"stage"` + RequestID string `json:"requestId"` + Authorizer *APIGatewayV2HTTPRequestContextAuthorizerDescription `json:"authorizer,omitempty"` + APIID string `json:"apiId"` // The API Gateway HTTP API Id + DomainName string `json:"domainName"` + DomainPrefix string `json:"domainPrefix"` + Time string `json:"time"` + TimeEpoch int64 `json:"timeEpoch"` + HTTP APIGatewayV2HTTPRequestContextHTTPDescription `json:"http"` } // APIGatewayV2HTTPRequestContextAuthorizerDescription contains authorizer information for the request context. diff --git a/events/apigw_test.go b/events/apigw_test.go index 0e2b0c0b..913306c3 100644 --- a/events/apigw_test.go +++ b/events/apigw_test.go @@ -244,3 +244,38 @@ func TestApiGatewayV2HTTPRequestMarshaling(t *testing.T) { assert.JSONEq(t, string(inputJSON), string(outputJSON)) } + +func TestApiGatewayV2HTTPRequestNoAuthorizerMarshaling(t *testing.T) { + + // read json from file + inputJSON, err := ioutil.ReadFile("./testdata/apigw-v2-request-no-authorizer.json") + if err != nil { + t.Errorf("could not open test file. details: %v", err) + } + + // de-serialize into Go object + var inputEvent APIGatewayV2HTTPRequest + if err := json.Unmarshal(inputJSON, &inputEvent); err != nil { + t.Errorf("could not unmarshal event. details: %v", err) + } + + // validate custom authorizer context + authContext := inputEvent.RequestContext.Authorizer + if authContext != nil { + t.Errorf("unexpected authorizer: %v", authContext) + } + + // validate HTTP details + http := inputEvent.RequestContext.HTTP + if http.Path != "/" { + t.Errorf("could not extract HTTP details: %v", http) + } + + // serialize to json + outputJSON, err := json.Marshal(inputEvent) + if err != nil { + t.Errorf("could not marshal event. details: %v", err) + } + + assert.JSONEq(t, string(inputJSON), string(outputJSON)) +} diff --git a/events/testdata/apigw-v2-request-no-authorizer.json b/events/testdata/apigw-v2-request-no-authorizer.json new file mode 100644 index 00000000..d21c1f3f --- /dev/null +++ b/events/testdata/apigw-v2-request-no-authorizer.json @@ -0,0 +1,35 @@ +{ + "version": "2.0", + "routeKey": "$default", + "rawPath": "/", + "rawQueryString": "", + "headers": { + "accept": "*/*", + "content-length": "0", + "host": "aaaaaaaaaa.execute-api.us-west-2.amazonaws.com", + "user-agent": "curl/7.58.0", + "x-amzn-trace-id": "Root=1-5e9f0c65-1de4d666d4dd26aced652b6c", + "x-forwarded-for": "1.2.3.4", + "x-forwarded-port": "443", + "x-forwarded-proto": "https" + }, + "requestContext": { + "accountId": "123456789012", + "apiId": "aaaaaaaaaa", + "domainName": "aaaaaaaaaa.execute-api.us-west-2.amazonaws.com", + "domainPrefix": "aaaaaaaaaa", + "http": { + "method": "GET", + "path": "/", + "protocol": "HTTP/1.1", + "sourceIp": "1.2.3.4", + "userAgent": "curl/7.58.0" + }, + "requestId": "LV7fzho-PHcEJPw=", + "routeKey": "$default", + "stage": "$default", + "time": "21/Apr/2020:15:08:21 +0000", + "timeEpoch": 1587481701067 + }, + "isBase64Encoded": false +}