Skip to content

Commit a4808ec

Browse files
authored
Fix empty map sent as array in Json requests (#873)
* Fix empty map sent as array * Fix Input initialization * Fix psalm * Add Changelog * Add tests
1 parent 32dda49 commit a4808ec

15 files changed

+337
-130
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## NOT RELEASED
44

5+
### Fixed
6+
7+
- Make sure required Map properties are validated before sending the request
8+
- Make sure empty Map properties are converted to `{}` in Json request.
9+
510
## 1.0.0
611

712
### Added

src/Input/BatchGetItemInput.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ final class BatchGetItemInput extends Input
3535
*/
3636
public function __construct(array $input = [])
3737
{
38-
$this->RequestItems = [];
39-
foreach ($input['RequestItems'] ?? [] as $key => $item) {
40-
$this->RequestItems[$key] = KeysAndAttributes::create($item);
38+
if (isset($input['RequestItems'])) {
39+
$this->RequestItems = [];
40+
foreach ($input['RequestItems'] as $key => $item) {
41+
$this->RequestItems[$key] = KeysAndAttributes::create($item);
42+
}
4143
}
4244
$this->ReturnConsumedCapacity = $input['ReturnConsumedCapacity'] ?? null;
4345
parent::__construct($input);
@@ -116,8 +118,13 @@ private function requestBody(): array
116118
throw new InvalidArgument(sprintf('Missing parameter "RequestItems" for "%s". The value cannot be null.', __CLASS__));
117119
}
118120

119-
foreach ($v as $name => $v) {
120-
$payload['RequestItems'][$name] = $v->requestBody();
121+
if (empty($v)) {
122+
$payload['RequestItems'] = new \stdClass();
123+
} else {
124+
$payload['RequestItems'] = [];
125+
foreach ($v as $name => $mv) {
126+
$payload['RequestItems'][$name] = $mv->requestBody();
127+
}
121128
}
122129
if (null !== $v = $this->ReturnConsumedCapacity) {
123130
if (!ReturnConsumedCapacity::exists($v)) {

src/Input/BatchWriteItemInput.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ final class BatchWriteItemInput extends Input
4646
*/
4747
public function __construct(array $input = [])
4848
{
49-
$this->RequestItems = [];
50-
foreach ($input['RequestItems'] ?? [] as $key => $item) {
51-
$this->RequestItems[$key] = array_map([WriteRequest::class, 'create'], $item);
49+
if (isset($input['RequestItems'])) {
50+
$this->RequestItems = [];
51+
foreach ($input['RequestItems'] ?? [] as $key => $item) {
52+
$this->RequestItems[$key] = array_map([WriteRequest::class, 'create'], $item);
53+
}
5254
}
5355
$this->ReturnConsumedCapacity = $input['ReturnConsumedCapacity'] ?? null;
5456
$this->ReturnItemCollectionMetrics = $input['ReturnItemCollectionMetrics'] ?? null;
@@ -146,12 +148,17 @@ private function requestBody(): array
146148
throw new InvalidArgument(sprintf('Missing parameter "RequestItems" for "%s". The value cannot be null.', __CLASS__));
147149
}
148150

149-
foreach ($v as $name => $v) {
150-
$index = -1;
151-
$payload['RequestItems'][$name] = [];
152-
foreach ($v as $listValue) {
153-
++$index;
154-
$payload['RequestItems'][$name][$index] = $listValue->requestBody();
151+
if (empty($v)) {
152+
$payload['RequestItems'] = new \stdClass();
153+
} else {
154+
$payload['RequestItems'] = [];
155+
foreach ($v as $name => $mv) {
156+
$index = -1;
157+
$payload['RequestItems'][$name] = [];
158+
foreach ($mv as $listValue) {
159+
++$index;
160+
$payload['RequestItems'][$name][$index] = $listValue->requestBody();
161+
}
155162
}
156163
}
157164
if (null !== $v = $this->ReturnConsumedCapacity) {

src/Input/DeleteItemInput.php

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,18 @@ public function __construct(array $input = [])
116116
{
117117
$this->TableName = $input['TableName'] ?? null;
118118

119-
$this->Key = [];
120-
foreach ($input['Key'] ?? [] as $key => $item) {
121-
$this->Key[$key] = AttributeValue::create($item);
119+
if (isset($input['Key'])) {
120+
$this->Key = [];
121+
foreach ($input['Key'] as $key => $item) {
122+
$this->Key[$key] = AttributeValue::create($item);
123+
}
122124
}
123125

124-
$this->Expected = [];
125-
foreach ($input['Expected'] ?? [] as $key => $item) {
126-
$this->Expected[$key] = ExpectedAttributeValue::create($item);
126+
if (isset($input['Expected'])) {
127+
$this->Expected = [];
128+
foreach ($input['Expected'] as $key => $item) {
129+
$this->Expected[$key] = ExpectedAttributeValue::create($item);
130+
}
127131
}
128132
$this->ConditionalOperator = $input['ConditionalOperator'] ?? null;
129133
$this->ReturnValues = $input['ReturnValues'] ?? null;
@@ -132,9 +136,11 @@ public function __construct(array $input = [])
132136
$this->ConditionExpression = $input['ConditionExpression'] ?? null;
133137
$this->ExpressionAttributeNames = $input['ExpressionAttributeNames'] ?? null;
134138

135-
$this->ExpressionAttributeValues = [];
136-
foreach ($input['ExpressionAttributeValues'] ?? [] as $key => $item) {
137-
$this->ExpressionAttributeValues[$key] = AttributeValue::create($item);
139+
if (isset($input['ExpressionAttributeValues'])) {
140+
$this->ExpressionAttributeValues = [];
141+
foreach ($input['ExpressionAttributeValues'] as $key => $item) {
142+
$this->ExpressionAttributeValues[$key] = AttributeValue::create($item);
143+
}
138144
}
139145
parent::__construct($input);
140146
}
@@ -348,12 +354,22 @@ private function requestBody(): array
348354
throw new InvalidArgument(sprintf('Missing parameter "Key" for "%s". The value cannot be null.', __CLASS__));
349355
}
350356

351-
foreach ($v as $name => $v) {
352-
$payload['Key'][$name] = $v->requestBody();
357+
if (empty($v)) {
358+
$payload['Key'] = new \stdClass();
359+
} else {
360+
$payload['Key'] = [];
361+
foreach ($v as $name => $mv) {
362+
$payload['Key'][$name] = $mv->requestBody();
363+
}
353364
}
354365
if (null !== $v = $this->Expected) {
355-
foreach ($v as $name => $v) {
356-
$payload['Expected'][$name] = $v->requestBody();
366+
if (empty($v)) {
367+
$payload['Expected'] = new \stdClass();
368+
} else {
369+
$payload['Expected'] = [];
370+
foreach ($v as $name => $mv) {
371+
$payload['Expected'][$name] = $mv->requestBody();
372+
}
357373
}
358374
}
359375
if (null !== $v = $this->ConditionalOperator) {
@@ -384,13 +400,23 @@ private function requestBody(): array
384400
$payload['ConditionExpression'] = $v;
385401
}
386402
if (null !== $v = $this->ExpressionAttributeNames) {
387-
foreach ($v as $name => $v) {
388-
$payload['ExpressionAttributeNames'][$name] = $v;
403+
if (empty($v)) {
404+
$payload['ExpressionAttributeNames'] = new \stdClass();
405+
} else {
406+
$payload['ExpressionAttributeNames'] = [];
407+
foreach ($v as $name => $mv) {
408+
$payload['ExpressionAttributeNames'][$name] = $mv;
409+
}
389410
}
390411
}
391412
if (null !== $v = $this->ExpressionAttributeValues) {
392-
foreach ($v as $name => $v) {
393-
$payload['ExpressionAttributeValues'][$name] = $v->requestBody();
413+
if (empty($v)) {
414+
$payload['ExpressionAttributeValues'] = new \stdClass();
415+
} else {
416+
$payload['ExpressionAttributeValues'] = [];
417+
foreach ($v as $name => $mv) {
418+
$payload['ExpressionAttributeValues'][$name] = $mv->requestBody();
419+
}
394420
}
395421
}
396422

src/Input/GetItemInput.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ public function __construct(array $input = [])
8484
{
8585
$this->TableName = $input['TableName'] ?? null;
8686

87-
$this->Key = [];
88-
foreach ($input['Key'] ?? [] as $key => $item) {
89-
$this->Key[$key] = AttributeValue::create($item);
87+
if (isset($input['Key'])) {
88+
$this->Key = [];
89+
foreach ($input['Key'] as $key => $item) {
90+
$this->Key[$key] = AttributeValue::create($item);
91+
}
9092
}
9193
$this->AttributesToGet = $input['AttributesToGet'] ?? null;
9294
$this->ConsistentRead = $input['ConsistentRead'] ?? null;
@@ -245,8 +247,13 @@ private function requestBody(): array
245247
throw new InvalidArgument(sprintf('Missing parameter "Key" for "%s". The value cannot be null.', __CLASS__));
246248
}
247249

248-
foreach ($v as $name => $v) {
249-
$payload['Key'][$name] = $v->requestBody();
250+
if (empty($v)) {
251+
$payload['Key'] = new \stdClass();
252+
} else {
253+
$payload['Key'] = [];
254+
foreach ($v as $name => $mv) {
255+
$payload['Key'][$name] = $mv->requestBody();
256+
}
250257
}
251258
if (null !== $v = $this->AttributesToGet) {
252259
$index = -1;
@@ -269,8 +276,13 @@ private function requestBody(): array
269276
$payload['ProjectionExpression'] = $v;
270277
}
271278
if (null !== $v = $this->ExpressionAttributeNames) {
272-
foreach ($v as $name => $v) {
273-
$payload['ExpressionAttributeNames'][$name] = $v;
279+
if (empty($v)) {
280+
$payload['ExpressionAttributeNames'] = new \stdClass();
281+
} else {
282+
$payload['ExpressionAttributeNames'] = [];
283+
foreach ($v as $name => $mv) {
284+
$payload['ExpressionAttributeNames'][$name] = $mv;
285+
}
274286
}
275287
}
276288

src/Input/PutItemInput.php

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,18 @@ public function __construct(array $input = [])
117117
{
118118
$this->TableName = $input['TableName'] ?? null;
119119

120-
$this->Item = [];
121-
foreach ($input['Item'] ?? [] as $key => $item) {
122-
$this->Item[$key] = AttributeValue::create($item);
120+
if (isset($input['Item'])) {
121+
$this->Item = [];
122+
foreach ($input['Item'] as $key => $item) {
123+
$this->Item[$key] = AttributeValue::create($item);
124+
}
123125
}
124126

125-
$this->Expected = [];
126-
foreach ($input['Expected'] ?? [] as $key => $item) {
127-
$this->Expected[$key] = ExpectedAttributeValue::create($item);
127+
if (isset($input['Expected'])) {
128+
$this->Expected = [];
129+
foreach ($input['Expected'] as $key => $item) {
130+
$this->Expected[$key] = ExpectedAttributeValue::create($item);
131+
}
128132
}
129133
$this->ReturnValues = $input['ReturnValues'] ?? null;
130134
$this->ReturnConsumedCapacity = $input['ReturnConsumedCapacity'] ?? null;
@@ -133,9 +137,11 @@ public function __construct(array $input = [])
133137
$this->ConditionExpression = $input['ConditionExpression'] ?? null;
134138
$this->ExpressionAttributeNames = $input['ExpressionAttributeNames'] ?? null;
135139

136-
$this->ExpressionAttributeValues = [];
137-
foreach ($input['ExpressionAttributeValues'] ?? [] as $key => $item) {
138-
$this->ExpressionAttributeValues[$key] = AttributeValue::create($item);
140+
if (isset($input['ExpressionAttributeValues'])) {
141+
$this->ExpressionAttributeValues = [];
142+
foreach ($input['ExpressionAttributeValues'] as $key => $item) {
143+
$this->ExpressionAttributeValues[$key] = AttributeValue::create($item);
144+
}
139145
}
140146
parent::__construct($input);
141147
}
@@ -349,12 +355,22 @@ private function requestBody(): array
349355
throw new InvalidArgument(sprintf('Missing parameter "Item" for "%s". The value cannot be null.', __CLASS__));
350356
}
351357

352-
foreach ($v as $name => $v) {
353-
$payload['Item'][$name] = $v->requestBody();
358+
if (empty($v)) {
359+
$payload['Item'] = new \stdClass();
360+
} else {
361+
$payload['Item'] = [];
362+
foreach ($v as $name => $mv) {
363+
$payload['Item'][$name] = $mv->requestBody();
364+
}
354365
}
355366
if (null !== $v = $this->Expected) {
356-
foreach ($v as $name => $v) {
357-
$payload['Expected'][$name] = $v->requestBody();
367+
if (empty($v)) {
368+
$payload['Expected'] = new \stdClass();
369+
} else {
370+
$payload['Expected'] = [];
371+
foreach ($v as $name => $mv) {
372+
$payload['Expected'][$name] = $mv->requestBody();
373+
}
358374
}
359375
}
360376
if (null !== $v = $this->ReturnValues) {
@@ -385,13 +401,23 @@ private function requestBody(): array
385401
$payload['ConditionExpression'] = $v;
386402
}
387403
if (null !== $v = $this->ExpressionAttributeNames) {
388-
foreach ($v as $name => $v) {
389-
$payload['ExpressionAttributeNames'][$name] = $v;
404+
if (empty($v)) {
405+
$payload['ExpressionAttributeNames'] = new \stdClass();
406+
} else {
407+
$payload['ExpressionAttributeNames'] = [];
408+
foreach ($v as $name => $mv) {
409+
$payload['ExpressionAttributeNames'][$name] = $mv;
410+
}
390411
}
391412
}
392413
if (null !== $v = $this->ExpressionAttributeValues) {
393-
foreach ($v as $name => $v) {
394-
$payload['ExpressionAttributeValues'][$name] = $v->requestBody();
414+
if (empty($v)) {
415+
$payload['ExpressionAttributeValues'] = new \stdClass();
416+
} else {
417+
$payload['ExpressionAttributeValues'] = [];
418+
foreach ($v as $name => $mv) {
419+
$payload['ExpressionAttributeValues'][$name] = $mv->requestBody();
420+
}
395421
}
396422
}
397423

0 commit comments

Comments
 (0)