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

Search filter introduces #3331 back with deprecations #3521

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions features/main/uuid.feature
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,17 @@ Feature: Using uuid identifier on resource
Then the response status code should be 404
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"

@!mongodb
@createSchema
Scenario: Create a resource identified by Ramsey\Uuid\Uuid
When I add "Content-Type" header equal to "application/ld+json"
And I send a "POST" request to "/ramsey_uuid_dummies" with body:
"""
{
"id": "41b29566-144b-11e6-a148-3e1d05defe78"
}
"""
Then the response status code should be 201
And the response should be in JSON
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
12 changes: 10 additions & 2 deletions src/Bridge/Doctrine/Orm/Filter/SearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* Filter the collection by given properties.
*
* @author Kévin Dunglas <dunglas@gmail.com>
* @final
*/
class SearchFilter extends AbstractContextAwareFilter implements SearchFilterInterface
{
Expand Down Expand Up @@ -113,7 +114,7 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
}

if (1 === \count($values)) {
$this->addWhereByStrategy($strategy, $queryBuilder, $queryNameGenerator, $alias, $field, $doctrineTypeField, $values[0], $caseSensitive);
$this->addWhereByStrategy($strategy, $queryBuilder, $queryNameGenerator, $alias, $field, $values[0], $caseSensitive, $doctrineTypeField);

return;
}
Expand Down Expand Up @@ -181,8 +182,15 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
*
* @throws InvalidArgumentException If strategy does not exist
*/
protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $fieldType, $value, bool $caseSensitive)
protected function addWhereByStrategy(string $strategy, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $alias, string $field, $value, bool $caseSensitive/*, string $fieldType = null*/)
{
$fieldType = null;
if (8 === \func_num_args()) {
$fieldType = func_get_arg(7);
} else {
@trigger_error(sprintf('Method "%s()" will have a 8th `string $fieldType` argument in version 3.0. Not defining it is deprecated since 2.6.', __METHOD__), E_USER_DEPRECATED);
}

$wrapCase = $this->createWrapCase($caseSensitive);
$valueParameter = $queryNameGenerator->generateParameterName($field);

Expand Down
31 changes: 31 additions & 0 deletions src/Bridge/RamseyUuid/Serializer/UuidDenormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?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\Core\Bridge\RamseyUuid\Serializer;

use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

final class UuidDenormalizer implements DenormalizerInterface
{
public function denormalize($data, $type, $format = null, array $context = [])
{
return Uuid::fromString($data);
}

public function supportsDenormalization($data, $type, $format = null)
{
return \is_string($data) && is_a($type, UuidInterface::class, true);
}
}
4 changes: 4 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/ramsey_uuid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
<service id="api_platform.identifier.uuid_normalizer" class="ApiPlatform\Core\Bridge\RamseyUuid\Identifier\Normalizer\UuidNormalizer" public="false">
<tag name="api_platform.identifier.denormalizer" />
</service>

<service id="api_platform.serializer.uuid_denormalizer" class="ApiPlatform\Core\Bridge\RamseyUuid\Serializer\UuidDenormalizer" public="false">
<tag name="serializer.normalizer" />
</service>
</services>
</container>
13 changes: 13 additions & 0 deletions tests/Bridge/Doctrine/Orm/Filter/SearchFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use ApiPlatform\Core\Exception\InvalidArgumentException;
use ApiPlatform\Core\Test\DoctrineOrmFilterTestCase;
use ApiPlatform\Core\Tests\Bridge\Doctrine\Common\Filter\SearchFilterTestTrait;
use ApiPlatform\Core\Tests\Fixtures\ExtendingSearchFilter;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Serializer\NameConverter\CustomConverter;
Expand Down Expand Up @@ -363,6 +364,18 @@ private function doTestApplyWithAnotherAlias(bool $request)

$expectedDql = sprintf('SELECT %s FROM %s %1$s WHERE %1$s.name = :name_p1', 'somealias', Dummy::class);
$this->assertEquals($expectedDql, $queryBuilder->getQuery()->getDQL());
$this->assertTrue($queryBuilder->getParameter('name_p1')->typeWasSpecified());
}

/**
* @group legacy
* @expectedDeprecation The "ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter" class is considered final. It may change without further notice as of its next major version. You should not extend it from "ApiPlatform\Core\Tests\Fixtures\ExtendingSearchFilter".
*
* @expectedDeprecation Method "ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter::addWhereByStrategy()" will have a 8th `string $fieldType` argument in version 3.0. Not defining it is deprecated since 2.6.
*/
public function testDeprecateAddWhereByStrategyWithoutType()
{
new ExtendingSearchFilter($this->repository->createQueryBuilder($this->alias));
}

public function provideApplyTestData(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,7 @@ private function getPartialContainerBuilderProphecy($configuration = null)
'api_platform.serializer.group_filter',
'api_platform.serializer.normalizer.item',
'api_platform.serializer.property_filter',
'api_platform.serializer.uuid_denormalizer',
'api_platform.serializer_locator',
'api_platform.subresource_data_provider',
'api_platform.subresource_operation_factory',
Expand Down
27 changes: 27 additions & 0 deletions tests/Fixtures/ExtendingSearchFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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\Core\Tests\Fixtures;

use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\SearchFilterInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
use Doctrine\ORM\QueryBuilder;

class ExtendingSearchFilter extends SearchFilter implements SearchFilterInterface
{
public function __construct(QueryBuilder $queryBuilder)
{
parent::addWhereByStrategy(self::STRATEGY_EXACT, $queryBuilder, new QueryNameGenerator(), 'o', 'name', 'test', false);
}
}