Skip to content

Commit

Permalink
fix: Handle empty custom data (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgrayston-paddle authored Oct 17, 2024
1 parent c4549fc commit 7bdd4ab
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Check our main [developer changelog](https://developer.paddle.com/?utm_source=dx
- `details->lineItems[]->priceId` is now nullable
- `items[]->priceId` is now nullable
- `details->lineItems[]->product` can now return `Product` (with `id`) or `TransactionPreviewProduct` (with nullable `id`)
- Empty custom data array will now serialize to empty JSON object `{}`

### Added
- `TransactionsClient::create()` now supports operation items with optional properties:
Expand Down
5 changes: 4 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use Psr\Log\NullLogger;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
Expand Down Expand Up @@ -177,7 +178,9 @@ private function requestRaw(string $method, string|UriInterface $uri, array|\Jso
);

if ($payload !== null) {
$body = $serializer->serialize($payload, 'json');
$body = $serializer->serialize($payload, 'json', [
AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true,
]);

$request = $request->withBody(
// Satisfies empty body requests.
Expand Down
6 changes: 4 additions & 2 deletions src/Entities/Shared/CustomData.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public function __construct(
) {
}

public function jsonSerialize(): array|\JsonSerializable
public function jsonSerialize(): array|\JsonSerializable|\stdClass
{
return $this->data;
return $this->data === []
? (object) []
: $this->data;
}
}
6 changes: 4 additions & 2 deletions src/Notifications/Entities/Shared/CustomData.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public function __construct(
) {
}

public function jsonSerialize(): array|\JsonSerializable
public function jsonSerialize(): array|\JsonSerializable|\stdClass
{
return $this->data;
return $this->data === []
? (object) []
: $this->data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "Annual (per seat)",
"name": "Annual (per seat)",
"product": {
"custom_data": [],
"custom_data": {},
"description": "Some description",
"image_url": "https://paddle-sandbox.s3.amazonaws.com/user/10889/2nmP8MQSret0aWeDemRw_icon1.png",
"name": "Annual (per seat)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "Annual (per seat)",
"name": "Annual (per seat)",
"product": {
"custom_data": [],
"custom_data": {},
"description": "Some description",
"image_url": "https://paddle-sandbox.s3.amazonaws.com/user/10889/2nmP8MQSret0aWeDemRw_icon1.png",
"name": "Annual (per seat)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "Annual (per seat)",
"name": "Annual (per seat)",
"product": {
"custom_data": [],
"custom_data": {},
"description": "Some description",
"image_url": "https://paddle-sandbox.s3.amazonaws.com/user/10889/2nmP8MQSret0aWeDemRw_icon1.png",
"name": "Annual (per seat)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "Annual (per seat)",
"name": "Annual (per seat)",
"product": {
"custom_data": [],
"custom_data": {},
"description": "Some description",
"image_url": "https://paddle-sandbox.s3.amazonaws.com/user/10889/2nmP8MQSret0aWeDemRw_icon1.png",
"name": "Annual (per seat)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "Annual (per seat)",
"name": "Annual (per seat)",
"product": {
"custom_data": [],
"custom_data": {},
"description": "Some description",
"image_url": "https://paddle-sandbox.s3.amazonaws.com/user/10889/2nmP8MQSret0aWeDemRw_icon1.png",
"name": "Annual (per seat)",
Expand Down

0 comments on commit 7bdd4ab

Please sign in to comment.