Skip to content

Serializer listener performance - anonymous class not serializable #1780

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

Merged
merged 1 commit into from
Mar 19, 2018
Merged
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
7 changes: 2 additions & 5 deletions src/EventListener/SerializeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Core\EventListener;

use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Serializer\ResourceList;
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
use ApiPlatform\Core\Util\RequestAttributesExtractor;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -59,11 +60,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
}

$context = $this->serializerContextBuilder->createFromRequest($request, true, $attributes);
$resources = new class() extends \ArrayObject {
public function serialize()
{
}
};
$resources = new ResourceList();
$context['resources'] = &$resources;
$request->attributes->set('_api_normalization_context', $context);

Expand Down
24 changes: 24 additions & 0 deletions src/Serializer/ResourceList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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\Core\Serializer;

/**
* @internal
*/
class ResourceList extends \ArrayObject
{
public function serialize()
{
}
}
40 changes: 31 additions & 9 deletions tests/EventListener/SerializeListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Core\Tests\EventListener;

use ApiPlatform\Core\EventListener\SerializeListener;
use ApiPlatform\Core\Serializer\ResourceList;
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
Expand Down Expand Up @@ -105,11 +106,22 @@ public function testSerializeCollectionOperation()
{
$expectedContext = ['request_uri' => '', 'resource_class' => 'Foo', 'collection_operation_name' => 'get'];
$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->serialize(Argument::any(), 'xml', Argument::that(function ($context) use ($expectedContext) {
unset($context['resources']);

return $context === $expectedContext;
}))->willReturn('bar')->shouldBeCalled();
$serializerProphecy
->serialize(
Argument::any(),
'xml',
Argument::allOf(
Argument::that(function (array $context) {
return $context['resources'] instanceof ResourceList;
}),
Argument::withEntry('request_uri', ''),
Argument::withEntry('resource_class', 'Foo'),
Argument::withEntry('collection_operation_name', 'get')
)
)
->willReturn('bar')
->shouldBeCalled();

$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'get']);
$request->setRequestFormat('xml');
Expand All @@ -130,11 +142,21 @@ public function testSerializeItemOperation()
{
$expectedContext = ['request_uri' => '', 'resource_class' => 'Foo', 'item_operation_name' => 'get'];
$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->serialize(Argument::any(), 'xml', Argument::that(function ($context) use ($expectedContext) {
unset($context['resources']);

return $context === $expectedContext;
}))->willReturn('bar')->shouldBeCalled();
$serializerProphecy
->serialize(
Argument::any(),
'xml',
Argument::allOf(
Argument::that(function (array $context) {
return $context['resources'] instanceof ResourceList;
}),
Argument::withEntry('request_uri', ''),
Argument::withEntry('resource_class', 'Foo'),
Argument::withEntry('item_operation_name', 'get')
)
)
->willReturn('bar')
->shouldBeCalled();

$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_item_operation_name' => 'get']);
$request->setRequestFormat('xml');
Expand Down
42 changes: 42 additions & 0 deletions tests/Serializer/ResourceListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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\Core\Tests\Serializer;

use ApiPlatform\Core\Serializer\ResourceList;
use PHPUnit\Framework\TestCase;

class ResourceListTest extends TestCase
{
/**
* @var ResourceList
*/
private $resourceList;

public function setUp()
{
$this->resourceList = new ResourceList();
}

public function testImplementsArrayObject()
{
$this->assertInstanceOf(\ArrayObject::class, $this->resourceList);
}

public function testSerialize()
{
$this->resourceList['foo'] = 'bar';

$this->assertEquals('N;', \serialize($this->resourceList));
}
}