-
-
Notifications
You must be signed in to change notification settings - Fork 841
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes an issue with the use of `EMPTY_ARRAY_AS_OBJECT` and `PRESERVE_EMPTY_OBJECTS` in the serialization context. Since the CollectionNormalizer was taking over the serialization of all iterables, the Symfony serializer was not called when normalizing a "raw" collection. Adding a better supports method fixes the issue. The supports method is not cacheable anymore since it relies on the context.
- Loading branch information
1 parent
14c7cba
commit 469d907
Showing
9 changed files
with
136 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
Feature: Serialize empty array as object | ||
In order to have a coherent JSON representation | ||
As a developer | ||
I should be able to serialize some empty array properties as objects | ||
|
||
@createSchema | ||
Scenario: Get a resource with empty array properties as objects | ||
When I add "Content-Type" header equal to "application/ld+json" | ||
And I send a "GET" request to "/empty_array_as_objects/5" | ||
Then the response status code should be 200 | ||
And the response should be in JSON | ||
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" | ||
And the JSON should be equal to: | ||
""" | ||
{ | ||
"@context": "/contexts/EmptyArrayAsObject", | ||
"@id": "/empty_array_as_objects/6", | ||
"@type": "EmptyArrayAsObject", | ||
"id": 6, | ||
"emptyArray": [], | ||
"emptyArrayAsObject": {}, | ||
"arrayObjectAsArray": [], | ||
"arrayObject": {}, | ||
"stringArray": [ | ||
"foo", | ||
"bar" | ||
], | ||
"objectArray": { | ||
"foo": 67, | ||
"bar": "baz" | ||
} | ||
} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\Fixtures\TestBundle\Model; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\State\EmptyArrayAsObjectProvider; | ||
use Symfony\Component\Serializer\Annotation\Context; | ||
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer; | ||
use Symfony\Component\Serializer\Serializer; | ||
|
||
#[ApiResource(operations: [new Get()], provider: EmptyArrayAsObjectProvider::class)] | ||
class EmptyArrayAsObject | ||
{ | ||
public int $id = 6; | ||
|
||
public array $emptyArray = []; | ||
|
||
#[Context([Serializer::EMPTY_ARRAY_AS_OBJECT => true, AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true])] | ||
public array $emptyArrayAsObject = []; | ||
|
||
public \ArrayObject $arrayObjectAsArray; | ||
|
||
#[Context([AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true])] | ||
public \ArrayObject $arrayObject; | ||
|
||
public array $stringArray = ['foo', 'bar']; | ||
|
||
public array $objectArray = ['foo' => 67, 'bar' => 'baz']; | ||
|
||
public function __construct() | ||
{ | ||
$this->arrayObjectAsArray = new \ArrayObject(); | ||
$this->arrayObject = new \ArrayObject(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
tests/Fixtures/TestBundle/State/EmptyArrayAsObjectProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Tests\Fixtures\TestBundle\State; | ||
|
||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProviderInterface; | ||
use ApiPlatform\Tests\Fixtures\TestBundle\Model\EmptyArrayAsObject; | ||
|
||
final class EmptyArrayAsObjectProvider implements ProviderInterface | ||
{ | ||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): EmptyArrayAsObject | ||
{ | ||
return new EmptyArrayAsObject(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.