• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.97
/src/Doctrine/Orm/Filter/ExistsFilter.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Doctrine\Orm\Filter;
15

16
use ApiPlatform\Doctrine\Common\Filter\ExistsFilterInterface;
17
use ApiPlatform\Doctrine\Common\Filter\ExistsFilterTrait;
18
use ApiPlatform\Doctrine\Common\Filter\PropertyPlaceholderOpenApiParameterTrait;
19
use ApiPlatform\Doctrine\Orm\Util\QueryBuilderHelper;
20
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
21
use ApiPlatform\Metadata\JsonSchemaFilterInterface;
22
use ApiPlatform\Metadata\OpenApiParameterFilterInterface;
23
use ApiPlatform\Metadata\Operation;
24
use ApiPlatform\Metadata\Parameter;
25
use Doctrine\ORM\Mapping\AssociationMapping;
26
use Doctrine\ORM\Mapping\ClassMetadata;
27
use Doctrine\ORM\Mapping\ManyToManyOwningSideMapping;
28
use Doctrine\ORM\Mapping\ToOneOwningSideMapping;
29
use Doctrine\ORM\Query\Expr\Join;
30
use Doctrine\ORM\QueryBuilder;
31
use Doctrine\Persistence\ManagerRegistry;
32
use Psr\Log\LoggerInterface;
33
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
34

35
/**
36
 * The exists filter allows you to select items based on a nullable field value. It will also check the emptiness of a collection association.
37
 *
38
 * Syntax: `?exists[property]=<true|false|1|0>`.
39
 *
40
 * <div data-code-selector>
41
 *
42
 * ```php
43
 * <?php
44
 * // api/src/Entity/Book.php
45
 * use ApiPlatform\Metadata\ApiFilter;
46
 * use ApiPlatform\Metadata\ApiResource;
47
 * use ApiPlatform\Doctrine\Orm\Filter\ExistFilter;
48
 *
49
 * #[ApiResource]
50
 * #[ApiFilter(ExistFilter::class, properties: ['comment'])]
51
 * class Book
52
 * {
53
 *     // ...
54
 * }
55
 * ```
56
 *
57
 * ```yaml
58
 * # config/services.yaml
59
 * services:
60
 *     book.exist_filter:
61
 *         parent: 'api_platform.doctrine.orm.exist_filter'
62
 *         arguments: [ { comment: ~ } ]
63
 *         tags:  [ 'api_platform.filter' ]
64
 *         # The following are mandatory only if a _defaults section is defined with inverted values.
65
 *         # You may want to isolate filters in a dedicated file to avoid adding the following lines (by adding them in the defaults section)
66
 *         autowire: false
67
 *         autoconfigure: false
68
 *         public: false
69
 *
70
 * # api/config/api_platform/resources.yaml
71
 * resources:
72
 *     App\Entity\Book:
73
 *         - operations:
74
 *               ApiPlatform\Metadata\GetCollection:
75
 *                   filters: ['book.exist_filter']
76
 * ```
77
 *
78
 * ```xml
79
 * <?xml version="1.0" encoding="UTF-8" ?>
80
 * <!-- api/config/services.xml -->
81
 * <?xml version="1.0" encoding="UTF-8" ?>
82
 * <container
83
 *         xmlns="http://symfony.com/schema/dic/services"
84
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
85
 *         xsi:schemaLocation="http://symfony.com/schema/dic/services
86
 *         https://symfony.com/schema/dic/services/services-1.0.xsd">
87
 *     <services>
88
 *         <service id="book.exist_filter" parent="api_platform.doctrine.orm.exist_filter">
89
 *             <argument type="collection">
90
 *                 <argument key="comment"/>
91
 *             </argument>
92
 *             <tag name="api_platform.filter"/>
93
 *         </service>
94
 *     </services>
95
 * </container>
96
 * <!-- api/config/api_platform/resources.xml -->
97
 * <resources
98
 *         xmlns="https://api-platform.com/schema/metadata/resources-3.0"
99
 *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
100
 *         xsi:schemaLocation="https://api-platform.com/schema/metadata/resources-3.0
101
 *         https://api-platform.com/schema/metadata/resources-3.0.xsd">
102
 *     <resource class="App\Entity\Book">
103
 *         <operations>
104
 *             <operation class="ApiPlatform\Metadata\GetCollection">
105
 *                 <filters>
106
 *                     <filter>book.exist_filter</filter>
107
 *                 </filters>
108
 *             </operation>
109
 *         </operations>
110
 *     </resource>
111
 * </resources>
112
 * ```
113
 *
114
 * </div>
115
 *
116
 * Given that the collection endpoint is `/books`, you can filter books with the following query: `/books?exists[comment]=true`.
117
 *
118
 * @author Teoh Han Hui <teohhanhui@gmail.com>
119
 */
