Skip to content

Commit ecff762

Browse files
committed
fix(state): parameter default value overrides falsy value
fixes #7354
1 parent 8edbb25 commit ecff762

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

src/State/Provider/ParameterProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
7777
unset($parameter->getExtraProperties()['_api_values']);
7878
}
7979

80-
if (($default = $parameter->getSchema()['default'] ?? false) && ($value instanceof ParameterNotFound || !$value)) {
80+
if (($default = $parameter->getSchema()['default'] ?? false) && $value instanceof ParameterNotFound) {
8181
$value = $default;
8282
}
8383

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7354;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Get;
18+
use ApiPlatform\Metadata\Operation;
19+
use ApiPlatform\Metadata\QueryParameter;
20+
21+
#[ApiResource(
22+
operations: [
23+
new Get(
24+
normalizationContext: ['hydra_prefix' => false],
25+
uriTemplate: '/issue7354_boolean_query_parameters',
26+
parameters: [
27+
'booleanParameter' => new QueryParameter(
28+
schema: [
29+
'type' => 'boolean',
30+
'default' => true,
31+
],
32+
castToNativeType: true,
33+
),
34+
],
35+
provider: [self::class, 'provide'],
36+
),
37+
]
38+
)]
39+
class BooleanQueryParameter
40+
{
41+
public function __construct(public bool $booleanParameter)
42+
{
43+
}
44+
45+
public static function provide(Operation $operation): self
46+
{
47+
return new self($operation->getParameters()->get('booleanParameter')->getValue());
48+
}
49+
}

tests/Functional/Issue7354Test.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Functional;
15+
16+
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
17+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7354\BooleanQueryParameter;
18+
use ApiPlatform\Tests\SetupClassResourcesTrait;
19+
20+
final class Issue7354Test extends ApiTestCase
21+
{
22+
use SetupClassResourcesTrait;
23+
24+
protected static ?bool $alwaysBootKernel = false;
25+
26+
/**
27+
* @return class-string[]
28+
*/
29+
public static function getResources(): array
30+
{
31+
return [BooleanQueryParameter::class];
32+
}
33+
34+
public function testBooleanQueryParameterDefaultOverride(): void
35+
{
36+
self::createClient()->request('GET', '/issue7354_boolean_query_parameters?booleanParameter=false');
37+
$this->assertResponseIsSuccessful();
38+
$this->assertJsonContains(['booleanParameter' => false]);
39+
}
40+
41+
public function testBooleanQueryParameterDefaultNotOverride(): void
42+
{
43+
self::createClient()->request('GET', '/issue7354_boolean_query_parameters?booleanParameter=true');
44+
$this->assertResponseIsSuccessful();
45+
$this->assertJsonContains(['booleanParameter' => true]);
46+
}
47+
48+
public function testBooleanQueryParameterDefaultValue(): void
49+
{
50+
self::createClient()->request('GET', '/issue7354_boolean_query_parameters');
51+
$this->assertResponseIsSuccessful();
52+
$this->assertJsonContains(['booleanParameter' => true]);
53+
}
54+
}

0 commit comments

Comments
 (0)