Skip to content

Commit b34e067

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

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
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: 9 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,20 @@ 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' => true,
87+
'false' => false,
88+
};
89+
}
90+
8391
$isCollectionType = fn ($t) => $t instanceof CollectionType;
8492
$isCollection = $parameter->getNativeType()?->isSatisfiedBy($isCollectionType) ?? false;
8593

0 commit comments

Comments
 (0)