Skip to content

feature: improve ids management for association and fix invalid iri/uuid in SearchFilter #1844

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
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
131 changes: 102 additions & 29 deletions features/doctrine/search_filter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,62 @@ Feature: Search filter on collections
When I send a "GET" request to "/dummy_cars?colors.prop=red"
Then the response status code should be 200
And the JSON should be deep equal to:
"""
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tab indent instead of spaces

{
"@context": "/contexts/DummyCar",
"@id": "/dummy_cars",
"@type": "hydra:Collection",
"hydra:member": [
"@context": "/contexts/DummyCar",
"@id": "/dummy_cars",
"@type": "hydra:Collection",
"hydra:member": [
{
"@id": "/dummy_cars/1",
"@type": "DummyCar",
"colors": [
{
"@id": "/dummy_cars/1",
"@type": "DummyCar",
"colors": [
{
"@id": "/dummy_car_colors/1",
"@type": "DummyCarColor",
"prop": "red"
},
{
"@id": "/dummy_car_colors/2",
"@type": "DummyCarColor",
"prop": "blue"
}
]
"@id": "/dummy_car_colors/1",
"@type": "DummyCarColor",
"prop": "red"
},
{
"@id": "/dummy_car_colors/2",
"@type": "DummyCarColor",
"prop": "blue"
}
],
"hydra:totalItems": 1,
"hydra:view": {
"@id": "/dummy_cars?colors.prop=red",
"@type": "hydra:PartialCollectionView"
},
"hydra:search": {
],
"secondColors": [
{
"@id": "/dummy_car_colors/1",
"@type": "DummyCarColor",
"prop": "red"
},
{
"@id": "/dummy_car_colors/2",
"@type": "DummyCarColor",
"prop": "blue"
}
],
"thirdColors": [
{
"@id": "/dummy_car_colors/1",
"@type": "DummyCarColor",
"prop": "red"
},
{
"@id": "/dummy_car_colors/2",
"@type": "DummyCarColor",
"prop": "blue"
}
],
"uuid": []
}
],
"hydra:totalItems": 1,
"hydra:view": {
"@id": "/dummy_cars?colors.prop=red",
"@type": "hydra:PartialCollectionView"
},
"hydra:search": {
"@type": "hydra:IriTemplate",
"hydra:template": "\/dummy_cars{?availableAt[before],availableAt[strictly_before],availableAt[after],availableAt[strictly_after],canSell,foobar[],foobargroups[],foobargroups_override[],colors.prop,name}",
"hydra:template": "/dummy_cars{?availableAt[before],availableAt[strictly_before],availableAt[after],availableAt[strictly_after],canSell,foobar[],foobargroups[],foobargroups_override[],colors.prop,colors,colors[],secondColors,secondColors[],thirdColors,thirdColors[],uuid,uuid[],name}",
"hydra:variableRepresentation": "BasicRepresentation",
"hydra:mapping": [
{
Expand Down Expand Up @@ -83,12 +108,24 @@ Feature: Search filter on collections
"property": "canSell",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "colors",
"property": "colors",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "colors.prop",
"property": "colors.prop",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "colors[]",
"property": "colors",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "foobar[]",
Expand All @@ -112,6 +149,42 @@ Feature: Search filter on collections
"variable": "name",
"property": "name",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "secondColors",
"property": "secondColors",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "secondColors[]",
"property": "secondColors",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "thirdColors",
"property": "thirdColors",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "thirdColors[]",
"property": "thirdColors",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "uuid",
"property": "uuid",
"required": false
},
{
"@type": "IriTemplateMapping",
"variable": "uuid[]",
"property": "uuid",
"required": false
}
]
}
Expand Down Expand Up @@ -389,8 +462,7 @@ Feature: Search filter on collections
"@id": {"pattern": "^/dummies$"},
"@type": {"pattern": "^hydra:Collection$"},
"hydra:member": {
"type": "array",
"maxItems": 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not remove this check, but replace it with the right value

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because the test was wrong in the first place, an invalid filter should not be applied and therefore should return every items of the collection. We fixed that here.

"type": "array"
},
"hydra:view": {
"type": "object",
Expand Down Expand Up @@ -655,3 +727,4 @@ Feature: Search filter on collections
}
}
"""

1 change: 1 addition & 0 deletions src/Bridge/Doctrine/Common/Filter/SearchFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ trait SearchFilterTrait

protected $iriConverter;
protected $propertyAccessor;
protected $identifiersExtractor;

