-
Notifications
You must be signed in to change notification settings - Fork 3
IBX-9678: JsonSerializableNormalizer return type mismatch causing 500 error #160
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||||||||||||||
*/ | ||||||||||||||
|
||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
namespace Ibexa\Tests\Rest\Output; | ||||||||||||||
|
||||||||||||||
class MoneyObject implements \JsonSerializable | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
{ | ||||||||||||||
private int $amount; | ||||||||||||||
|
||||||||||||||
private string $currency; | ||||||||||||||
|
||||||||||||||
public function __construct(int $amount, string $currency) | ||||||||||||||
{ | ||||||||||||||
$this->amount = $amount; | ||||||||||||||
$this->currency = $currency; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
#[\ReturnTypeWillChange] | ||||||||||||||
public function jsonSerialize() | ||||||||||||||
Comment on lines
+22
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can narrow down the return type from an interface. This is allowed.
Suggested change
|
||||||||||||||
{ | ||||||||||||||
return [ | ||||||||||||||
'amount' => $this->amount, | ||||||||||||||
'currency' => $this->currency, | ||||||||||||||
]; | ||||||||||||||
} | ||||||||||||||
} |
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. | ||||||||
*/ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
{ | ||||||||
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']); | ||||||||
} | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, we should remove the return type, as \JsonSerializable::jsonSerialize() method does not offer any guaranties about the returned type 😞