Skip to content

Commit

Permalink
fix: Handle empty custom data
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgrayston-paddle committed Oct 11, 2024
1 parent 807fd31 commit 02ae267
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Check our main [developer changelog](https://developer.paddle.com/?utm_source=dx
- `TransactionsClient::preview()` `TransactionPreview` response now allows null price ID for non-catalog prices:
- `TransactionPreview` `items[]->price` can now return `Price` (with `id`) or `TransactionPreviewPrice` (with nullable `id`)
- `TransactionPreview` `details->lineItems[]->priceId` is now nullable
- Empty custom data array will now serialize to empty JSON object `{}`

### Added
- `TransactionsClient::create()` now supports operation items with optional properties:
Expand Down
12 changes: 11 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Serializer\Context\Normalizer\ObjectNormalizerContextBuilder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
Expand Down Expand Up @@ -172,7 +173,16 @@ private function requestRaw(string $method, string|UriInterface $uri, array|\Jso
$request = $this->requestFactory->createRequest($method, $uri);

$serializer = new Serializer(
[new BackedEnumNormalizer(), new JsonSerializableNormalizer(), new ObjectNormalizer(nameConverter: new CamelCaseToSnakeCaseNameConverter())],
[
new BackedEnumNormalizer(),
new JsonSerializableNormalizer(),
new ObjectNormalizer(
nameConverter: new CamelCaseToSnakeCaseNameConverter(),
defaultContext: (new ObjectNormalizerContextBuilder())
->withPreserveEmptyObjects(true)
->toArray(),
),
],
[new JsonEncoder()],
);

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 02ae267

Please sign in to comment.