Skip to content
Merged
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
16 changes: 15 additions & 1 deletion src/Metadata/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function __construct(
protected ?bool $castToArray = null,
protected ?bool $castToNativeType = null,
protected mixed $castFn = null,
protected mixed $default = null,
) {
}

Expand Down Expand Up @@ -139,7 +140,7 @@ public function getSecurityMessage(): ?string
*/
public function getValue(mixed $default = new ParameterNotFound()): mixed
{
return $this->extraProperties['_api_values'] ?? $default;
return $this->extraProperties['_api_values'] ?? $this->default ?? $default;
}

/**
Expand Down Expand Up @@ -370,4 +371,17 @@ public function withCastFn(mixed $castFn): self

return $self;
}

public function getDefault(): mixed
{
return $this->default;
}

public function withDefault(mixed $default): self
{
$self = clone $this;
$self->default = $default;

return $self;
}
}
30 changes: 27 additions & 3 deletions src/Metadata/Tests/ParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,40 @@

namespace ApiPlatform\Metadata\Tests;

use ApiPlatform\Metadata\Parameter;
use ApiPlatform\Metadata\QueryParameter;
use ApiPlatform\State\ParameterNotFound;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class ParameterTest extends TestCase
{
public function testDefaultValue(): void
{
$parameter = new QueryParameter();
$this->assertSame('test', $parameter->getValue('test'));
$this->assertInstanceOf(ParameterNotFound::class, $parameter->getValue());
$this->assertInstanceOf(ParameterNotFound::class, (new QueryParameter())->getValue());
}

#[DataProvider('provideDefaultValueCases')]
public function testDefaultValueWithFallbackValue(Parameter $parameter, mixed $fallbackValue, mixed $expectedDefault): void
{
$this->assertSame($expectedDefault, $parameter->getValue($fallbackValue));
}

/** @return iterable<array{Parameter, mixed, mixed}> */
public static function provideDefaultValueCases(): iterable
{
$fallbackValue = new ParameterNotFound();

yield 'no default specified' => [
new QueryParameter(), $fallbackValue, $fallbackValue,
];

yield 'with a default specified' => [
new QueryParameter(default: false), $fallbackValue, false,
];

yield 'with null as default' => [
new QueryParameter(default: null), $fallbackValue, $fallbackValue,
];
}
}
7 changes: 6 additions & 1 deletion src/OpenApi/Factory/OpenApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,19 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
}

$in = $p instanceof HeaderParameterInterface ? 'header' : 'query';
$defaultSchema = ['type' => 'string'];
if (null !== $p->getDefault()) {
$defaultSchema['default'] = $p->getDefault();
}

$defaultParameter = new Parameter(
$key,
$in,
$p->getDescription() ?? "$resourceShortName $key",
$p->getRequired() ?? false,
false,
null,
$p->getSchema() ?? ['type' => 'string'],
$p->getSchema() ?? $defaultSchema,
);

$linkParameter = $p->getOpenApi();
Expand Down
4 changes: 2 additions & 2 deletions src/State/Provider/ParameterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
unset($parameter->getExtraProperties()['_api_values']);
}

if (null !== ($default = $parameter->getSchema()['default'] ?? null) && $value instanceof ParameterNotFound) {
if (null !== ($default = $parameter->getDefault() ?? $parameter->getSchema()['default'] ?? null) && $value instanceof ParameterNotFound) {
$value = $default;
}

Expand Down Expand Up @@ -128,7 +128,7 @@ private function handlePathParameters(HttpOperation $operation, array $uriVariab
unset($uriVariable->getExtraProperties()['_api_values']);
}

if (($default = $uriVariable->getSchema()['default'] ?? false) && ($value instanceof ParameterNotFound || !$value)) {
if (($default = $uriVariable->getDefault() ?? $uriVariable->getSchema()['default'] ?? false) && ($value instanceof ParameterNotFound || !$value)) {
$value = $default;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,27 @@
],
castToNativeType: true,
),
'anotherBooleanParameter' => new QueryParameter(
default: true,
),
],
provider: [self::class, 'provide'],
),
]
)]
class BooleanQueryParameter
{
public function __construct(public bool $booleanParameter)
{
public function __construct(
public bool $booleanParameter,
public bool $anotherBooleanParameter,
) {
}

public static function provide(Operation $operation): self
{
return new self($operation->getParameters()->get('booleanParameter')->getValue());
return new self(
$operation->getParameters()->get('booleanParameter')->getValue(),
$operation->getParameters()->get('anotherBooleanParameter')->getValue(),
);
}
}
31 changes: 30 additions & 1 deletion tests/Fixtures/TestBundle/ApiResource/WithParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\Country;
use Webmozart\Assert\Assert as Assertion;

#[Get(
uriTemplate: 'with_parameters/{id}{._format}',
Expand Down Expand Up @@ -281,7 +282,25 @@
],
provider: [self::class, 'collectionProvider'],
)]

#[GetCollection(
uriTemplate: 'parameter_defaults',
parameters: [
'false_as_default' => new QueryParameter(
default: false,
),
'true_as_default' => new QueryParameter(
default: true,
),
'foo_as_default' => new QueryParameter(
default: 'foo',
),
'required_with_a_default' => new QueryParameter(
required: true,
default: 'bar'
),
],
provider: [self::class, 'checkParameterDefaults'],
)]
#[QueryParameter(key: 'everywhere')]
class WithParameter
{
Expand Down Expand Up @@ -372,4 +391,14 @@ public static function provideDummyFromParameter(Operation $operation, array $ur
{
return $operation->getParameters()->get('dummy')->getValue();
}

public static function checkParameterDefaults(Operation $operation, array $uriVariables = [], array $context = []): JsonResponse
{
Assertion::false($operation->getParameters()->get('false_as_default')->getValue());
Assertion::true($operation->getParameters()->get('true_as_default')->getValue());
Assertion::same($operation->getParameters()->get('foo_as_default')->getValue(), 'foo');
Assertion::same($operation->getParameters()->get('required_with_a_default')->getValue(), 'bar');

return new JsonResponse([]);
}
}
6 changes: 6 additions & 0 deletions tests/Functional/Parameters/ParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,10 @@ public static function provideCountriesValues(): iterable
yield 'valid country' => ['country=FR', 200];
yield 'array of countries' => ['country[]=FR', 200];
}

public function testDefaultValues(): void
{
self::createClient()->request('GET', 'parameter_defaults');
$this->assertResponseIsSuccessful();
}
}
Loading