Skip to content

tests: test parameters type detection #7240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 4.1
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions src/Metadata/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function __construct(
protected array|string|null $filterContext = null,
protected ?Type $nativeType = null,
protected ?bool $castToArray = null,
protected ?bool $castToNativeType = null,
) {
}

Expand Down Expand Up @@ -332,4 +333,17 @@ public function withCastToArray(bool $castToArray): self

return $self;
}

public function getCastToNativeType(): ?bool
{
return $this->castToNativeType;
}

public function withCastToNativeType(bool $castToNativeType): self
{
$self = clone $this;
$self->castToNativeType = $castToNativeType;

return $self;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ private function getDefaultParameters(Operation $operation, string $resourceClas
$parameter = $parameter->withNativeType(Type::string());
} elseif ('boolean' === ($parameter->getSchema()['type'] ?? null)) {
$parameter = $parameter->withNativeType(Type::bool());
} elseif ('integer' === ($parameter->getSchema()['type'] ?? null)) {
$parameter = $parameter->withNativeType(Type::int());
} elseif ('number' === ($parameter->getSchema()['type'] ?? null)) {
$parameter = $parameter->withNativeType(Type::float());
} else {
$parameter = $parameter->withNativeType(Type::union(Type::string(), Type::list(Type::string())));
}
Expand Down
11 changes: 10 additions & 1 deletion src/State/Util/ParameterParserTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
use ApiPlatform\Metadata\Parameter;
use ApiPlatform\State\ParameterNotFound;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\Type\UnionType;
use Symfony\Component\TypeInfo\TypeIdentifier;

/**
* @internal
Expand Down Expand Up @@ -72,14 +74,21 @@ private function extractParameterValues(Parameter $parameter, array $values): st
$value = $value[$accessor];
} else {
$value = new ParameterNotFound();
continue;
}
}

if ($value instanceof ParameterNotFound) {
return $value;
}

if ($parameter->getNativeType()?->isIdentifiedBy(TypeIdentifier::BOOL) && $parameter->getCastToNativeType()) {
$value = match ($value) {
'true', 1, '1' => true,
'false', 0, '0' => false,
default => $value,
};
}

$isCollectionType = fn ($t) => $t instanceof CollectionType;
$isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Count;
use Symfony\Component\Validator\Constraints\DivisibleBy;
use Symfony\Component\Validator\Constraints\Expression;
use Symfony\Component\Validator\Constraints\GreaterThan;
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
use Symfony\Component\Validator\Constraints\Length;
Expand Down Expand Up @@ -189,8 +190,8 @@ private function addSchemaValidation(Parameter $parameter, ?array $schema = null
$assertions[] = new Unique();
}

if (isset($schema['type']) && 'array' === $schema['type']) {
$assertions[] = new Type(type: 'array');
if (isset($schema['type']) && \in_array($schema['type'], ['array', 'boolean'], true)) {
$assertions[] = new Type(type: $schema['type']);
}

if (!$assertions) {
Expand Down
67 changes: 66 additions & 1 deletion tests/Fixtures/TestBundle/ApiResource/WithParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,72 @@
'minimum' => 1,
'maximum' => 5,
],
required: true,
),
],
provider: [self::class, 'noopProvider']
)]
#[GetCollection(
uriTemplate: 'header_float',
parameters: [
'Bar' => new HeaderParameter(
schema: [
'type' => 'number',
'example' => 42.0,
'minimum' => 1.0,
'maximum' => 100.0,
'multipleOf' => 0.01,
],
),
],
provider: [self::class, 'noopProvider']
)]
#[GetCollection(
uriTemplate: 'header_boolean',
parameters: [
'Lorem' => new HeaderParameter(
schema: [
'type' => 'boolean',
],
),
],
provider: [self::class, 'noopProvider']
)]
#[GetCollection(
uriTemplate: 'query_integer',
parameters: [
'Foo' => new QueryParameter(
schema: [
'type' => 'integer',
'example' => 3,
'minimum' => 1,
'maximum' => 5,
],
),
],
provider: [self::class, 'noopProvider']
)]
#[GetCollection(
uriTemplate: 'query_float',
parameters: [
'Bar' => new QueryParameter(
schema: [
'type' => 'number',
'example' => 42.0,
'minimum' => 1.0,
'maximum' => 100.0,
'multipleOf' => 0.01,
],
),
],
provider: [self::class, 'noopProvider']
)]
#[GetCollection(
uriTemplate: 'query_boolean',
parameters: [
'Lorem' => new QueryParameter(
schema: [
'type' => 'boolean',
],
),
],
provider: [self::class, 'noopProvider']
Expand Down
10 changes: 2 additions & 8 deletions tests/Functional/Parameters/BooleanFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,8 @@ public static function booleanFilterScenariosProvider(): \Generator
#[DataProvider('booleanFilterNullAndEmptyScenariosProvider')]
public function testBooleanFilterWithNullAndEmptyValues(string $url): void
{
$response = self::createClient()->request('GET', $url);
$this->assertResponseIsSuccessful();

$responseData = $response->toArray();
$filteredItems = $responseData['hydra:member'];

$expectedItemCount = 3;
$this->assertCount($expectedItemCount, $filteredItems, \sprintf('Expected %d items for URL %s', $expectedItemCount, $url));
self::createClient()->request('GET', $url);
$this->assertResponseStatusCodeSame(422);
}

public static function booleanFilterNullAndEmptyScenariosProvider(): \Generator
Expand Down
66 changes: 60 additions & 6 deletions tests/Functional/Parameters/ParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,72 @@ public function testHeaderParameterRequired(): void
}

#[DataProvider('provideHeaderValues')]
public function testHeaderParameterInteger(string $value, int $expectedStatusCode): void
public function testHeaderParameter(string $url, array $headers, int $expectedStatusCode): void
{
self::createClient()->request('GET', 'header_integer', ['headers' => ['Foo' => $value]]);
self::createClient()->request('GET', $url, ['headers' => $headers]);
$this->assertResponseStatusCodeSame($expectedStatusCode);
}

public static function provideHeaderValues(): iterable
{
yield 'valid integer' => ['3', 200];
yield 'too high' => ['6', 422];
yield 'too low' => ['0', 422];
yield 'invalid integer' => ['string', 422];
// header_integer
yield 'missing header header_integer' => ['header_integer', [], 200];
yield 'valid integer header_integer' => ['header_integer', ['Foo' => '3'], 200];
yield 'too high header_integer' => ['header_integer', ['Foo' => '6'], 422];
yield 'too low header_integer' => ['header_integer', ['Foo' => '0'], 422];
yield 'invalid integer header_integer' => ['header_integer', ['Foo' => 'string'], 422];

// header_float
yield 'missing header header_float' => ['header_float', [], 200];
yield 'valid float header_float' => ['header_float', ['Bar' => '3.5'], 200];
yield 'valid integer header_float' => ['header_float', ['Bar' => '3'], 200];
yield 'too high header_float' => ['header_float', ['Bar' => '600'], 422];
yield 'too low header_float' => ['header_float', ['Bar' => '0'], 422];
yield 'invalid number header_float' => ['header_float', ['Bar' => 'string'], 422];

// header_boolean
yield 'missing header header_boolean' => ['header_boolean', [], 200];
yield 'valid boolean false header_boolean' => ['header_boolean', ['Lorem' => 'false'], 200];
yield 'valid boolean true header_boolean' => ['header_boolean', ['Lorem' => 'true'], 200];
yield 'valid boolean 0 header_boolean' => ['header_boolean', ['Lorem' => 0], 200];
yield 'valid boolean 0 string header_boolean' => ['header_boolean', ['Lorem' => '0'], 200];
yield 'valid boolean 1 header_boolean' => ['header_boolean', ['Lorem' => 1], 200];
yield 'valid boolean 1 string header_boolean' => ['header_boolean', ['Lorem' => '1'], 200];
yield 'invalid boolean header_boolean' => ['header_boolean', ['Lorem' => 'string'], 422];
}

#[DataProvider('provideQueryValues')]
public function testQueryParameter(string $url, array $query, int $expectedStatusCode): void
{
self::createClient()->request('GET', $url, ['query' => $query]);
$this->assertResponseStatusCodeSame($expectedStatusCode);
}

public static function provideQueryValues(): iterable
{
// query_integer
yield 'valid integer query_integer' => ['query_integer', ['Foo' => '3'], 200];
yield 'too high query_integer' => ['query_integer', ['Foo' => '6'], 422];
yield 'too low query_integer' => ['query_integer', ['Foo' => '0'], 422];
yield 'invalid integer query_integer' => ['query_integer', ['Foo' => 'string'], 422];

// query_float
yield 'valid float query_float' => ['query_float', ['Bar' => '3.5'], 200];
yield 'valid integer query_float' => ['query_float', ['Bar' => '3'], 200];
yield 'too high query_float' => ['query_float', ['Bar' => '600'], 422];
yield 'too low query_float' => ['query_float', ['Bar' => '0'], 422];
yield 'invalid number query_float' => ['query_float', ['Bar' => 'string'], 422];

// query_boolean
yield 'valid boolean false query_boolean' => ['query_boolean', ['Lorem' => false], 200];
yield 'valid boolean false string query_boolean' => ['query_boolean', ['Lorem' => 'false'], 200];
yield 'valid boolean true query_boolean' => ['query_boolean', ['Lorem' => true], 200];
yield 'valid boolean true string query_boolean' => ['query_boolean', ['Lorem' => 'true'], 200];
yield 'valid boolean 0 query_boolean' => ['query_boolean', ['Lorem' => 0], 200];
yield 'valid boolean 0 string query_boolean' => ['query_boolean', ['Lorem' => '0'], 200];
yield 'valid boolean 1 query_boolean' => ['query_boolean', ['Lorem' => 1], 200];
yield 'valid boolean 1 string query_boolean' => ['query_boolean', ['Lorem' => '1'], 200];
yield 'invalid boolean query_boolean' => ['query_boolean', ['Lorem' => 'string'], 422];
}

#[DataProvider('provideCountryValues')]
Expand Down
Loading