Skip to content

Commit d35e46b

Browse files
authored
fix(hydra): "property" may not be defined (#7293)
fixes #7290
1 parent 543e575 commit d35e46b

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

src/Hydra/Serializer/CollectionFiltersNormalizer.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,20 @@ private function getSearch(string $resourceClass, array $parts, array $filters,
152152
continue;
153153
}
154154

155-
if (($prop = $parameter->getProperty()) && ($description['property'] ?? null) !== $prop) {
155+
if (!($descriptionProperty = $description['property'] ?? null)) {
156+
continue;
157+
}
158+
159+
if (($prop = $parameter->getProperty()) && $descriptionProperty !== $prop) {
156160
continue;
157161
}
158162

159163
// :property is a pattern allowed when defining parameters
160-
$k = str_replace(':property', $description['property'], $key);
161-
$variable = str_replace($description['property'], $k, $variable);
164+
$k = str_replace(':property', $descriptionProperty, $key);
165+
$variable = str_replace($descriptionProperty, $k, $variable);
162166
$variables[] = $variable;
163-
$m = ['@type' => 'IriTemplateMapping', 'variable' => $variable, 'property' => $description['property'], 'required' => $description['required']];
164-
if (null !== ($required = $parameter->getRequired())) {
167+
$m = ['@type' => 'IriTemplateMapping', 'variable' => $variable, 'property' => $descriptionProperty];
168+
if (null !== ($required = $parameter->getRequired() ?? $description['required'] ?? null)) {
165169
$m['required'] = $required;
166170
}
167171
$mapping[] = $m;

tests/Fixtures/TestBundle/ApiResource/WithParameter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,14 @@
267267
],
268268
provider: [self::class, 'noopProvider'],
269269
)]
270+
#[GetCollection(
271+
uriTemplate: 'with_parameters_filter_without_property{._format}',
272+
parameters: [
273+
'myParam' => new QueryParameter(filter: 'some_custom_filter_without_description'),
274+
],
275+
provider: [self::class, 'collectionProvider'],
276+
)]
277+
270278
#[QueryParameter(key: 'everywhere')]
271279
class WithParameter
272280
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\Tests\Fixtures\TestBundle\Filter;
15+
16+
use ApiPlatform\Metadata\FilterInterface;
17+
18+
class NoDescriptionFilter implements FilterInterface
19+
{
20+
public function getDescription(string $resourceClass): array
21+
{
22+
return ['a' => ['required' => true]];
23+
}
24+
}

tests/Fixtures/app/config/config_doctrine.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ services:
1616
arguments: [ { 'id': ~, 'name': 'desc', 'description': ~, 'relatedDummy.name': ~, 'embeddedDummy.dummyName': 'desc', 'relatedDummy.symfony': ~, 'dummyDate': ~} ]
1717
tags: [ { name: 'api_platform.filter', id: 'my_dummy.order' } ]
1818

19+
app.some_custom_filter_without_description:
20+
class: 'ApiPlatform\Tests\Fixtures\TestBundle\Filter\NoDescriptionFilter'
21+
tags: [ { name: 'api_platform.filter', id: 'some_custom_filter_without_description' } ]
22+
1923
app.my_dummy_resource.date_filter:
2024
parent: 'api_platform.doctrine.orm.date_filter'
2125
arguments: [ { 'dummyDate': ~, 'relatedDummy.dummyDate': ~, 'embeddedDummy.dummyDate': ~ } ]

tests/Functional/Parameters/HydraTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,14 @@ public function testHydraTemplate(): void
4141
],
4242
]], $response->toArray());
4343
}
44+
45+
public function testNoPropertyDescription(): void
46+
{
47+
$response = self::createClient()->request('GET', 'with_parameters_filter_without_property.jsonld');
48+
$this->assertArraySubset(['hydra:search' => [
49+
'hydra:template' => '/with_parameters_filter_without_property.jsonld{?}',
50+
'hydra:mapping' => [
51+
],
52+
]], $response->toArray());
53+
}
4454
}

0 commit comments

Comments
 (0)