Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Metadata/HeaderParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace ApiPlatform\Metadata;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE | \Attribute::TARGET_PROPERTY)]
class HeaderParameter extends Parameter implements HeaderParameterInterface
{
}
2 changes: 1 addition & 1 deletion src/Metadata/QueryParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace ApiPlatform\Metadata;

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE | \Attribute::TARGET_PROPERTY)]
class QueryParameter extends Parameter implements QueryParameterInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ private function getDefaultParameters(Operation $operation, string $resourceClas
$propertyNames = $properties = [];
$parameters = $operation->getParameters() ?? new Parameters();

foreach ($this->createParametersFromAttributes($operation) as $key => $parameter) {
$parameters->add($key, $parameter);
}

// First loop we look for the :property placeholder and replace its key
foreach ($parameters as $key => $parameter) {
if (!str_contains($key, ':property')) {
Expand Down Expand Up @@ -469,4 +473,38 @@ private function getFilterInstance(object|string|null $filter): ?object

return $this->filterLocator->get($filter);
}

private function createParametersFromAttributes(Operation $operation): Parameters
{
$parameters = new Parameters();

if (null === $resourceClass = $operation->getClass()) {
return $parameters;
}

foreach ((new \ReflectionClass($resourceClass))->getProperties() as $reflectionProperty) {
foreach ($reflectionProperty->getAttributes(Parameter::class, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
$parameter = $attribute->newInstance();

$propertyName = $reflectionProperty->getName();
$key = $parameter->getKey() ?? $propertyName;

if (null === $parameterPropertyName = $parameter->getProperty()) {
$parameter = $parameter->withProperty($propertyName);
} elseif ($parameterPropertyName !== $propertyName) {
throw new RuntimeException(\sprintf('Parameter attribute on property "%s" must target itself or have no explicit property. Got "property: \'%s\'" instead.', $propertyName, $parameterPropertyName));
}

if (null === ($parameterProperties = $parameter->getProperties()) || \in_array($propertyName, $parameterProperties, true)) {
$parameter = $parameter->withProperties([$propertyName]);
} elseif (!\in_array($propertyName, $parameterProperties, true)) {
throw new RuntimeException(\sprintf('Parameter attribute on property "%s" must target itself or have no explicit properties. Got "properties: [%s]" instead.', $propertyName, implode(', ', $parameterProperties)));
}

$parameters->add($key, $parameter);
}
}

return $parameters;
}
}

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions tests/Fixtures/TestBundle/Document/ParameterOnProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Tests\Fixtures\TestBundle\Document;

use ApiPlatform\Doctrine\Odm\Filter\PartialSearchFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

#[ODM\Document]
#[ApiResource(
uriTemplate: 'parameter_on_properties',
operations: [
new GetCollection(),
new Get(),
]
)]
class ParameterOnProperties
{
#[ODM\Id]
private ?string $id = null;

#[ODM\Field(type: 'string')]
#[QueryParameter(key: 'qname', filter: new PartialSearchFilter())]
private string $name = '';

#[ODM\Field(type: 'string', nullable: true)]
private ?string $description = null;

public function __construct(string $name = '', ?string $description = null)
{
$this->name = $name;
$this->description = $description;
}

public function getId(): ?string
{
return $this->id;
}

public function getName(): string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

public function getDescription(): ?string
{
return $this->description;
}

public function setDescription(?string $description): self
{
$this->description = $description;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Tests\Fixtures\TestBundle\Document;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\HeaderParameter;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

#[ODM\Document]
#[ApiResource(
uriTemplate: 'parameter_on_properties_with_header_parameter',
operations: [
new GetCollection(),
new Get(),
]
)]
class ParameterOnPropertiesWithHeaderParameter
{
#[ODM\Id]
private ?string $id = null;

#[ODM\Field(type: 'string')]
#[HeaderParameter(key: 'X-Authorization', description: 'Authorization header')]
public string $authToken = '';

public function __construct(string $authToken = '')
{
$this->authToken = $authToken;
}

public function getId(): ?string
{
return $this->id;
}
}
79 changes: 79 additions & 0 deletions tests/Fixtures/TestBundle/Entity/ParameterOnProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Tests\Fixtures\TestBundle\Entity;

use ApiPlatform\Doctrine\Orm\Filter\PartialSearchFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ApiResource(
uriTemplate: 'parameter_on_properties',
operations: [
new GetCollection(),
new Get(),
]
)]
class ParameterOnProperties
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;

#[ORM\Column(type: 'string')]
#[QueryParameter(key: 'qname', filter: new PartialSearchFilter())]
private string $name = '';

#[ORM\Column(type: 'string', nullable: true)]
private ?string $description = null;

public function __construct(string $name = '', ?string $description = null)
{
$this->name = $name;
$this->description = $description;
}

public function getId(): ?int
{
return $this->id;
}

public function getName(): string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}

public function getDescription(): ?string
{
return $this->description;
}

public function setDescription(?string $description): self
{
$this->description = $description;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Tests\Fixtures\TestBundle\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\HeaderParameter;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity]
#[ApiResource(
uriTemplate: 'parameter_on_properties_with_header_parameter',
operations: [
new GetCollection(),
new Get(),
]
)]
class ParameterOnPropertiesWithHeaderParameter
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;

#[ORM\Column(type: 'string', name: 'auth_token')]
#[HeaderParameter(key: 'X-Authorization', description: 'Authorization header')]
public string $authToken = '';

public function __construct(string $authToken = '')
{
$this->authToken = $authToken;
}

public function getId(): ?int
{
return $this->id;
}
}
Loading
Loading