Skip to content

Commit

Permalink
Compatiblity with Symfony 4.3 (api-platform#2784)
Browse files Browse the repository at this point in the history
* Compatiblity with Symfony 4.3

* Fix all broken unit tests

* Fix some deprecations

* Remove @Final from ItemNormalizer

* Fix Behat tests

* Use composition for the Exception Listener

* Fix default firewall logout_on_user_change deprecation

* Make sure that sodium is supported

* Fix composer and tests

* Fix PHPStan config and CS

* Remove deprecated option

* Fix MongoDB

* Fix rebase
  • Loading branch information
dunglas authored and betd-sthibault committed Aug 7, 2019
1 parent ea11026 commit e06475f
Show file tree
Hide file tree
Showing 21 changed files with 129 additions and 47 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ jobs:
- *validate-openapi-v3-yaml

- php: '7.3'
env: SYMFONY_DEPRECATIONS_HELPER=0
env: SYMFONY_DEPRECATIONS_HELPER=max[total]=0
before_install:
- *install-mongodb-php-extension
- *disable-xdebug-php-extension
Expand All @@ -229,5 +229,5 @@ jobs:
- *validate-openapi-v3-yaml

allow_failures:
- env: SYMFONY_DEPRECATIONS_HELPER=0
- env: SYMFONY_DEPRECATIONS_HELPER=max[total]=0
fast_finish: true
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"symfony/http-kernel": "^3.4 || ^4.0",
"symfony/property-access": "^3.4 || ^4.0",
"symfony/property-info": "^3.4 || ^4.0",
"symfony/serializer": "^4.2.6",
"symfony/serializer": "^4.3",
"symfony/web-link": "^4.1",
"willdurand/negotiation": "^2.0.3"
},
Expand All @@ -44,7 +44,7 @@
"guzzlehttp/guzzle": "^6.0",
"jangregor/phpstan-prophecy": "^0.3",
"justinrainbow/json-schema": "^5.0",
"nelmio/api-doc-bundle": "^2.13.3",
"nelmio/api-doc-bundle": "^2.13.4",
"phpdocumentor/reflection-docblock": "^3.0 || ^4.0",
"phpdocumentor/type-resolver": "^0.3 || ^0.4",
"phpspec/prophecy": "^1.8",
Expand All @@ -71,10 +71,10 @@
"symfony/expression-language": "^3.4 || ^4.0",
"symfony/finder": "^3.4 || ^4.0",
"symfony/form": "^3.4 || ^4.0",
"symfony/framework-bundle": "^4.2",
"symfony/framework-bundle": "^4.3",
"symfony/mercure-bundle": "*",
"symfony/messenger": "^4.2",
"symfony/phpunit-bridge": "^4.3",
"symfony/messenger": "^4.3",
"symfony/phpunit-bridge": "^4.3.1",
"symfony/routing": "^3.4 || ^4.0",
"symfony/security-bundle": "^3.4 || ^4.0",
"symfony/security-core": "^3.4 || ^4.0",
Expand All @@ -86,8 +86,7 @@
},
"conflict": {
"doctrine/common": "<2.7",
"doctrine/mongodb-odm": "<2.0",
"symfony/messenger": ">=4.3"
"doctrine/mongodb-odm": "<2.0"
},
"suggest": {
"doctrine/mongodb-odm-bundle": "To support MongoDB. Only versions 4.0 and later are supported.",
Expand Down
2 changes: 1 addition & 1 deletion features/bootstrap/HydraContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ private function getPropertyInfo(string $propertyName, string $className): stdCl
}
}