120
final class ExistsFilter extends AbstractFilter implements ExistsFilterInterface, JsonSchemaFilterInterface, OpenApiParameterFilterInterface
121
{
122
    use ExistsFilterTrait;
123
    use PropertyPlaceholderOpenApiParameterTrait;
124

125
    public function __construct(?ManagerRegistry $managerRegistry = null, ?LoggerInterface $logger = null, ?array $properties = null, string $existsParameterName = self::QUERY_PARAMETER_KEY, ?NameConverterInterface $nameConverter = null)
126
    {
UNCOV
127
        parent::__construct($managerRegistry, $logger, $properties, $nameConverter);
9✔
128

UNCOV
129
        $this->existsParameterName = $existsParameterName;
9✔
130
    }
131

132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function apply(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
136
    {
UNCOV
137
        $parameter = $context['parameter'] ?? null;
8✔
138

UNCOV
139
        if (null !== ($value = $context['filters'][$parameter?->getProperty()] ?? null)) {
8✔
UNCOV
140
            $this->filterProperty($this->denormalizePropertyName($parameter->getProperty()), $value, $queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context);
4✔
141

UNCOV
142
            return;
4✔
143
        }
144

UNCOV
145
        foreach ($context['filters'][$this->existsParameterName] ?? [] as $property => $value) {
4✔
UNCOV
146
            $this->filterProperty($this->denormalizePropertyName($property), $value, $queryBuilder, $queryNameGenerator, $resourceClass, $operation, $context);
2✔
147
        }
148
    }
149

150
    /**
151
     * {@inheritdoc}
152
     */
153
    protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
154
    {
155
        if (
UNCOV
156
            !$this->isPropertyEnabled($property, $resourceClass)
6✔
UNCOV
157
            || !$this->isPropertyMapped($property, $resourceClass, true)
6✔
UNCOV
158
            || !$this->isNullableField($property, $resourceClass)
6✔
159
        ) {
160
            return;
×
161
        }
162

UNCOV
163
        $value = $this->normalizeValue($value, $property);
6✔
UNCOV
164
        if (null === $value) {
6✔
165
            return;
×
166
        }
167

UNCOV
168
        $alias = $queryBuilder->getRootAliases()[0];
6✔
UNCOV
169
        $field = $property;
6✔
170

UNCOV
171
        $associations = [];
6✔
UNCOV
172
        if ($this->isPropertyNested($property, $resourceClass)) {
6✔
173
            [$alias, $field, $associations] = $this->addJoinsForNestedProperty($property, $alias, $queryBuilder, $queryNameGenerator, $resourceClass, Join::INNER_JOIN);
×
174
        }
UNCOV
175
        $metadata = $this->getNestedMetadata($resourceClass, $associations);
6✔
176

UNCOV
177
        if ($metadata->hasAssociation($field)) {
6✔
178
            if ($metadata->isCollectionValuedAssociation($field)) {
×
179
                $queryBuilder
×
180
                    ->andWhere(\sprintf('%s.%s %s EMPTY', $alias, $field, $value ? 'IS NOT' : 'IS'));
×
181

182
                return;
×
183
            }
184

185
            if ($metadata->isAssociationInverseSide($field)) {
×
186
                $alias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, $alias, $field, Join::LEFT_JOIN);
×
187

188
                $queryBuilder
×
189
                    ->andWhere(\sprintf('%s %s NULL', $alias, $value ? 'IS NOT' : 'IS'));
×
190

191
                return;
×
192
            }
193

194
            $queryBuilder
×
195
                ->andWhere(\sprintf('%s.%s %s NULL', $alias, $field, $value ? 'IS NOT' : 'IS'));
×
196

197
            return;
×
198
        }
199

UNCOV
200
        if ($metadata->hasField($field)) {
6✔
UNCOV
201
            $queryBuilder
6✔
UNCOV
202
                ->andWhere(\sprintf('%s.%s %s NULL', $alias, $field, $value ? 'IS NOT' : 'IS'));
6✔
203
        }
204
    }
205

206
    /**
207
     * {@inheritdoc}
208
     */
209
    protected function isNullableField(string $property, string $resourceClass): bool
210
    {
UNCOV
211
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
11✔
UNCOV
212
        $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
11✔
213

UNCOV
214
        $field = $propertyParts['field'];
11✔
215

UNCOV
216
        if ($metadata->hasAssociation($field)) {
11✔
UNCOV
217
            if ($metadata->isSingleValuedAssociation($field)) {
5✔
UNCOV
218
                if (!($metadata instanceof ClassMetadata)) {
5✔
219
                    return false;
×
220
                }
221

UNCOV
222
                $associationMapping = $metadata->getAssociationMapping($field);
5✔
223

UNCOV
224
                return $this->isAssociationNullable($associationMapping);
5✔
225
            }
226

UNCOV
227
            return true;
5✔
228
        }
229

UNCOV
230
        if ($metadata instanceof ClassMetadata && $metadata->hasField($field)) {
11✔
UNCOV
231
            return $metadata->isNullable($field);
11✔
232
        }
233

234
        return false;
×
235
    }
236

237
    /**
238
     * Determines whether an association is nullable.
239
     *
240
     * @see https://github.com/doctrine/doctrine2/blob/v2.5.4/lib/Doctrine/ORM/Tools/EntityGenerator.php#L1221-L1246
241
     */
242
    private function isAssociationNullable(AssociationMapping|array $associationMapping): bool
243
    {
UNCOV
244
        if ($associationMapping instanceof AssociationMapping) {
5✔
UNCOV
245
            if (!empty($associationMapping->id)) {
5✔
246
                return false;
×
247
            }
248

UNCOV
249
            if ($associationMapping instanceof ToOneOwningSideMapping || $associationMapping instanceof ManyToManyOwningSideMapping) {
5✔
UNCOV
250
                foreach ($associationMapping->joinColumns as $joinColumn) {
5✔
UNCOV
251
                    if (false === $joinColumn->nullable) {
5✔
252
                        return false;
×
253
                    }
254
                }
255

UNCOV
256
                return true;
5✔
257
            }
258

259
            return true;
×
260
        }
261

262
        if (!empty($associationMapping['id'])) {
×
263
            return false;
×
264
        }
265

266
        if (!isset($associationMapping['joinColumns'])) {
×
267
            return true;
×
268
        }
269

270
        $joinColumns = $associationMapping['joinColumns'];
×
271
        foreach ($joinColumns as $joinColumn) {
×
272
            if (isset($joinColumn['nullable']) && !$joinColumn['nullable']) {
×
273
                return false;
×
274
            }
275
        }
276

277
        return true;
×
278
    }
279

280
    /**
281
     * @return array<string, mixed>
282
     */
283
    public function getSchema(Parameter $parameter): array
284
    {
UNCOV
285
        return ['type' => 'boolean'];
1✔
286
    }
287
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc