Skip to content

Commit

Permalink
backup
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Mar 21, 2024
1 parent ed7a9e6 commit bc42359
Show file tree
Hide file tree
Showing 30 changed files with 111 additions and 87 deletions.
7 changes: 4 additions & 3 deletions src/Doctrine/Orm/Extension/ParameterExtension.php
Expand Up @@ -14,7 +14,6 @@
namespace ApiPlatform\Doctrine\Orm\Extension;

use ApiPlatform\Doctrine\Orm\Filter\FilterInterface;
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use ApiPlatform\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\HeaderParameterInterface;
Expand All @@ -31,7 +30,8 @@ public function __construct(private readonly ContainerInterface $filterLocator)
{
}

private function applyFilter(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, ?string $resourceClass = null, ?Operation $operation = null, array $context = []) {
private function applyFilter(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, ?string $resourceClass = null, ?Operation $operation = null, array $context = []): void
{
if (!($request = $context['request'] ?? null)) {
return;

Check warning on line 36 in src/Doctrine/Orm/Extension/ParameterExtension.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Orm/Extension/ParameterExtension.php#L36

Added line #L36 was not covered by tests
}
Expand All @@ -51,9 +51,10 @@ private function applyFilter(QueryBuilder $queryBuilder, QueryNameGeneratorInter
continue;

Check warning on line 51 in src/Doctrine/Orm/Extension/ParameterExtension.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Orm/Extension/ParameterExtension.php#L49-L51

Added lines #L49 - L51 were not covered by tests
}

$value = $parameters[$key];
$filter = $this->filterLocator->has($filterId) ? $this->filterLocator->get($filterId) : null;
if ($filter instanceof FilterInterface) {
$filter->apply($queryBuilder, $queryNameGenerator, $resourceClass, $operation, ['filters' => [$parameter->getProperty() ?? $key => $parameters[$key]]] + $context);
$filter->apply($queryBuilder, $queryNameGenerator, $resourceClass, $operation, ['filters' => [$key => $value]] + $context);

Check warning on line 57 in src/Doctrine/Orm/Extension/ParameterExtension.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Orm/Extension/ParameterExtension.php#L54-L57

Added lines #L54 - L57 were not covered by tests
}
}
}
Expand Down
22 changes: 18 additions & 4 deletions src/Hydra/Serializer/CollectionFiltersNormalizer.php
Expand Up @@ -128,7 +128,7 @@ public function normalize(mixed $object, ?string $format = null, array $context
}
}

if ($currentFilters || $parameters) {
if ($currentFilters || ($parameters && \count($parameters))) {
$data['hydra:search'] = $this->getSearch($resourceClass, $requestParts, $currentFilters, $parameters);
}

Expand All @@ -151,7 +151,7 @@ public function setNormalizer(NormalizerInterface $normalizer): void
* @param LegacyFilterInterface[]|FilterInterface[] $filters
* @param array<string, Parameter> $parameters
*/
private function getSearch(string $resourceClass, array $parts, array $filters, null|array|Parameters $parameters): array
private function getSearch(string $resourceClass, array $parts, array $filters, array|Parameters|null $parameters): array
{
$variables = [];
$mapping = [];
Expand All @@ -164,12 +164,26 @@ private function getSearch(string $resourceClass, array $parts, array $filters,

foreach ($parameters ?? [] as $key => $parameter) {
// Each IriTemplateMapping maps a variable used in the template to a property
if (!$parameter instanceof QueryParameterInterface || !($property = $parameter->getProperty())) {
if (!$parameter instanceof QueryParameterInterface) {
continue;

Check warning on line 168 in src/Hydra/Serializer/CollectionFiltersNormalizer.php

View check run for this annotation

Codecov / codecov/patch

src/Hydra/Serializer/CollectionFiltersNormalizer.php#L167-L168

Added lines #L167 - L168 were not covered by tests
}

if (!($property = $parameter->getProperty()) && ($filterId = $parameter->getFilter())) {
$filter = $this->getFilter($filterId);
foreach ($filter->getDescription($resourceClass) as $variable => $description) {
$variables[] = $variable;
$m = ['@type' => 'IriTemplateMapping', 'variable' => $variable, 'property' => $description['property'], 'required' => $description['required']];
if (null !== ($required = $parameter->getRequired())) {
$m['required'] = $required;

Check warning on line 177 in src/Hydra/Serializer/CollectionFiltersNormalizer.php

View check run for this annotation

Codecov / codecov/patch

src/Hydra/Serializer/CollectionFiltersNormalizer.php#L171-L177

Added lines #L171 - L177 were not covered by tests
}
$mapping[] = $m;

Check warning on line 179 in src/Hydra/Serializer/CollectionFiltersNormalizer.php

View check run for this annotation

Codecov / codecov/patch

src/Hydra/Serializer/CollectionFiltersNormalizer.php#L179

Added line #L179 was not covered by tests
}

continue;

Check warning on line 182 in src/Hydra/Serializer/CollectionFiltersNormalizer.php

View check run for this annotation

Codecov / codecov/patch

src/Hydra/Serializer/CollectionFiltersNormalizer.php#L182

Added line #L182 was not covered by tests
}

$variables[] = $key;
$m = ['@type' => 'IriTemplateMapping', 'variable' => $key, 'property' => $property];
$variables[] = $key;
if (null !== ($required = $parameter->getRequired())) {
$m['required'] = $required;

Check warning on line 188 in src/Hydra/Serializer/CollectionFiltersNormalizer.php

View check run for this annotation

Codecov / codecov/patch

src/Hydra/Serializer/CollectionFiltersNormalizer.php#L185-L188

Added lines #L185 - L188 were not covered by tests
}
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/ApiResource.php
Expand Up @@ -960,7 +960,7 @@ public function __construct(
$provider = null,
$processor = null,
protected ?OptionsInterface $stateOptions = null,
protected null|array|Parameters $parameters = null,
protected array|Parameters|null $parameters = null,
protected array $extraProperties = [],
) {
parent::__construct(
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Delete.php
Expand Up @@ -94,7 +94,7 @@ public function __construct(
$provider = null,
$processor = null,
?OptionsInterface $stateOptions = null,
null|array|Parameters $parameters = null,
array|Parameters|null $parameters = null,
array $extraProperties = [],
) {
parent::__construct(
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Extractor/XmlResourceExtractor.php
Expand Up @@ -535,7 +535,7 @@ private function buildParameters(\SimpleXMLElement $resource): ?array
property: $this->phpize($parameter, 'property', 'string'),
description: $this->phpize($parameter, 'description', 'string'),
priority: $this->phpize($parameter, 'priority', 'integer'),
extraProperties: $this->buildExtraProperties($parameter, 'extraProperties'),
extraProperties: $this->buildExtraProperties($parameter, 'extraProperties') ?? [],
);

Check warning on line 539 in src/Metadata/Extractor/XmlResourceExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Extractor/XmlResourceExtractor.php#L510-L539

Added lines #L510 - L539 were not covered by tests
}

Expand Down
4 changes: 2 additions & 2 deletions src/Metadata/Extractor/YamlResourceExtractor.php
Expand Up @@ -470,7 +470,7 @@ private function buildParameters(array $resource): ?array
key: $key,
required: $this->phpize($parameter, 'required', 'bool'),
schema: $parameter['schema'],
openapi: ($parameter['openapi'] ?? null) ? new Parameter(
openApi: ($parameter['openapi'] ?? null) ? new Parameter(
name: $parameter['openapi']['name'],
in: $parameter['in'] ?? 'query',
description: $parameter['openapi']['description'] ?? '',
Expand All @@ -490,7 +490,7 @@ private function buildParameters(array $resource): ?array
property: $this->phpize($parameter, 'property', 'string'),
description: $this->phpize($parameter, 'description', 'string'),
priority: $this->phpize($parameter, 'priority', 'integer'),
extraProperties: $this->buildArrayValue($resource, 'extraProperties'),
extraProperties: $this->buildArrayValue($parameter, 'extraProperties') ?? [],
);

Check warning on line 494 in src/Metadata/Extractor/YamlResourceExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Extractor/YamlResourceExtractor.php#L466-L494

Added lines #L466 - L494 were not covered by tests
}

Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Get.php
Expand Up @@ -94,7 +94,7 @@ public function __construct(
$provider = null,
$processor = null,
?OptionsInterface $stateOptions = null,
null|array|Parameters $parameters = null,
array|Parameters|null $parameters = null,
array $extraProperties = [],
) {
parent::__construct(
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/GetCollection.php
Expand Up @@ -94,7 +94,7 @@ public function __construct(
$provider = null,
$processor = null,
?OptionsInterface $stateOptions = null,
null|array|Parameters $parameters = null,
array|Parameters|null $parameters = null,
array $extraProperties = [],
private ?string $itemUriTemplate = null,
) {
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/GraphQl/Operation.php
Expand Up @@ -85,7 +85,7 @@ public function __construct(
$provider = null,
$processor = null,
?OptionsInterface $stateOptions = null,
null|array|Parameters $parameters = null,
array|Parameters|null $parameters = null,
array $extraProperties = []
) {
parent::__construct(
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/GraphQl/Query.php
Expand Up @@ -69,7 +69,7 @@ public function __construct(
$provider = null,
$processor = null,
?OptionsInterface $stateOptions = null,
null|array|Parameters $parameters = null,
array|Parameters|null $parameters = null,
array $extraProperties = [],

protected ?bool $nested = null,
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/GraphQl/QueryCollection.php
Expand Up @@ -70,7 +70,7 @@ public function __construct(
$provider = null,
$processor = null,
protected ?OptionsInterface $stateOptions = null,
null|array|Parameters $parameters = null,
array|Parameters|null $parameters = null,
array $extraProperties = [],

?bool $nested = null,
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/GraphQl/Subscription.php
Expand Up @@ -69,7 +69,7 @@ public function __construct(
$provider = null,
$processor = null,
?OptionsInterface $stateOptions = null,
null|array|Parameters $parameters = null,
array|Parameters|null $parameters = null,
array $extraProperties = [],
) {
parent::__construct(
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/HttpOperation.php
Expand Up @@ -200,7 +200,7 @@ public function __construct(
$provider = null,
$processor = null,
?OptionsInterface $stateOptions = null,
null|array|Parameters $parameters = null,
array|Parameters|null $parameters = null,
array $extraProperties = [],
) {
parent::__construct(
Expand Down
22 changes: 11 additions & 11 deletions src/Metadata/Metadata.php
Expand Up @@ -21,15 +21,15 @@
abstract class Metadata
{
/**
* @param string|null $deprecationReason https://api-platform.com/docs/core/deprecations/#deprecating-resource-classes-operations-and-properties
* @param string|null $security https://api-platform.com/docs/core/security
* @param string|null $securityPostDenormalize https://api-platform.com/docs/core/security/#executing-access-control-rules-after-denormalization
* @param mixed|null $mercure
* @param mixed|null $messenger
* @param mixed|null $input
* @param mixed|null $output
* @param mixed|null $provider
* @param mixed|null $processor
* @param string|null $deprecationReason https://api-platform.com/docs/core/deprecations/#deprecating-resource-classes-operations-and-properties
* @param string|null $security https://api-platform.com/docs/core/security
* @param string|null $securityPostDenormalize https://api-platform.com/docs/core/security/#executing-access-control-rules-after-denormalization
* @param mixed|null $mercure
* @param mixed|null $messenger
* @param mixed|null $input
* @param mixed|null $output
* @param mixed|null $provider
* @param mixed|null $processor
* @param Parameters|array<string, Parameter> $parameters
*/
public function __construct(
Expand Down Expand Up @@ -73,7 +73,7 @@ public function __construct(
/**
* @experimental
*/
protected null|array|Parameters $parameters = [],
protected array|Parameters|null $parameters = [],
protected array $extraProperties = []
) {
}
Expand Down Expand Up @@ -574,7 +574,7 @@ public function withStateOptions(?OptionsInterface $stateOptions): static
/**
* @return array<string, Parameter>
*/
public function getParameters(): null|array|Parameters
public function getParameters(): array|Parameters|null
{
return $this->parameters;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Operation.php
Expand Up @@ -806,7 +806,7 @@ public function __construct(
protected $provider = null,
protected $processor = null,
protected ?OptionsInterface $stateOptions = null,
protected null|array|Parameters $parameters = [],
protected array|Parameters|null $parameters = [],
protected array $extraProperties = [],
) {
parent::__construct(
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Parameter.php
Expand Up @@ -37,7 +37,7 @@ public function __construct(
protected ?string $description = null,
protected ?bool $required = null,
protected ?int $priority = null,
protected array $extraProperties = [],
protected ?array $extraProperties = [],
) {
}

Expand Down
5 changes: 5 additions & 0 deletions src/Metadata/Parameters.php
Expand Up @@ -15,6 +15,8 @@

/**
* An parameter dictionnary.
*
* @implements \IteratorAggregate<string, Parameter>
*/
final class Parameters implements \IteratorAggregate, \Countable
{
Expand All @@ -36,6 +38,9 @@ public function __construct(array $parameters = [])
$this->sort();
}

/**
* @return \ArrayIterator<string, Parameter>
*/
public function getIterator(): \Traversable
{
return (function (): \Generator {
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Patch.php
Expand Up @@ -94,7 +94,7 @@ public function __construct(
$provider = null,
$processor = null,
?OptionsInterface $stateOptions = null,
null|array|Parameters $parameters = null,
array|Parameters|null $parameters = null,
array $extraProperties = [],
) {
parent::__construct(
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Post.php
Expand Up @@ -94,7 +94,7 @@ public function __construct(
$provider = null,
$processor = null,
?OptionsInterface $stateOptions = null,
null|array|Parameters $parameters = null,
array|Parameters|null $parameters = null,
array $extraProperties = [],
private ?string $itemUriTemplate = null
) {
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Put.php
Expand Up @@ -94,7 +94,7 @@ public function __construct(
$provider = null,
$processor = null,
?OptionsInterface $stateOptions = null,
null|array|Parameters $parameters = null,
array|Parameters|null $parameters = null,
array $extraProperties = [],
private ?bool $allowCreate = null,
) {
Expand Down
Expand Up @@ -19,7 +19,6 @@
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\OpenApi;
use ApiPlatform\Serializer\Filter\FilterInterface as SerializerFilterInterface;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\WithParameter;
use Psr\Container\ContainerInterface;

/**
Expand Down Expand Up @@ -57,11 +56,11 @@ public function create(string $resourceClass): ResourceMetadataCollection

// Read filter description to populate the Parameter
$description = $filter instanceof FilterInterface ? $filter->getDescription($resourceClass) : [];
if (($schema = $description['schema'] ?? null) && null === $parameter->getSchema()) {
if (($schema = $description[$key]['schema'] ?? null) && null === $parameter->getSchema()) {
$parameter = $parameter->withSchema($schema);

Check warning on line 60 in src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php#L60

Added line #L60 was not covered by tests
}

if (null === $parameter->getOpenApi() && $openApi = $description['openapi'] ?? null) {
if (null === $parameter->getOpenApi() && $openApi = $description[$key]['openapi'] ?? null) {
if ($openApi instanceof OpenApi\Model\Parameter) {
$parameter = $parameter->withOpenApi($openApi);

Check warning on line 65 in src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Resource/Factory/ParameterResourceMetadataCollectionFactory.php#L64-L65

Added lines #L64 - L65 were not covered by tests
}
Expand All @@ -70,8 +69,8 @@ public function create(string $resourceClass): ResourceMetadataCollection
$parameter->withOpenApi(new OpenApi\Model\Parameter(
$key,
$parameter instanceof HeaderParameterInterface ? 'header' : 'query',
$description['description'] ?? '',
$description['required'] ?? $openApi['required'] ?? false,
$description[$key]['description'] ?? '',
$description[$key]['required'] ?? $openApi['required'] ?? false,
$openApi['deprecated'] ?? false,
$openApi['allowEmptyValue'] ?? true,
$schema,
Expand All @@ -97,25 +96,4 @@ public function create(string $resourceClass): ResourceMetadataCollection

return $resourceMetadataCollection;
}

/**
* @return Iterable<string, Parameter>
*/
private function getIterator(null|array|\SplPriorityQueue $parameters): Iterable {
if (!$parameters) {
return [];
}

if (is_array($parameters)) {
foreach ($parameters as $key => $parameter) {
yield $key => $parameter;
}

return $parameters;
}

foreach ($parameters as $priority => $parameter) {
yield $parameter->getKey() => $parameter->withPriority($priority);
}
}
}
2 changes: 1 addition & 1 deletion src/Metadata/Tests/Extractor/XmlExtractorTest.php
Expand Up @@ -386,7 +386,7 @@ public function testValidXML(): void
schema: [
'type' => 'string',
],
extraProperties: []
extraProperties: ['foo' => 'bar']
),
],
],
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Tests/Extractor/YamlExtractorTest.php
Expand Up @@ -405,7 +405,7 @@ public function testValidYaml(): void
'stateOptions' => null,
'links' => null,
'headers' => ['hello' => 'world'],
'parameters' => ['author' => new QueryParameter(schema: ['type' => 'string'], required: true, key: 'author')],
'parameters' => ['author' => new QueryParameter(schema: ['type' => 'string'], required: true, key: 'author', description: 'hello')],
],
],
'graphQlOperations' => null,
Expand Down
4 changes: 2 additions & 2 deletions src/OpenApi/Factory/OpenApiFactory.php
Expand Up @@ -308,9 +308,9 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
}

$openapiParameters = $openapiOperation->getParameters();
foreach ($operation->getParameters() ?? [] as $p) {
foreach ($operation->getParameters() ?? [] as $key => $p) {
$in = $p instanceof HeaderParameterInterface ? 'header' : 'query';
$parameter = new Parameter($p->getKey(), $in, $p->getDescription() ?? "$resourceShortName $key", $p->getRequired() ?? false, false, false, $p->getSchema() ?? ['type' => 'string']);
$parameter = new Parameter($key, $in, $p->getDescription() ?? "$resourceShortName $key", $p->getRequired() ?? false, false, false, $p->getSchema() ?? ['type' => 'string']);

if ($linkParameter = $p->getOpenApi()) {
$parameter = $this->mergeParameter($parameter, $linkParameter);

Check warning on line 316 in src/OpenApi/Factory/OpenApiFactory.php

View check run for this annotation

Codecov / codecov/patch

src/OpenApi/Factory/OpenApiFactory.php#L316

Added line #L316 was not covered by tests
Expand Down

0 comments on commit bc42359

Please sign in to comment.