throw new \InvalidArgumentException(sprintf('Property "%s" of class "%s" does\'nt exist', $propertyName, $className));
throw new \InvalidArgumentException(sprintf('Property "%s" of class "%s" doesn\'t exist', $propertyName, $className));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion features/graphql/mutation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ Feature: GraphQL mutation support
When I send the following GraphQL request:
"""
mutation {
createDummy(input: {_id: 12, name: "", foo: [], clientMutationId: "myId"}) {
createDummy(input: {name: "", foo: [], clientMutationId: "myId"}) {
clientMutationId
}
}
Expand Down
2 changes: 0 additions & 2 deletions features/hydra/docs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ Feature: Documentation support
And the value of the node "hydra:title" of the Hydra class "Dummy" is "Dummy"
And the value of the node "hydra:description" of the Hydra class "Dummy" is "Dummy."
# Properties
And "id" property is readable for Hydra class "Dummy"
And "id" property is writable for Hydra class "Dummy"
And "name" property is readable for Hydra class "Dummy"
And "name" property is writable for Hydra class "Dummy"
And "name" property is required for Hydra class "Dummy"
Expand Down
1 change: 0 additions & 1 deletion features/jsonld/context.feature
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ Feature: JSON-LD contexts generation
"jsonData": "Dummy/jsonData",
"arrayData": "Dummy/arrayData",
"nameConverted": "Dummy/nameConverted",
"id": "Dummy/id",
"name": "http://schema.org/name",
"alias": "https://schema.org/alternateName",
"foo": "Dummy/foo"
Expand Down
4 changes: 2 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ parameters:
- '#Call to an undefined method Doctrine\\Common\\Persistence\\ObjectManager::getConnection\(\)#'
# https://github.com/symfony/symfony/pull/31903
-
message: '#Access to an undefined property object::\$headers\.#'
message: '#Access to property \$headers on an unknown class Symfony\\Component\\HttpKernel\\Response\.#'
path: %currentWorkingDirectory%/tests/Bridge/Symfony/Bundle/Twig/ApiPlatformProfilerPanelTest.php
-
message: '#Call to an undefined method object::getStatusCode\(\)\.#'
message: '#Call to method getStatusCode\(\) on an unknown class Symfony\\Component\\HttpKernel\\Response\.#'
path: %currentWorkingDirectory%/tests/Bridge/Symfony/Bundle/Twig/ApiPlatformProfilerPanelTest.php
- '#Parameter \#1 \$function of function call_user_func expects callable\(\): mixed, .+ given\.#'
- '#Parameter \#1 \$exception of static method Symfony\\Component\\Debug\\Exception\\FlattenException::create\(\) expects Exception, Symfony\\Component\\Serializer\\Exception\\ExceptionInterface given\.#'
Expand Down
45 changes: 38 additions & 7 deletions src/Bridge/Doctrine/MongoDbOdm/PropertyInfo/DoctrineExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Persistence\Mapping\MappingException;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Types\Type as MongoDbType;
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\PropertyInfo\Type;
Expand All @@ -29,7 +31,7 @@
* @author Kévin Dunglas <[email protected]>
* @author Alan Poulain <[email protected]>
*/
final class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeExtractorInterface
final class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeExtractorInterface, PropertyAccessExtractorInterface
{
private $objectManager;

Expand All @@ -43,9 +45,7 @@ public function __construct(ObjectManager $objectManager)
*/
public function getProperties($class, array $context = [])
{
try {
$metadata = $this->objectManager->getClassMetadata($class);
} catch (MappingException $exception) {
if (null === $metadata = $this->getMetadata($class)) {
return null;
}

Expand All @@ -57,9 +57,7 @@ public function getProperties($class, array $context = [])
*/
public function getTypes($class, $property, array $context = [])
{
try {
$metadata = $this->objectManager->getClassMetadata($class);
} catch (MappingException $exception) {
if (null === $metadata = $this->getMetadata($class)) {
return null;
}

Expand Down Expand Up @@ -111,6 +109,39 @@ public function getTypes($class, $property, array $context = [])
}
}

/**
* {@inheritdoc}
*/
public function isReadable($class, $property, array $context = []): ?bool
{
return null;
}

/**
* {@inheritdoc}
*/
public function isWritable($class, $property, array $context = []): ?bool
{
if (
null === ($metadata = $this->getMetadata($class))
|| ClassMetadata::GENERATOR_TYPE_NONE === $metadata->generatorType
|| !\in_array($property, $metadata->getIdentifierFieldNames(), true)
) {
return null;
}

return false;
}

private function getMetadata(string $class): ?ClassMetadata
{
try {
return $this->objectManager->getClassMetadata($class);
} catch (MappingException $exception) {
return null;
}
}

/**
* Gets the corresponding built-in PHP type.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
<argument type="service" id="api_platform.serializer" />
<argument>%api_platform.formats%</argument>
<argument type="service" id="message_bus" on-invalid="ignore" />
<argument type="service" id="messenger.default_bus" on-invalid="ignore" />
<argument type="service" id="mercure.hub.default.publisher" />

<tag name="doctrine.event_listener" event="onFlush" />
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Symfony/Bundle/Resources/config/messenger.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="api_platform.message_bus" alias="message_bus" />
<service id="api_platform.message_bus" alias="messenger.default_bus" />

<service id="api_platform.messenger.data_persister" class="ApiPlatform\Core\Bridge\Symfony\Messenger\DataPersister" public="false">
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,13 @@
</thead>

<tbody>
{% for key, value in collector.resourceMetadata.attributes if key != 'filters' %}
<tr>
<th scope="row">{{ key }}</th>
<td>{{- profiler_dump(value, 2) -}}</td>
</tr>
{% for key, value in collector.resourceMetadata.attributes %}
{% if key != 'filters' %}
<tr>
<th scope="row">{{ key }}</th>
<td>{{- profiler_dump(value, 2) -}}</td>
</tr>
{% endif %}
{% endfor %}
</tbody>
</table>
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Symfony/Messenger/DataPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ public function persist($data, array $context = [])
*/
public function remove($data, array $context = [])
{
$this->messageBus->dispatch(new Envelope($data, new RemoveStamp()));
$this->messageBus->dispatch(new Envelope($data, [new RemoveStamp()]));
}
}
12 changes: 10 additions & 2 deletions src/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Core\EventListener;

use ApiPlatform\Core\Util\RequestAttributesExtractor;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as BaseExceptionListener;

Expand All @@ -23,8 +24,15 @@
* @author Samuel ROZE <[email protected]>
* @author Kévin Dunglas <[email protected]>
*/
final class ExceptionListener extends BaseExceptionListener
final class ExceptionListener
{
private $exceptionListener;

public function __construct($controller, LoggerInterface $logger = null)
{
$this->exceptionListener = new BaseExceptionListener($controller, $logger);
}

public function onKernelException(GetResponseForExceptionEvent $event): void
{
$request = $event->getRequest();
Expand All @@ -36,6 +44,6 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
return;
}

parent::onKernelException($event);
$this->exceptionListener->onKernelException($event);
}
}
10 changes: 5 additions & 5 deletions src/Serializer/AbstractItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ protected function denormalizeCollection(string $attribute, PropertyMetadata $pr
);
}

$values[$index] = $this->denormalizeRelation($attribute, $propertyMetadata, $className, $obj, $format, $this->createChildContext($context, $attribute));
$values[$index] = $this->denormalizeRelation($attribute, $propertyMetadata, $className, $obj, $format, $this->createChildContext($context, $attribute, $format));
}

return $values;
Expand Down Expand Up @@ -536,7 +536,7 @@ protected function getAttributeValue($object, $attribute, $format = null, array
$this->resourceClassResolver->isResourceClass($className)
) {
$resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className);
$childContext = $this->createChildContext($context, $attribute);
$childContext = $this->createChildContext($context, $attribute, $format);
$childContext['resource_class'] = $resourceClass;

return $this->normalizeCollectionOfRelations($propertyMetadata, $attributeValue, $resourceClass, $format, $childContext);
Expand All @@ -547,8 +547,8 @@ protected function getAttributeValue($object, $attribute, $format = null, array
($className = $type->getClassName()) &&
$this->resourceClassResolver->isResourceClass($className)
) {
$resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className);
$childContext = $this->createChildContext($context, $attribute);
$resourceClass = $this->resourceClassResolver->getResourceClass($attributeValue, $className, true);
$childContext = $this->createChildContext($context, $attribute, $format);
$childContext['resource_class'] = $resourceClass;

return $this->normalizeRelation($propertyMetadata, $attributeValue, $resourceClass, $format, $childContext);
Expand Down Expand Up @@ -665,7 +665,7 @@ private function createAttributeValue($attribute, $value, $format = null, array
$this->resourceClassResolver->isResourceClass($className)
) {
$resourceClass = $this->resourceClassResolver->getResourceClass(null, $className);
$childContext = $this->createChildContext($context, $attribute);
$childContext = $this->createChildContext($context, $attribute, $format);
$childContext['resource_class'] = $resourceClass;

return $this->denormalizeRelation($attribute, $propertyMetadata, $resourceClass, $value, $format, $childContext);
Expand Down
2 changes: 0 additions & 2 deletions src/Serializer/ItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
/**
* Generic item normalizer.
*
* @final
*
* @author Kévin Dunglas <[email protected]>
*/
class ItemNormalizer extends AbstractItemNormalizer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ApiPlatform\Core\Tests\Bridge\Doctrine\MongoDbOdm\PropertyInfo\Fixtures\DoctrineDummy;
use ApiPlatform\Core\Tests\Bridge\Doctrine\MongoDbOdm\PropertyInfo\Fixtures\DoctrineEmbeddable;
use ApiPlatform\Core\Tests\Bridge\Doctrine\MongoDbOdm\PropertyInfo\Fixtures\DoctrineFooType;
use ApiPlatform\Core\Tests\Bridge\Doctrine\MongoDbOdm\PropertyInfo\Fixtures\DoctrineGeneratedValue;
use ApiPlatform\Core\Tests\Bridge\Doctrine\MongoDbOdm\PropertyInfo\Fixtures\DoctrineRelation;
use ApiPlatform\Core\Tests\Bridge\Doctrine\MongoDbOdm\PropertyInfo\Fixtures\DoctrineWithEmbedded;
use Doctrine\Common\Collections\Collection;
Expand Down Expand Up @@ -193,6 +194,15 @@ public function testGetTypesCatchException(): void
$this->assertNull($this->createExtractor()->getTypes('Not\Exist', 'baz'));
}

public function testGeneratedValueNotWritable()
{
$extractor = $this->createExtractor();
$this->assertFalse($extractor->isWritable(DoctrineGeneratedValue::class, 'id'));
$this->assertNull($extractor->isReadable(DoctrineGeneratedValue::class, 'id'));
$this->assertNull($extractor->isWritable(DoctrineGeneratedValue::class, 'foo'));
$this->assertNull($extractor->isReadable(DoctrineGeneratedValue::class, 'foo'));
}

private function createExtractor(): DoctrineExtractor
{
$config = DoctrineMongoDbOdmSetup::createAnnotationMetadataConfiguration([__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'], true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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\Bridge\Doctrine\MongoDbOdm\PropertyInfo\Fixtures;

use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Field;
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id;

/**
* @Document
*
* @author Kévin Dunglas <[email protected]>
* @author Alan Poulain <[email protected]>
*/
class DoctrineGeneratedValue
{
/**
* @Id(strategy="INCREMENT", type="integer")
*/
public $id;

/**
* @Field
*/
public $foo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public function testDisabledSwaggerUIAndRedoc()
public function testDisabledMessenger()
{
$containerBuilderProphecy = $this->getBaseContainerBuilderProphecy();
$containerBuilderProphecy->setAlias('api_platform.message_bus', 'message_bus')->shouldNotBeCalled();
$containerBuilderProphecy->setAlias('api_platform.message_bus', 'messenger.default_bus')->shouldNotBeCalled();
$containerBuilderProphecy->setDefinition('api_platform.messenger.data_persister', Argument::type(Definition::class))->shouldNotBeCalled();
$containerBuilderProphecy->setDefinition('api_platform.messenger.data_transformer', Argument::type(Definition::class))->shouldNotBeCalled();
$containerBuilder = $containerBuilderProphecy->reveal();
Expand Down Expand Up @@ -1140,7 +1140,7 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo

$aliases = [
'api_platform.http_cache.purger' => 'api_platform.http_cache.purger.varnish',
'api_platform.message_bus' => 'message_bus',
'api_platform.message_bus' => 'messenger.default_bus',
EagerLoadingExtension::class => 'api_platform.doctrine.orm.query_extension.eager_loading',
FilterExtension::class => 'api_platform.doctrine.orm.query_extension.filter',
FilterEagerLoadingExtension::class => 'api_platform.doctrine.orm.query_extension.filter_eager_loading',
Expand Down
Loading

0 comments on commit e06475f

Please sign in to comment.