Skip to content

Commit f1c3bbb

Browse files
feat: introduce Parameter::castToNativeType
1 parent a52c4a9 commit f1c3bbb

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/Metadata/Parameter.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function __construct(
5151
protected array|string|null $filterContext = null,
5252
protected ?Type $nativeType = null,
5353
protected ?bool $castToArray = null,
54+
protected ?bool $castToNativeType = null,
5455
) {
5556
}
5657

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

333334
return $self;
334335
}
336+
337+
public function getCastToNativeType(): ?bool
338+
{
339+
return $this->castToNativeType;
340+
}
341+
342+
public function withCastToNativeType(bool $castToNativeType): self
343+
{
344+
$self = clone $this;
345+
$self->castToNativeType = $castToNativeType;
346+
347+
return $self;
348+
}
335349
}

src/State/Util/ParameterParserTrait.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
use ApiPlatform\Metadata\Parameter;
1919
use ApiPlatform\State\ParameterNotFound;
2020
use Symfony\Component\HttpFoundation\Request;
21+
use Symfony\Component\TypeInfo\Type;
2122
use Symfony\Component\TypeInfo\Type\CollectionType;
2223
use Symfony\Component\TypeInfo\Type\UnionType;
24+
use Symfony\Component\TypeInfo\TypeIdentifier;
2325

2426
/**
2527
* @internal
@@ -72,14 +74,21 @@ private function extractParameterValues(Parameter $parameter, array $values): st
7274
$value = $value[$accessor];
7375
} else {
7476
$value = new ParameterNotFound();
75-
continue;
7677
}
7778
}
7879

7980
if ($value instanceof ParameterNotFound) {
8081
return $value;
8182
}
8283

84+
if ($parameter->getNativeType()?->isIdentifiedBy(TypeIdentifier::BOOL) && $parameter->getCastToNativeType()) {
85+
$value = match ($value) {
86+
'true', 1, '1' => true,
87+
'false', 0, '0' => false,
88+
default => $value,
89+
};
90+
}
91+
8392
$isCollectionType = fn ($t) => $t instanceof CollectionType;
8493
$isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;
8594

src/Validator/Metadata/Resource/Factory/ParameterValidationResourceMetadataCollectionFactory.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,10 @@ private function addSchemaValidation(Parameter $parameter, ?array $schema = null
190190
$assertions[] = new Unique();
191191
}
192192

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

197-
// Allow null in case of optional parameter
198-
if (isset($schema['type']) && 'boolean' === $schema['type']) {
199-
$assertions[] = new Expression(expression: 'value in [null, 0, 1, "0", "1", false, true, "false", "true"]');
200-
}
201-
202197
if (!$assertions) {
203198
return $parameter;
204199
}

0 commit comments

Comments
 (0)