Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IBX-9678: JsonSerializableNormalizer return type mismatch causing 500 error #160

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/lib/Output/Normalizer/JsonSerializableNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ final class JsonSerializableNormalizer implements NormalizerInterface, Normalize
/**
* @param array<mixed> $context
*
* @return string|array<mixed>
*
* {@inheritDoc}
*/
public function normalize($object, ?string $format = null, array $context = []): string
public function normalize($object, ?string $format = null, array $context = []): string|array
{
assert($object instanceof JsonSerializable);

Expand Down
30 changes: 30 additions & 0 deletions tests/lib/Output/MoneyObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
declare(strict_types=1);

namespace Ibexa\Tests\Rest\Output;

class MoneyObject implements \JsonSerializable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class MoneyObject implements \JsonSerializable
final class MoneyObject implements \JsonSerializable

{
private int $amount;

private string $currency;

public function __construct(int $amount, string $currency)
{
$this->amount = $amount;
$this->currency = $currency;
}

#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return [
'amount' => $this->amount,
'currency' => $this->currency,
];
}
}
28 changes: 28 additions & 0 deletions tests/lib/Output/Normalizer/ArrayNormalizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
*/
declare(strict_types=1);


namespace Ibexa\Tests\Rest\Output\Normalizer;

use Ibexa\Rest\Output\Normalizer\JsonSerializableNormalizer;
use Ibexa\Tests\Rest\Output\MoneyObject;
use PHPUnit\Framework\TestCase;

class ArrayNormalizerTest extends TestCase
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class ArrayNormalizerTest extends TestCase
final class ArrayNormalizerTest extends TestCase

{
public function testNormalizeArray(): void
{
$normalizer = new JsonSerializableNormalizer();
$money = new MoneyObject(100, 'EUR');

$result = $normalizer->normalize($money);

self::assertIsArray($result);

self::assertSame(100, $result['amount']);
self::assertSame('EUR', $result['currency']);
}
}
Loading