Skip to content

Commit ba31528

Browse files
ewbankkitbmoffatt
andauthored
Mark optional API Gateway v2 HTTP Request fields with 'omitempty'. (#285)
Co-authored-by: Bryan Moffatt <bmoffatt@users.noreply.github.com>
1 parent 235b78e commit ba31528

File tree

3 files changed

+86
-16
lines changed

3 files changed

+86
-16
lines changed

events/apigw.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,29 @@ type APIGatewayV2HTTPRequest struct {
4848
RouteKey string `json:"routeKey"`
4949
RawPath string `json:"rawPath"`
5050
RawQueryString string `json:"rawQueryString"`
51-
Cookies []string `json:"cookies"`
51+
Cookies []string `json:"cookies,omitempty"`
5252
Headers map[string]string `json:"headers"`
53-
QueryStringParameters map[string]string `json:"queryStringParameters"`
54-
PathParameters map[string]string `json:"pathParameters"`
53+
QueryStringParameters map[string]string `json:"queryStringParameters,omitempty"`
54+
PathParameters map[string]string `json:"pathParameters,omitempty"`
5555
RequestContext APIGatewayV2HTTPRequestContext `json:"requestContext"`
56-
StageVariables map[string]string `json:"stageVariables"`
57-
Body string `json:"body"`
56+
StageVariables map[string]string `json:"stageVariables,omitempty"`
57+
Body string `json:"body,omitempty"`
5858
IsBase64Encoded bool `json:"isBase64Encoded"`
5959
}
6060

6161
// APIGatewayV2HTTPRequestContext contains the information to identify the AWS account and resources invoking the Lambda function.
6262
type APIGatewayV2HTTPRequestContext struct {
63-
RouteKey string `json:"routeKey"`
64-
AccountID string `json:"accountId"`
65-
Stage string `json:"stage"`
66-
RequestID string `json:"requestId"`
67-
Authorizer APIGatewayV2HTTPRequestContextAuthorizerDescription `json:"authorizer"`
68-
APIID string `json:"apiId"` // The API Gateway HTTP API Id
69-
DomainName string `json:"domainName"`
70-
DomainPrefix string `json:"domainPrefix"`
71-
Time string `json:"time"`
72-
TimeEpoch int64 `json:"timeEpoch"`
73-
HTTP APIGatewayV2HTTPRequestContextHTTPDescription `json:"http"`
63+
RouteKey string `json:"routeKey"`
64+
AccountID string `json:"accountId"`
65+
Stage string `json:"stage"`
66+
RequestID string `json:"requestId"`
67+
Authorizer *APIGatewayV2HTTPRequestContextAuthorizerDescription `json:"authorizer,omitempty"`
68+
APIID string `json:"apiId"` // The API Gateway HTTP API Id
69+
DomainName string `json:"domainName"`
70+
DomainPrefix string `json:"domainPrefix"`
71+
Time string `json:"time"`
72+
TimeEpoch int64 `json:"timeEpoch"`
73+
HTTP APIGatewayV2HTTPRequestContextHTTPDescription `json:"http"`
7474
}
7575

7676
// APIGatewayV2HTTPRequestContextAuthorizerDescription contains authorizer information for the request context.

events/apigw_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,38 @@ func TestApiGatewayV2HTTPRequestMarshaling(t *testing.T) {
244244

245245
assert.JSONEq(t, string(inputJSON), string(outputJSON))
246246
}
247+
248+
func TestApiGatewayV2HTTPRequestNoAuthorizerMarshaling(t *testing.T) {
249+
250+
// read json from file
251+
inputJSON, err := ioutil.ReadFile("./testdata/apigw-v2-request-no-authorizer.json")
252+
if err != nil {
253+
t.Errorf("could not open test file. details: %v", err)
254+
}
255+
256+
// de-serialize into Go object
257+
var inputEvent APIGatewayV2HTTPRequest
258+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
259+
t.Errorf("could not unmarshal event. details: %v", err)
260+
}
261+
262+
// validate custom authorizer context
263+
authContext := inputEvent.RequestContext.Authorizer
264+
if authContext != nil {
265+
t.Errorf("unexpected authorizer: %v", authContext)
266+
}
267+
268+
// validate HTTP details
269+
http := inputEvent.RequestContext.HTTP
270+
if http.Path != "/" {
271+
t.Errorf("could not extract HTTP details: %v", http)
272+
}
273+
274+
// serialize to json
275+
outputJSON, err := json.Marshal(inputEvent)
276+
if err != nil {
277+
t.Errorf("could not marshal event. details: %v", err)
278+
}
279+
280+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
281+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"version": "2.0",
3+
"routeKey": "$default",
4+
"rawPath": "/",
5+
"rawQueryString": "",
6+
"headers": {
7+
"accept": "*/*",
8+
"content-length": "0",
9+
"host": "aaaaaaaaaa.execute-api.us-west-2.amazonaws.com",
10+
"user-agent": "curl/7.58.0",
11+
"x-amzn-trace-id": "Root=1-5e9f0c65-1de4d666d4dd26aced652b6c",
12+
"x-forwarded-for": "1.2.3.4",
13+
"x-forwarded-port": "443",
14+
"x-forwarded-proto": "https"
15+
},
16+
"requestContext": {
17+
"accountId": "123456789012",
18+
"apiId": "aaaaaaaaaa",
19+
"domainName": "aaaaaaaaaa.execute-api.us-west-2.amazonaws.com",
20+
"domainPrefix": "aaaaaaaaaa",
21+
"http": {
22+
"method": "GET",
23+
"path": "/",
24+
"protocol": "HTTP/1.1",
25+
"sourceIp": "1.2.3.4",
26+
"userAgent": "curl/7.58.0"
27+
},
28+
"requestId": "LV7fzho-PHcEJPw=",
29+
"routeKey": "$default",
30+
"stage": "$default",
31+
"time": "21/Apr/2020:15:08:21 +0000",
32+
"timeEpoch": 1587481701067
33+
},
34+
"isBase64Encoded": false
35+
}

0 commit comments

Comments
 (0)