/**
* {@inheritdoc}
Expand Down
23 changes: 21 additions & 2 deletions src/Bridge/Doctrine/MongoDbOdm/Filter/SearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@

namespace ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter;

use ApiPlatform\Core\Api\IdentifiersExtractorInterface;
use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\SearchFilterInterface;
use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\SearchFilterTrait;
use ApiPlatform\Core\Exception\InvalidArgumentException;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Aggregation\Builder;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as MongoDBClassMetadata;
use Doctrine\ODM\MongoDB\Types\Type as MongoDbType;
use MongoDB\BSON\Regex;
use Psr\Log\LoggerInterface;
Expand All @@ -40,12 +42,17 @@ final class SearchFilter extends AbstractFilter implements SearchFilterInterface

public const DOCTRINE_INTEGER_TYPE = MongoDbType::INTEGER;

public function __construct(ManagerRegistry $managerRegistry, IriConverterInterface $iriConverter, PropertyAccessorInterface $propertyAccessor = null, LoggerInterface $logger = null, array $properties = null)
public function __construct(ManagerRegistry $managerRegistry, IriConverterInterface $iriConverter, PropertyAccessorInterface $propertyAccessor = null, LoggerInterface $logger = null, array $properties = null, IdentifiersExtractorInterface $identifiersExtractor = null)
{
parent::__construct($managerRegistry, $logger, $properties);

if (null === $identifiersExtractor) {
@trigger_error('Not injecting ItemIdentifiersExtractor is deprecated since API Platform 2.5 and can lead to unexpected behaviors, it will not be possible anymore in API Platform 3.0.', E_USER_DEPRECATED);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mongo is marked as @experimental. Not sure BC is relevant here...

}

$this->iriConverter = $iriConverter;
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
$this->identifiersExtractor = $identifiersExtractor;
}

protected function getIriConverter(): IriConverterInterface
Expand Down Expand Up @@ -77,6 +84,10 @@ protected function filterProperty(string $property, $value, Builder $aggregation
if ($this->isPropertyNested($property, $resourceClass)) {
[$matchField, $field, $associations] = $this->addLookupsForNestedProperty($property, $aggregationBuilder, $resourceClass);
}

/**
* @var MongoDBClassMetadata
*/
$metadata = $this->getNestedMetadata($resourceClass, $associations);

$values = $this->normalizeValues((array) $value, $property);
Expand Down Expand Up @@ -124,8 +135,16 @@ protected function filterProperty(string $property, $value, Builder $aggregation
}

$values = array_map([$this, 'getIdFromValue'], $values);
$associationFieldIdentifier = 'id';
$doctrineTypeField = $this->getDoctrineFieldType($property, $resourceClass);

if (null !== $this->identifiersExtractor) {
$associationResourceClass = $metadata->getAssociationTargetClass($field);
$associationFieldIdentifier = $this->identifiersExtractor->getIdentifiersFromResourceClass($associationResourceClass)[0];
$doctrineTypeField = $this->getDoctrineFieldType($associationFieldIdentifier, $associationResourceClass);
}

if (!$this->hasValidValues($values, $this->getDoctrineFieldType($property, $resourceClass))) {
if (!$this->hasValidValues($values, $doctrineTypeField)) {
$this->logger->notice('Invalid filter ignored', [
'exception' => new InvalidArgumentException(sprintf('Values for field "%s" are not valid according to the doctrine type.', $property)),
]);
Expand Down
27 changes: 22 additions & 5 deletions src/Bridge/Doctrine/Orm/Filter/SearchFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Filter;

use ApiPlatform\Core\Api\IdentifiersExtractorInterface;
use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\SearchFilterInterface;
use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\SearchFilterTrait;
Expand All @@ -21,6 +22,7 @@
use ApiPlatform\Core\Exception\InvalidArgumentException;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\DBAL\Types\Type as DBALType;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\QueryBuilder;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -38,12 +40,17 @@ class SearchFilter extends AbstractContextAwareFilter implements SearchFilterInt

public const DOCTRINE_INTEGER_TYPE = DBALType::INTEGER;

public function __construct(ManagerRegistry $managerRegistry, ?RequestStack $requestStack, IriConverterInterface $iriConverter, PropertyAccessorInterface $propertyAccessor = null, LoggerInterface $logger = null, array $properties = null)
public function __construct(ManagerRegistry $managerRegistry, ?RequestStack $requestStack, IriConverterInterface $iriConverter, PropertyAccessorInterface $propertyAccessor = null, LoggerInterface $logger = null, array $properties = null, IdentifiersExtractorInterface $identifiersExtractor = null)
{
parent::__construct($managerRegistry, $requestStack, $logger, $properties);

if (null === $identifiersExtractor) {
@trigger_error('Not injecting ItemIdentifiersExtractor is deprecated since API Platform 2.5 and can lead to unexpected behaviors, it will not be possible anymore in API Platform 3.0.', E_USER_DEPRECATED);
}

$this->iriConverter = $iriConverter;
$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
$this->identifiersExtractor = $identifiersExtractor;
}

protected function getIriConverter(): IriConverterInterface
Expand Down Expand Up @@ -76,6 +83,10 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
if ($this->isPropertyNested($property, $resourceClass)) {
[$alias, $field, $associations] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass);
}

/**
* @var ClassMetadata
*/
$metadata = $this->getNestedMetadata($resourceClass, $associations);

$values = $this->normalizeValues((array) $value, $property);
Expand All @@ -84,7 +95,6 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
}

