Skip to content

Commit 0f54bd2

Browse files
authored
fix(request): build request body by operation payload field (#126)
This is a follow-up fix for #120 . The PR modifies the request build again with the following changes: when the payload field is specified inside the input schema, the request body will be built based on the parameter that specified by the payload field. This behavior imitates the behavior defined in aws-sdk-js: https://github.com/aws/aws-sdk-js/blob/02153340ce052614c5437fe46ab1f49bfd8483f7/lib/protocol/rest_json.js#L24-L41 as well as https://github.com/aws/aws-sdk-js/blob/02153340ce052614c5437fe46ab1f49bfd8483f7/lib/protocol/rest_xml.js#L10-L26 FTI-6138
1 parent e5b38f7 commit 0f54bd2

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ Release process:
177177
1. test installing the rock from LuaRocks
178178

179179

180+
### Unreleased
181+
182+
- fix: build the request body based on payload field
183+
[126](https://github.com/Kong/lua-resty-aws/pull/126)
184+
185+
180186
### 1.5.2 (29-Jul-2024)
181187

182188
- fix: fix sts regional endpoint injection under several cases

spec/02-requests/02-build_request_spec.lua

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ describe("operations protocol", function()
55

66

77
local build_request
8-
local operation
9-
local config
10-
local params
8+
local operation, operation_with_payload_field
9+
local config, config_with_payload
10+
local params, params_with_payload
1111
local snapshot
1212
local binary_data
1313

@@ -105,6 +105,37 @@ describe("operations protocol", function()
105105
}
106106
}
107107

108+
operation_with_payload_field = {
109+
name = "PutObject",
110+
http = {
111+
method = "PUT",
112+
requestUri = "/{Bucket}/{Key+}"
113+
},
114+
input = {
115+
type = "structure",
116+
required = {
117+
"Bucket",
118+
"Key"
119+
},
120+
members = {
121+
Bucket = {
122+
type = "string",
123+
location = "uri",
124+
locationName = "Bucket"
125+
},
126+
Key = {
127+
type = "string",
128+
location = "uri",
129+
locationName = "Key"
130+
},
131+
Body = {
132+
type = "blob",
133+
},
134+
},
135+
payload = "Body"
136+
},
137+
}
138+
108139
config = {
109140
apiVersion = "2011-06-15",
110141
--endpointPrefix = "sts",
@@ -119,6 +150,19 @@ describe("operations protocol", function()
119150
xmlNamespace = "https://sts.amazonaws.com/doc/2011-06-15/"
120151
}
121152

153+
config_with_payload = {
154+
apiVersion = "2006-03-01",
155+
signingName = "s3",
156+
globalEndpoint = "s3.amazonaws.com",
157+
--protocol = "query",
158+
serviceAbbreviation = "AWS S3",
159+
serviceFullName = "AWS Object Storage",
160+
serviceId = "S3",
161+
signatureVersion = "v4",
162+
uid = "s3-2006-03-01",
163+
xmlNamespace = "https://s3.amazonaws.com/doc/2006-03-01/"
164+
}
165+
122166
params = {
123167
RoleArn = "hello",
124168
RoleSessionName = "world",
@@ -134,6 +178,12 @@ describe("operations protocol", function()
134178
BinaryData = binary_data,
135179
}
136180

181+
params_with_payload = {
182+
Bucket = "hello",
183+
Key = "world",
184+
Body = binary_data,
185+
}
186+
137187
end)
138188

139189

@@ -256,6 +306,27 @@ describe("operations protocol", function()
256306
}, request)
257307
end)
258308

309+
it("json: querystring, uri, header and body params, with payload field", function()
310+
311+
config_with_payload.protocol = "json"
312+
313+
local request = build_request(operation_with_payload_field, config_with_payload, params_with_payload)
314+
315+
assert.same({
316+
headers = {
317+
["Content-Length"] = 4,
318+
["X-Amz-Target"] = "s3.PutObject",
319+
["Host"] = "s3.amazonaws.com",
320+
},
321+
method = 'PUT',
322+
path = '/hello/world',
323+
host = 's3.amazonaws.com',
324+
port = 443,
325+
body = binary_data,
326+
query = {},
327+
}, request)
328+
end)
329+
259330

260331
it("rest-xml: querystring, uri, header and body params", function()
261332

src/resty/aws/request/build.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,21 @@ local function build_request(operation, config, params)
205205
request.query[k] = v
206206
end
207207

208+
local payload_member = operation.input and operation.input.payload
209+
if payload_member and body_tbl[payload_member] then
210+
local member_config = operation.input.members[payload_member]
211+
if member_config.type == "structure" then
212+
body_tbl = type(body_tbl[payload_member]) == "table" and body_tbl[payload_member] or body_tbl
213+
214+
elseif body_tbl[payload_member] then
215+
request.body = body_tbl[payload_member]
216+
if member_config.type == "binary" and member_config.streaming == true then
217+
request.headers["Content-Type"] = "binary/octet-stream"
218+
end
219+
body_tbl[payload_member] = nil
220+
end
221+
end
222+
208223
local table_empty = not next(body_tbl)
209224
-- already has a raw body, or no body at all
210225
if request.body then

0 commit comments

Comments
 (0)