forked from api-platform/core
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(laravel): jsonapi query parameters (page, sort, fields and include)
- Loading branch information
Showing
12 changed files
with
310 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\JsonApi\Filter; | ||
|
||
use ApiPlatform\Metadata\JsonSchemaFilterInterface; | ||
use ApiPlatform\Metadata\OpenApiParameterFilterInterface; | ||
use ApiPlatform\Metadata\Parameter as MetadataParameter; | ||
use ApiPlatform\Metadata\ParameterProviderFilterInterface; | ||
use ApiPlatform\Metadata\PropertiesFilterInterface; | ||
use ApiPlatform\Metadata\QueryParameter; | ||
use ApiPlatform\OpenApi\Model\Parameter; | ||
|
||
final class SparseFieldset implements OpenApiParameterFilterInterface, JsonSchemaFilterInterface, ParameterProviderFilterInterface, PropertiesFilterInterface | ||
{ | ||
public function getSchema(MetadataParameter $parameter): array | ||
{ | ||
return [ | ||
'type' => 'array', | ||
'items' => [ | ||
'type' => 'string', | ||
], | ||
]; | ||
} | ||
|
||
public function getOpenApiParameters(MetadataParameter $parameter): Parameter|array|null | ||
{ | ||
$example = \sprintf( | ||
'%1$s[]={propertyName}&%1$s[]={anotherPropertyName}', | ||
$parameter->getKey() | ||
); | ||
|
||
return new Parameter( | ||
name: $parameter->getKey().'[]', | ||
in: $parameter instanceof QueryParameter ? 'query' : 'header', | ||
description: 'Allows you to reduce the response to contain only the properties you need. If your desired property is nested, you can address it using nested arrays. Example: '.$example | ||
); | ||
} | ||
|
||
public static function getParameterProvider(): string | ||
{ | ||
return SparseFieldsetParameterProvider::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\JsonApi\Filter; | ||
|
||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\Metadata\Parameter; | ||
use ApiPlatform\State\ParameterProviderInterface; | ||
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; | ||
|
||
final readonly class SparseFieldsetParameterProvider implements ParameterProviderInterface | ||
{ | ||
public function provide(Parameter $parameter, array $parameters = [], array $context = []): ?Operation | ||
{ | ||
if (!($operation = $context['operation'] ?? null)) { | ||
return null; | ||
} | ||
|
||
$allowedProperties = $parameter->getExtraProperties()['_properties'] ?? []; | ||
$value = $parameter->getValue(); | ||
$normalizationContext = $operation->getNormalizationContext(); | ||
|
||
if (!\is_array($value)) { | ||
return null; | ||
} | ||
|
||
$properties = []; | ||
$shortName = strtolower($operation->getShortName()); | ||
foreach ($value as $resource => $fields) { | ||
if (strtolower($resource) === $shortName) { | ||
$p = &$properties; | ||
} else { | ||
$properties[$resource] = []; | ||
$p = &$properties[$resource]; | ||
} | ||
|
||
foreach (explode(',', $fields) as $f) { | ||
if (\array_key_exists($f, $allowedProperties)) { | ||
$p[] = $f; | ||
} | ||
} | ||
} | ||
|
||
if (isset($normalizationContext[AbstractNormalizer::ATTRIBUTES])) { | ||
$properties = array_merge_recursive((array) $normalizationContext[AbstractNormalizer::ATTRIBUTES], $properties); | ||
} | ||
|
||
$normalizationContext[AbstractNormalizer::ATTRIBUTES] = $properties; | ||
|
||
return $operation->withNormalizationContext($normalizationContext); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\Laravel\JsonApi\State; | ||
|
||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\State\ProviderInterface; | ||
|
||
/** | ||
* This is a copy of ApiPlatform\JsonApi\State\JsonApiProvider without the support of sort,filter and fields as these should be implemented using QueryParameters and specific Filters. | ||
* At some point we want to merge both classes but for now we don't have the SortFilter inside Symfony. | ||
* | ||
* @internal | ||
*/ | ||
final class JsonApiProvider implements ProviderInterface | ||
{ | ||
public function __construct(private readonly ProviderInterface $decorated) | ||
{ | ||
} | ||
|
||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null | ||
{ | ||
$request = $context['request'] ?? null; | ||
|
||
if (!$request || 'jsonapi' !== $request->getRequestFormat()) { | ||
return $this->decorated->provide($operation, $uriVariables, $context); | ||
} | ||
|
||
$filters = $request->attributes->get('_api_filters', []); | ||
$queryParameters = $request->query->all(); | ||
|
||
$pageParameter = $queryParameters['page'] ?? null; | ||
if ( | ||
\is_array($pageParameter) | ||
) { | ||
$filters = array_merge($pageParameter, $filters); | ||
} | ||
|
||
if (isset($pageParameter['offset'])) { | ||
$filters['page'] = $pageParameter['offset']; | ||
unset($filters['offset']); | ||
} | ||
|
||
$includeParameter = $queryParameters['include'] ?? null; | ||
|
||
if ($includeParameter) { | ||
$request->attributes->set('_api_included', explode(',', $includeParameter)); | ||
} | ||
|
||
if ($filters) { | ||
$request->attributes->set('_api_filters', $filters); | ||
} | ||
|
||
return $this->decorated->provide($operation, $uriVariables, $context); | ||
} | ||
} |
Oops, something went wrong.