Skip to content

Commit d8db0c7

Browse files
committed
laravel fixes
1 parent e534029 commit d8db0c7

File tree

7 files changed

+26
-26
lines changed

7 files changed

+26
-26
lines changed

src/GraphQl/Type/FieldsBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ private function getFilterInstance(object|string|null $filter): ?FilterInterface
731731
}
732732

733733
if (\is_object($filter)) {
734-
return $filter;
734+
return $filter instanceof FilterInterface ? $filter : null;
735735
}
736736

737737
if (!$this->filterLocator->has($filter)) {

src/JsonApi/Filter/SparseFieldsetParameterProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function provide(Parameter $parameter, array $parameters = [], array $con
4545
}
4646

4747
foreach (explode(',', $fields) as $f) {
48-
if (\array_key_exists($f, $allowedProperties)) {
48+
if (\in_array($f, $allowedProperties, true)) {
4949
$p[] = $f;
5050
}
5151
}

src/Laravel/Eloquent/Extension/FilterQueryExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function apply(Builder $builder, array $uriVariables, Operation $operatio
6060

6161
$filter = $filterId instanceof FilterInterface ? $filterId : ($this->filterLocator->has($filterId) ? $this->filterLocator->get($filterId) : null);
6262
if ($filter instanceof FilterInterface) {
63-
$builder = $filter->apply($builder, $values, $parameter->withKey($parameter->getExtraProperties()['_query_property'] ?? $parameter->getKey()), $context + ($parameter->getFilterContext() ?? []));
63+
$builder = $filter->apply($builder, $values, $parameter, $context + ($parameter->getFilterContext() ?? []));
6464
}
6565
}
6666

src/Laravel/Eloquent/Filter/JsonApi/SortFilterParameterProvider.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function provide(Parameter $parameter, array $parameters = [], array $con
2626
}
2727

2828
$parameters = $operation->getParameters();
29-
$properties = $parameter->getExtraProperties()['_properties'] ?? [];
29+
$properties = $parameter->getProperties() ?? [];
3030
$value = $parameter->getValue();
3131

3232
// most eloquent filters work with only a single value
@@ -47,14 +47,12 @@ public function provide(Parameter $parameter, array $parameters = [], array $con
4747
$v = substr($v, 1);
4848
}
4949

50-
if (\array_key_exists($v, $properties)) {
51-
$orderBy[$properties[$v]] = $dir;
50+
if (\in_array($v, $properties, true)) {
51+
$orderBy[$v] = $dir;
5252
}
5353
}
5454

55-
$parameters->add($parameter->getKey(), $parameter->withExtraProperties(
56-
['_api_values' => $orderBy] + $parameter->getExtraProperties()
57-
));
55+
$parameters->add($parameter->getKey(), $parameter->setValue($orderBy));
5856

5957
return $operation->withParameters($parameters);
6058
}

src/Laravel/State/ParameterValidatorProvider.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,19 @@
1717
use ApiPlatform\Metadata\Operation;
1818
use ApiPlatform\State\ParameterNotFound;
1919
use ApiPlatform\State\ProviderInterface;
20-
use ApiPlatform\State\Util\ParameterParserTrait;
2120
use Illuminate\Support\Facades\Validator;
2221
use Illuminate\Validation\ValidationException;
2322
use Symfony\Component\HttpFoundation\Request;
2423

2524
/**
26-
* Validates parameters using the Symfony validator.
25+
* Validates parameters using the Laravel validator.
2726
*
2827
* @implements ProviderInterface<object>
2928
*
3029
* @experimental
3130
*/
3231
final class ParameterValidatorProvider implements ProviderInterface
3332
{
34-
use ParameterParserTrait;
3533
use ValidationErrorTrait;
3634

3735
/**
@@ -54,6 +52,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
5452
}
5553

5654
$allConstraints = [];
55+
5756
foreach ($operation->getParameters() ?? [] as $parameter) {
5857
if (!$constraints = $parameter->getConstraints()) {
5958
continue;
@@ -69,17 +68,15 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
6968
$value = null;
7069
}
7170

72-
// Basically renames our key from order[:property] to order.* to assign the rule properly (see https://laravel.com/docs/11.x/validation#rule-in)
73-
if (str_contains($key, '[:property]')) {
74-
$k = str_replace('[:property]', '', $key);
75-
$allConstraints[$k.'.*'] = $constraints;
76-
continue;
77-
}
71+
// Laravel Validator requires dot notation for nested rules (e.g., "sort.isActive"),
72+
// not nested arrays. We convert HTTP bracket syntax "sort[isActive]" to "sort.isActive".
73+
$ruleKey = str_replace(['[', ']'], ['.', ''], $key);
7874

79-
$allConstraints[$key] = $constraints;
75+
$allConstraints[$ruleKey] = $constraints;
8076
}
8177

8278
$validator = Validator::make($request->query->all(), $allConstraints);
79+
8380
if ($validator->fails()) {
8481
throw $this->getValidationError($validator, new ValidationException($validator));
8582
}

src/Laravel/Tests/Eloquent/Filter/OrderFilterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ public function testQueryParameterWithCamelCaseProperty(): void
3434
DB::enableQueryLog();
3535
$response = $this->get('/api/active_books?sort[isActive]=asc', ['Accept' => ['application/ld+json']]);
3636
$response->assertStatus(200);
37-
$this->assertEquals(\DB::getQueryLog()[1]['query'], 'select * from "active_books" order by "isActive" asc limit 30 offset 0');
37+
$this->assertEquals(\DB::getQueryLog()[1]['query'], 'select * from "active_books" order by "is_active" asc limit 30 offset 0');
3838
DB::flushQueryLog();
3939
$response = $this->get('/api/active_books?sort[isActive]=desc', ['Accept' => ['application/ld+json']]);
4040
$response->assertStatus(200);
41-
$this->assertEquals(DB::getQueryLog()[1]['query'], 'select * from "active_books" order by "isActive" desc limit 30 offset 0');
41+
$this->assertEquals(DB::getQueryLog()[1]['query'], 'select * from "active_books" order by "is_active" desc limit 30 offset 0');
4242
}
4343
}

src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use ApiPlatform\Metadata\Parameter;
2424
use ApiPlatform\Metadata\ParameterProviderFilterInterface;
2525
use ApiPlatform\Metadata\Parameters;
26+
use ApiPlatform\Metadata\PropertiesAwareInterface;
2627
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2728
use ApiPlatform\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
2829
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
@@ -173,6 +174,10 @@ private function getDefaultParameters(Operation $operation, string $resourceClas
173174

174175
['propertyNames' => $propertyNames, 'properties' => $properties] = $this->getProperties($resourceClass, $parameter);
175176

177+
if ($filter instanceof PropertiesAwareInterface) {
178+
$parameter = $parameter->withProperties($propertyNames);
179+
}
180+
176181
$parameter = $this->setDefaults($key, $parameter, $filter, $properties, $operation);
177182
// We don't do any type cast yet, a query parameter or an header is always a string or a list of strings
178183
if (null === $parameter->getNativeType()) {
@@ -250,17 +255,17 @@ private function setDefaults(string $key, Parameter $parameter, ?object $filter,
250255
$parameter = $parameter->withProperty($key);
251256
}
252257

253-
if (null === $parameter->getProperty() && $this->nameConverter && ($nameConvertedKey = $this->nameConverter->normalize($key)) && isset($properties[$nameConvertedKey])) {
254-
$parameter = $parameter->withProperty($key)->withExtraProperties(['_query_property' => $nameConvertedKey] + $parameter->getExtraProperties());
255-
$currentKey = $nameConvertedKey;
256-
}
258+
// if (null === $parameter->getProperty() && $this->nameConverter && ($nameConvertedKey = $this->nameConverter->normalize($key)) && isset($properties[$nameConvertedKey])) {
259+
// $parameter = $parameter->withProperty($key)->withExtraProperties(['_query_property' => $nameConvertedKey] + $parameter->getExtraProperties());
260+
// $currentKey = $nameConvertedKey;
261+
// }
257262

258263
if ($this->nameConverter && $property = $parameter->getProperty()) {
259264
$parameter = $parameter->withProperty($this->nameConverter->normalize($property));
260265
}
261266

262267
if (isset($properties[$currentKey]) && ($eloquentRelation = ($properties[$currentKey]->getExtraProperties()['eloquent_relation'] ?? null)) && isset($eloquentRelation['foreign_key'])) {
263-
$parameter = $parameter->withExtraProperties(['_query_property' => $eloquentRelation['foreign_key']] + $parameter->getExtraProperties());
268+
$parameter = $parameter->withProperty($eloquentRelation['foreign_key']);
264269
}
265270

266271
$parameter = $this->addFilterMetadata($parameter);

0 commit comments

Comments
 (0)