$caseSensitive = true;

if ($metadata->hasField($field)) {
if ('id' === $field) {
$values = array_map([$this, 'getIdFromValue'], $values);
Expand Down Expand Up @@ -134,8 +144,16 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
}

$values = array_map([$this, 'getIdFromValue'], $values);
$associationFieldIdentifier = 'id';
$doctrineTypeField = $this->getDoctrineFieldType($property, $resourceClass);

if (!$this->hasValidValues($values, $this->getDoctrineFieldType($property, $resourceClass))) {
if (null !== $this->identifiersExtractor) {
$associationResourceClass = $metadata->getAssociationTargetClass($field);
$associationFieldIdentifier = $this->identifiersExtractor->getIdentifiersFromResourceClass($associationResourceClass)[0];
$doctrineTypeField = $this->getDoctrineFieldType($associationFieldIdentifier, $associationResourceClass);
}

if (!$this->hasValidValues($values, $doctrineTypeField)) {
$this->logger->notice('Invalid filter ignored', [
'exception' => new InvalidArgumentException(sprintf('Values for field "%s" are not valid according to the doctrine type.', $field)),
]);
Expand All @@ -145,10 +163,9 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB

$association = $field;
$valueParameter = $queryNameGenerator->generateParameterName($association);

if ($metadata->isCollectionValuedAssociation($association)) {
$associationAlias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, $alias, $association);
$associationField = 'id';
$associationField = $associationFieldIdentifier;
} else {
$associationAlias = $alias;
$associationField = $field;
Expand Down
1 change: 1 addition & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@
<argument type="service" id="api_platform.property_accessor" />
<argument type="service" id="api_platform.resource_class_resolver" />
</service>
<service id="ApiPlatform\Core\Api\IdentifiersExtractorInterface" alias="api_platform.identifiers_extractor.cached" />

<service id="api_platform.identifier.converter" class="ApiPlatform\Core\Identifier\IdentifierConverter" public="false">
<argument type="service" id="api_platform.identifiers_extractor.cached" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@
<argument type="service" id="api_platform.iri_converter" />
<argument type="service" id="api_platform.property_accessor" />
<argument type="service" id="logger" on-invalid="ignore" />
<argument key="$identifiersExtractor" type="service" id="api_platform.identifiers_extractor.cached" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<argument key="$identifiersExtractor" type="service" id="api_platform.identifiers_extractor.cached" />
<argument type="service" id="api_platform.identifiers_extractor.cached" on-invalid="ignore" />

</service>

<service id="ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter\SearchFilter" alias="api_platform.doctrine_mongodb.odm.search_filter" />

<service id="api_platform.doctrine_mongodb.odm.boolean_filter" class="ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm\Filter\BooleanFilter" public="false" abstract="true">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<argument type="service" id="api_platform.iri_converter" />
<argument type="service" id="api_platform.property_accessor" />
<argument type="service" id="logger" on-invalid="ignore" />
<argument key="$identifiersExtractor" type="service" id="api_platform.identifiers_extractor.cached" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<argument key="$identifiersExtractor" type="service" id="api_platform.identifiers_extractor.cached" />
<argument type="service" id="api_platform.identifiers_extractor.cached" on-invalid="ignore" />

</service>
<service id="ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter" alias="api_platform.doctrine.orm.search_filter" />

Expand Down
1 change: 1 addition & 0 deletions src/Test/DoctrineMongoDbOdmFilterTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ protected function doTestApply(?array $properties, array $filterParameters, arra
$pipeline = $aggregationBuilder->getPipeline();
} catch (\OutOfRangeException $e) {
}

$this->assertEquals($expectedPipeline, $pipeline);
}

Expand Down
Loading